Wrox Programmer Forums
|
BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0
This is the forum to discuss the Wrox book ASP.NET 2.0 Website Programming: Problem - Design - Solution by Marco Bellinaso; ISBN: 9780764584640
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 21st, 2008, 11:24 PM
Authorized User
 
Join Date: Jul 2003
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to Tremmorkeep Send a message via MSN to Tremmorkeep Send a message via Yahoo to Tremmorkeep
Default The IDataReader....a suggestion.

While this book is really, really great...I've found that there are a couple of things I do differently. the first being that rather than create a custom reader for each provider, I place the following function in the DataAccess.cs class

 protected T GetField<T>(IDataReader Reader, string ColumnName)
        {
            int i = Reader.GetOrdinal(ColumnName);

            if (i == -1 || Reader.IsDBNull(i))
                return default(T);
            else
            {
                if (typeof(T).IsEnum == true)
                {
                    return (T)Enum.ToObject(typeof(T), Reader.GetInt32(i));
                }

                return (T)Reader.GetValue(i);
            }
        }


Then you all it easily from any of your functions...

myObject=new myObject(GetField<int>(reader, "MyID"));


use of generics can be maximized, and get rid of some of the errors you may encounter with casting..

matt...

[email protected]
 
Old February 22nd, 2008, 06:10 AM
Friend of Wrox
 
Join Date: Mar 2007
Posts: 488
Thanks: 2
Thanked 11 Times in 10 Posts
Default

matt,

i do a similar type thing with the following in DataAccess.cs:

      /// <summary>
      /// Cast DBNull using generics
      /// </summary>
      public static T CastTo<T>(object value)
      {
          return value != DBNull.Value ? (T)value : default(T);
      }

I then in the provider call it as such (in this case the DAL/Provider/SearchProvider.cs):

        protected virtual SearchItemEntity GetSearchItemFromReader(IDataReader reader)
        {
            return new SearchItemEntity(
                DataAccess.CastTo<string>(reader["linkID"]),
                DataAccess.CastTo<string>(reader["descriptiontext"]),
                DataAccess.CastTo<string>(reader["category"]),
                DataAccess.CastTo<string>(reader["title"]),
                DataAccess.CastTo<string>(reader["source"]),
                DataAccess.CastTo<DateTime>(reader["lasteditdate"]));
        }

i 'think' this is a similar use of generics to what you're doing above ;)



jimi

http://www.originaltalent.com
 
Old February 22nd, 2008, 07:11 AM
Friend of Wrox
 
Join Date: Mar 2007
Posts: 488
Thanks: 2
Thanked 11 Times in 10 Posts
Default

matt,

similarly, i use delegates in the BLL/Articles/Articles.cs (and all BLL/*.* classes where appropriate). having read up on it, it's supposedly more efficient and more performant:

        /// <summary>
        /// Returns a list of Article objects filled with the data taken from the input list of ArticleDetails
        /// </summary>
        private static List<Article> GetArticleListFromArticleDetailsList(List<ArticleD etails> recordset)
        {
            List<Article> articles = new List<Article>();
            recordset.ForEach(delegate(ArticleDetails record)
            {
                articles.Add(GetArticleFromArticleDetails(record)) ;
            });
            return articles;
        }

can't say i've got that much data yet to be able to compare, but i've convinced myself :D

jimi

http://www.originaltalent.com
 
Old February 22nd, 2008, 11:37 AM
Authorized User
 
Join Date: Jul 2003
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to Tremmorkeep Send a message via MSN to Tremmorkeep Send a message via Yahoo to Tremmorkeep
Default

They are quite efficient. Yours does basically the same thing as mine, though I"m storing the Int32 value of some enums in the database, so I had to accomodate converting that back into my enum for use in code...all I do is pass in the enum type when I call GetField.


GetField<EnumCategories>(reader, "CategoryType")

and it returns the proper enum for the code...I like what you did though, fast and efficient.







Similar Threads
Thread Thread Starter Forum Replies Last Post
Two question for suggestion? ssomchai BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 0 April 30th, 2008 09:31 PM
Getting ahead of IDataReader? technimedia BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 13 July 31st, 2007 11:34 AM
Need your suggestion... popp SQL Language 1 July 2nd, 2007 01:44 AM
Suggestion Snib Forum and Wrox.com Feedback 2 April 10th, 2004 04:27 PM
Suggestion Ben Horne Flash (all versions) 4 April 7th, 2004 09:18 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.