Home
  • Deutsch (DE-CH-AT)
  • English (United Kingdom)
  • Japanese(JP)
Accelerating Linq2SQL PDF Print
Tuesday, 21 September 2010 17:30

Note: The project is now available on http://linqaccelerator.codeplex.com. Sources and binaries can be downloaded from there.


I was using Linq2SQL, an excellent C# extension for database connectivity, quite a lot since it got released with VS2008.

One thing I have noticed very early is that when using Linq, there was a lot of code I was writing over and over again.

Whenever I wanted to get an object from the database, set it or delete it, I was basically writing the same code.

The following snippets sample the "set", "get", and "delete" I have mentioned above.

 

Set

Get

Delete

 

When you are dealing a lot with different tables or objects that you have persisted into a table, you will find yourself implementing

the above sort of functions over and over again without writing something new. I have tried to factor these three functions out into

something that can be used for any arbitrary get/set/delete database operation. My solution heavily relies upon a C# language features

that is called "Generics" as well as a feature that is called "Lambda expressions". I thought it would look cool, if we were able to specify

the data context from which we want to read as a Generic type parameter while we pass the "search rule" of the object(s) we are looking

for as a Lambda to a function. My thoughts resulted into the following:

 

SetLinqObject


Usage: SetLinqObject<Linq2SQLType, Linq2SQLTypeDataContext>((x) => x.ID == strID, Linq2SQLTypeInstance);


GetLinqObjectFirst


Usage: GetLinqObjectFirst<Linq2SQLType, Linq2SQLTypeDataContext>((x) => x.ID == strID);


GetLinqObjectAll


Usage: GetLinqObjectAll<Linq2SQLType, Linq2SQLTypeDataContext>((x) => x.ID == strID);


DeleteLinqObject


Usage: DetLinqObject<Linq2SQLType, Linq2SQLTypeDataContext>((x) => x.ID == strID);


 

This should give you a handy tool with a stunning syntax. We have transformed 5 - 8 lines of code into 1 single line of code.

Of course, you can catch the exception in the surrounding code and rethrow whatever you want to rethrow here or just directly

process the exception.

 

 

Thanks,

Robert

 

 

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace System.Linq
{
public abstract class UT_Linq2SQL
{
public static T GetLinqObjectFirst<T, U>(Func<T, bool> _func)
where T : class
where U : System.Data.Linq.DataContext, new()
{
System.Data.Linq.DataContext ctx = new U();
var query = ctx.GetTable<T>().Where(_func);

if (query.Count() == 0)
throw new Exception("There is no such object in the database.");

return query.First();
}

public static T[] GetLinqObjectAll<T, U>(Func<T, bool> _func)
where T : class
where U : System.Data.Linq.DataContext, new()
{
System.Data.Linq.DataContext ctx = new U();
var query = ctx.GetTable<T>().Where(_func);

if (query.Count() == 0)
throw new Exception("There are no such objects in the database.");

return query.ToArray();
}

public static void SetLinqObject<T, U>(Func<T, bool> _func, T _object)
where T : class
where U : System.Data.Linq.DataContext, new()
{
System.Data.Linq.DataContext ctx = new U();
var query = ctx.GetTable<T>().Where(_func);

if (query.Count() != 0)
{
DeleteLinqObject<T, U>(_func);
ctx.SubmitChanges();

}

ctx = new U();
ctx.GetTable<T>().InsertOnSubmit(_object);
ctx.SubmitChanges();
}

public static void DeleteLinqObject<T, U>(Func<T, bool> _func)
where T : class
where U : System.Data.Linq.DataContext, new()
{
System.Data.Linq.DataContext ctx = new U();
var query = ctx.GetTable<T>().Where(_func);

if (query.Count() == 0)
throw new Exception("There is no such linq object in the databas.");

ctx.GetTable<T>().DeleteOnSubmit(query.First());
ctx.SubmitChanges();
}
}
}

Last Updated on Sunday, 14 November 2010 10:41