Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Extension Methods
Message
De
27/01/2010 10:07:16
 
 
À
27/01/2010 06:00:42
Information générale
Forum:
ASP.NET
Catégorie:
Conception classe
Divers
Thread ID:
01446025
Message ID:
01446151
Vues:
28
Part of the reason I used a gazillion overloads in my methods is so that casting to the proper type wasn't necessary ... but that's not the point of your suggestion, just a point I wanted to make.

You've got a great idea though ... I could change my code to be extensions of DataRow instead of object. I like that. Thanks for the idea ... looks like I'll have to change my blog yet again!

~~Bonnie



>>No, sorry Kevin, I don't currently have any downloading capabilities on my blog. I haven't bothered yet to figure out if I can do it or not from blogspot. Probably can, but I don't know for sure.
>>
>>BTW, the main thing that I *don't* like about those extension methods is that all those GetNonNull() methods are extensions for "object" (remember, the methods were originally intended to work with values in DataTable columns) ... consequently, Intellisense "picks up" those methods for *everything* (since everything is an object). I find it annoying.
>
>Would something like this have worked as a more 'generic' approach:
    public static class BBExtensions
>    {
>        public static object GetNonNull(this System.Data.DataRow dataRow, string columnName, object useValue)
>        {
>            if (dataRow.Table.Columns[columnName] == null)
>                throw new Exception("Column does not belong to Table");
>
>            var v = dataRow[columnName];
>            if (v != DBNull.Value) return v;
>
>            System.Data.DataColumn dc = dataRow.Table.Columns[columnName];
>
>            if (useValue != null)
>            {
>                if (dc.DataType != useValue.GetType())
>                {
>                    throw new Exception("Invalid type for default");
>                }
>                return useValue;
>            }
>            else
>            {
>                if (dc.DataType == typeof(string))
>                    return "";
>                else if (dc.DataType == typeof(DateTime))
>                    return DateTime.Now;
>                //  etc for other types
>                else throw new Exception("No default supplied for " + dc.DataType.ToString());
>            }
>        }
>
>        public static object GetNonNull(this System.Data.DataRow dataRow,string columnName)
>        {
>            return GetNonNull(dataRow, columnName, null);
>        }
>    }
Usage then becomes:
DateTime date = (DateTime) dt.Rows[0].GetNonNull("MyDateColumn", DateTime.Now);
>string s = (string)dt.Rows[0].GetNonNull("MyStringColumn");
>
>
>>
>>~~Bonnie
>>
>>
>>
>>
>>>Bonnie,
>>>
>>>I noticed that you updated your blog with a new version of your extension methods. Looks great. When I try to copy the code, it all comes out as one line. No breaks/formatting.
>>>
>>>Do you have this class in a download anywhere?
>>>
>>>
>>>Thanks
>>>K
Bonnie Berent DeWitt
NET/C# MVP since 2003

http://geek-goddess-bonnie.blogspot.com
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform