Linq + Unity + AOP = Fun

by Shannon Deminick 20. February 2009 07:28

[Entry migrated from suite101.thefarmdigital.com.au]

I've recently had some time to look at using Dependency Injection alongside of Linq to SQL to manage the DataContext. Because of the way that Microsoft made Linq to SQL, it's not very easy to use dependency injection properly with the DataContext because many of the members in this framework are not interfaces or cannot be instantiated directly from code. Type mocking frameworks can be used to mock these objects but i wanted to see if there was a way around all of this. I've posted a project on the CodeProject website here. A few tricks had to be implemented like creating an interface for the DataContext (IDataContext) and exposing a new property called GetITable exposing a new interface called IEnumerableTable<>. This way, you can resolve IEnumerableTable's from the IDataContext instead of resolving a System.Data.Linq.Table from the GetTable<> method (which you would never be able to implement in a custom DataContext class). Because the IEnumerableTable<> interface is IEnumerable<T> itself, you can run all your normal Linq queries against it as per normal. IEnumerableTable<> also implements ITable so you can perform all of the normal Linq to SQL table methods (i.e. InsertOnSubmit, etc...) on the resolved object.

To get this framework setup and working there's quite a few things you need to build, but it's quite a fun exercise. Included in the source is a custom DataContext called XDataContext which turns the data store into XML files instead of database tables and still uses the Linq to SQL entities that are generated. You can simply just map the IDataContext to any custom data context using the Unity (Microsoft's dependency injection framework) configuration. I've also implemented PolicyInjection into this framework as it is extremely easy to do now that all of the objects are interfaced already. Now it's too easy to cache and log calls to methods.

There's a complete description of everything in the article.

Categories: .Net

Comments

11/8/2009 9:22:59 PM #

That is some inspirational stuff. Never knew that opinions could be this varied. Thanks for all the enthusiasm to offer such helpful information here.

final fantasy xi guide United States

11/12/2009 4:25:59 AM #

You really make it seem so easy with your presentation but I find this topic to be really something which I think I would never understand. It seems too complicated and very broad for me. I am looking forward for your next post, I will try to get the hang of it!

DigBands.com United States

3/26/2011 10:18:00 PM #

Having read this material, I have learned for myself a lot of the new. Thanks
http://www.collegeboard.org/

Mario Piting United States

4/7/2011 12:07:30 PM #

I think Dependency injection is a specific form of inversion of control, where the concern being inverted is the process of obtaining the needed dependency. <a href="custom-writing.org/.../a>

Bill Portugal

4/15/2011 3:38:47 PM #

Your <a href="http://www.consultsites.com/">websites for sale </a> is an absolute must. When it comes to <a href="http://www.consult-soft.co.uk/">websites for sale </a> there is none better than <a href="http://www.consultsites.com/">turnkey websites</a>,  <a href="http://www.consult-soft.co.uk/">dropship websites</a>. In order to <a href="http://www.consultsites.com/">buy a website</a> or an <a href="http://www.consult-soft.co.uk/">online business</a> you need to first adopt a kitty.

Consultsoft United Kingdom

8/11/2011 3:41:21 PM #

I think the LINQ to Objects provider is used for querying in-memory collections, using the local query execution engine of LINQ.
http://youressayhelp.com/

David Portugal