ToffersBrain.AddExtension()

A while ago I had a lot of redundant code that did foreach loops through elements of IQueryable objects being returned from a Linq To Sql DataContext and then converted them into a collection of local MVC model objects…for the purpose of this post I’ll call it LinqToSqlClassModel, so it takes IQueryable<LinqToSqlClass> and turns it into a Collection<LinqToSqlClassModel>.  Why did I need to convert them…it had to do with MVC / WPF issues when a query was being executed by Linq To Sql, but I’m going to save that little gem for another post.

After thinking about it, I thought this was a good time to experiement with something I had read about called Extensions.  Every good coder knows that when you have a repetative piece of code, you take that chunk and turn it into a function.  It makes it easier to maintain any changes needed to the logic and is less prone to typo mistakes.  Extensions are just a way to logically place those functions onto objects that they apply too…even if it’s an object you didn’t directly define…just like the auto generated objects from Linq To Sql.  Sure I could dig through the autogen code and hack a new method into it…but that runs the risk of messing up some voodoo magic autogen stuff and also runs the risk of being completely removed if you have to make any updates to the Linq To Sql model from the database…because it’ll just re-autogen everything and I’ve seen times where my extra code is obliterated because of that.

So instead of doing something silly like that…again…I created a static class called Extensions.  It takes this…

Collection<LinqToSqlClassModel> payload = 
    new Collection<LinqToSqlClassModel>();

IQueryable<LinqToSqlClass> linqToSqlItems =
    DataContext.LinqToSqlClassItems;

foreach (var linqToSqlItem in linqToSqlItems)
{
    LinqToSqlClassModel model = new LinqToSqlClassModel(linqToSqlItem);
    payload.Add(model);
}

and turns it into this…

Collection<LinqToSqlClassModel> payload = 
    DataContext.LinqToSqlClassItems.ToLinqToSqlClassModelCollection();

Ahhhh, much better.  But where did foreach loop go?  That went into a new static method within the Extensions class I created earlier.

public static Collection<LinqToSqlClassModel> ToLinqToSqlClassModelCollection(this IQueryable<LinqToSqlClass> linqToSqlClassItems)
{
    Collection<LinqToSqlClassModel> linqToSqlClassModelCollection =
new Collection<LinqToSqlClassModel>();

    if (linqToSqlClassItems != null)
    {
        foreach (var item in linqToSqlClassItems)
        {
            LinqToSqlClassModel model = new LinqToSqlClassModel(item);
            linqToSqlClassModelCollection.Add(model);
        }
    }

    return linqToSqlClassModelCollection;
}

I thought it was pretty nifty and elegant and made the code organization make a lot more sense in my opinion.

EXTRA CREDIT…before you read this next section be warned this next part DOES NOT WORK.  I tried to take the extensions a step further and figure out how to make it a bit more generic so I wouldn’t have to create a new extension method for every single class I wanted to turn into a collection, but I couldn’t make it work…here’s what I ended with in my failed atempts to figure this out…if anyone reading my blog is interested in taking a crack at it, I’d be more than happy to spend some time on it with you…really this is just something that would up the “cool” factor and woudn’t really help code execution by being any more efficient.  🙂

public static Collection<TOne> ToModelCollection<TOne, TTwo>(this IQuerable<TTwo> tableItems)
{
    Collection<TOne> modelCollection = new Collection<TOne>();

    if (tableItems != null)
    {
        foreach (var tableItem in tableItems)
        {
            TOne newTOne = new TOne(tableItem);
            modelCollection.Add(newTOne);
        }
    }

    return modelCollection;
}

Hope this helps someone.  Enjoy.  🙂

Advertisements
ToffersBrain.AddExtension()

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s