Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3280856
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:45:05+00:00 2026-05-17T19:45:05+00:00

Part of the data layer for my application is a converter cache similar to

  • 0

Part of the data layer for my application is a converter cache similar to System.Data.DataRowExtensions.UnboxT, used in Linq-to-Data. The premise of the cache for all known types I generate a simple converter and cache it. The converter casts the object as T, or in the case of DBNull.Value, returns default(T) (unlike UnboxT which throws an exception on non-nullable value types.) The reason I can’t use UnboxT is that our developers don’t like checking for DBNull before assigning a value from the datarow and they just want it done for them.
We also have a factory to generate helper delegate that instantiates objects off of a DataRow, and it’d be annoying to add logic in the delegate.
i.e. it generates something like this this::

datarow =>
    new MyObject()
    {
        property1 = DBConverterCache<TProperty1>.Converter(datarow[columnName1]),
        property2 = DBConverterCache<TProperty2>.Converter(datarow[columnName2]),
        /*etc...*/

    };

Even more so, I have another annoyance. Objects in the object layer may not match correctly to ones in the database. This is a problem because you can’t unbox things to the “wrong” type. I.e. the property in the object layer is an Int32, the column in the DB is an Int64. To fix this I have for the struct IConvertibles the converter basically does this::

value => value == DBNull.Value ? default(T) : (value as IConvertible).To<Type*>(null);

* in the case of Nullable<T> or Enum it casts to underlying type, then casts up to T

This is ugly as we have to use reflection to generate the call to ToType, which relies on the assumption that they will never expand the IConvertible interface to add more objects that are convertible. It’s a hack but it avoids boxing the return type. Which a method like IConvertible.ToType does.

of course this works just as well:

value => value == DBNull.Value ? default(T) : (T)(value as dynamic);

Maybe even better, as I don’t have to specialize the call based on the type I can just make that my default converter. The only issue I haven’t a clue as to how to use Expression.Dynamic, and I can’t create an expression that takes dynamic as a parameter. I suppose I could just bind it to a static method or the above lambda expression, but I’d like to do everything as expression trees, if possible.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T19:45:05+00:00Added an answer on May 17, 2026 at 7:45 pm

    Well I got this to work, with some good old reflector.

        public static Converter<Object, T> CreateDynamicConverter<T>()
        {
            var param = Expression.Parameter(typeof(object)); 
            var expression = Expression.Lambda<Converter<object, T>>(
                Expression.Condition(
                    Expression.Equal(
                        param,
                        Expression.Constant(
                            DBNull.Value
                        )
                    ),
                    Expression.Default(
                        typeof(T)
                    ),
                    Expression.Dynamic(
                        Binder.Convert(
                            CSharpBinderFlags.ConvertExplicit, 
                            typeof(T), 
                            typeof(MyApplicationNameHere)
                        ), 
                        typeof(T), 
                        param
                    )
                ),
                param
            );
            return expression.Compile();
        }
    

    Seems to be working just fine.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Should the data validation be in the application/business layer or should it part of
As part of a web application users can upload files of data, which generates
I have a SQL data calculation which is used as part of where clause
I am trying to create an initial-data.yml file for a Play! Framework application. Part
If I want to create a 3-Layer ASP.Net application (Presentation Layer, Business Layer, Data
I've an application that has three layers: 1. data layer: exposes Entity Framework entities
Edit: I think I've figured out how to do the binary data part. Double
In a part of my data warehousing stored procedures, i have a procedure that
I've one situation where I need to copy part of the data from one
As part of a bulk load of data from an external source the stageing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.