Question

This is pure curiosity/challenge, no practical importance at all. So I'm not looking for alternate solutions that get the job done.

From this question Most efficient way to check for DBNull and then assign to a variable? I found this answer which looks like:

oSomeObject.IntMemeber = oRow["Value"] as int? ?? iDefault;
oSomeObject.StringMember = oRow["Name"] as string ?? sDefault;

Can I move the above expressions to one generic function (or two, or more) so that it accepts both int? and string as well and that I can call it like:

oSomeObject.IntMemeber = oRow.Read<int?>("Value", 0); //iDefault is now 0
//or
oSomeObject.IntMemeber = oRow.Read<int>("Value"); //iDefault is now default(int)

//and 
oSomeObject.StringMember = oRow.Read<string>("Name"); //sDefault is now default(string)

Requirements:

1) I need an option to specify a default value in case of DBNulls. I also need an option to return default(T) in case I dont specify a default value. So this wont work:

public static T Read<T>(this IDataRecord dr, string field, T defaultValue) where T : class
{
    return dr[field] as T ?? defaultValue;
}

public static T? Read<T>(this IDataRecord dr, string field, T? defaultValue) where T : struct
{
    return dr[field] as T? ?? defaultValue;
}

because I cant call oSomeObject.StringMemeber = oRow.Read<string>("Name")

It need not be optional parameter, it can even be an overload:

public static T Read<T>(this IDataRecord dr, string field) where T : class
{
    return dr[field] as T ?? default(T);
}

public static T? Read<T>(this IDataRecord dr, string field) where T : struct
{
    return dr[field] as T? ?? default(T?);
}

public static T Read<T>(this IDataRecord dr, string field, T defaultValue) where T : class
{
    return dr[field] as T ?? defaultValue;
}

public static T? Read<T>(this IDataRecord dr, string field, T? defaultValue) where T : struct
{
    return dr[field] as T? ?? defaultValue;
}

This wont compile, since method 1 and 2 have just the same signature.

2) Generic functions (in case of overloads) should have the same name.

3) as keyword must be used check and cast type. As I stated previously I'm not really looking to solutions to read from IDataRecord or enhance performance or something.


There are similar questions

  1. C# generic class using reference types and nullable value types

  2. Is creating a C# generic method that accepts (nullable) value type and reference type possible?

  3. Can a Generic Method handle both Reference and Nullable Value types?

But this is very specific to as keyword. So the answers there are not applicable.

Addendum: I know there won't be one single solution to this. I will accept which is most elegant alternative.

Was it helpful?

Solution

The answers you linked to seem to pretty clearly indicate that there isn't a good way to have a generic method handle both reference types and value types in the clean way you would like. It seems the answer, as shown there, is to give them different names.

You can, however, reduce your code to just two methods instead of four by using a default value for the defaultValue parameter:

public static T Read<T>(this IDataRecord dr, string field, 
                        T defaultValue = null) where T : class
{
    return dr[field] as T ?? defaultValue;
}

public static T? ReadNullable<T>(this IDataRecord dr, string field, 
                                 T? defaultValue = null) where T : struct
{
    return dr[field] as T? ?? defaultValue;
}

This way you can use dr.ReadNullable(field) when you want the result to be null when the value can't be cast, and dr.ReadNullable(field, someValue) when you want to specify a default value.

As discussed below, the use of default(T) and default(T?) in your original code aren't necessary, since they will always be null anyway.

OTHER TIPS

Idea is to use is instead of as. Below methods will do the trick:

    public static T Read<T>(this IDictionary<string, object> dr, string field, T defaultValue)
    {
        var v = dr[field];
        return v is T ? (T)v : defaultValue;
    }

    public static T Read<T>(this IDictionary<string, object> dr, string field)
    {
        var v = dr[field];
        return v is T ? (T)v : default(T);
    }

Usage:

    Dictionary<string, object> d = new Dictionary<string, object>();

    d["s"] = "string";
    d["i"] = 5;
    d["db.null"] = DBNull.Value;

    Console.WriteLine(d.Read("i", 7));                        // 5
    Console.WriteLine(d.Read("s", "default string"));         // string
    Console.WriteLine(d.Read("db.null", "default string"));   // default string
    Console.WriteLine(d.Read("db.null", -1));                 // -1
    Console.WriteLine(d.Read<int>("i"));                      // 5
    Console.WriteLine(d.Read<string>("s"));                   // string
    Console.WriteLine(d.Read<int>("db.null"));                // 0
    Console.WriteLine(d.Read<string>("db.null") ?? "null");   // null

I have used Dictionary for quick examples, IDataRecord will behave the same way.

Seems there is not one way. JLRishe's answer is good. It relies on renaming overloads. Here is something that doesn't rename functions, but compromise on ease of calling (ie one optional parameter less)

static T Read<T>(this object obj, T defaultValue) where T : class
{
    return obj as T ?? defaultValue;
}

static T? Read<T>(this object obj, T? defaultValue) where T : struct
{
    return obj as T? ?? defaultValue;
}

public static T Read<T>(this IDataRecord dr, string field, 
                        T defaultValue) where T : class //no optional parameter here :(
{
    return dr[index].Read<T>(defaultValue);
}

public static T? Read<T>(this IDataRecord dr, string field, T? defaultValue = null) 
                        where T : struct
{
    return dr[index].Read<T>(defaultValue);
}

And call, for eg.:

Session s = new Session();
s.Id = r.Read("", (byte[])null);
s.UserId = (int)r.Read<int>("");
s.LoginTime = (DateTime)r.Read<DateTime>("");
s.LogoutTime = r.Read("", default(DateTime?));
s.MachineFingerprint = r.Read("", (string)null);

As you see reference types needs to be specified a default value. Not so elegant..

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top