How do you abstract your persistence code when using LINQ to SQL?

I like LINQ to SQL, but it pushed me to the fact that when using it, my repository code is generated using the LINQ to SQL framework and, therefore, is closely connected with the SQL Server database.

Are you using LINQ to SQL in an abstract, loosely coupled way, and if so, how did you feel about the problem of saving your code without a database?

+6
linq-to-sql
source share
4 answers

For yourself; I am pleased with the reuse of the object model generated by LINQ / dbml, since in fact the attributes do not harm me, and any other implementation can provide a model with a similar level, but I do not use my data context outside of DAL. So I have something like:

  • IFooRepository - defines methods available both with generated dbml objects and with some classes of POCO classes
  • FooRepository - an implementation that knows about the data context

My repository methods do not reveal LINQ concepts such as IQueryable<T> and Expression<...> , because they are leaky abstractions ; other implementations will work differently (EF supports various aspects of both, for example).

Next, I used to designate most of the properties of the association as internal and used them only during DAL requests (not so much during OO work).

I could display pure POCOs, but I see no advantage. I have a few more thoughts on this: Pragmatic LINQ .

+3
source share

You can use a similar approach, such as the one used by NerdsDinner . This project uses the context as a gateway to the database and creates a repository around it.

This type of repository template adds some conditions, filters, a sort command ... etc. according to the called method, and then returns the IQuerable to the caller, leaving the door open for further modifications.

In fact, you do pretty much the same thing when you create storage around ISibernate ISession.

If you decide to replace LinqtoSql with NHibernate, for example, you just need to request a Linq session inside your repository instead of datacontext. Of course, you will need to at least complete partial classes with properties that LinqtoSql automatically adds.

+1
source share

Hey nathan, good question.

As others have said, it's best to distract all of your GET / WRITE methods and completely hide the DataContext from your business logic.

My approach to this was to write a DAL with a bunch of common methods that use reflection to do whatever is needed.

In these cases, as soon as the method receives an object, say, β€œProduct”, it can do whatever it wants, regardless of you. Technology ORM / Data Access. If you want him to literally just write an SQL string based on several parameters and reflection of the object.

HOWEVER , only one will not completely undo you from LINQ to SQL.

The problem is really in the entities themselves. Even if you draw a method to retrieve data, you will still use these Entites in your business logic.

I feel that at the moment this is what I’m ready to live with, because rewriting my own disconnected Entities seems like a bridge that I’m not yet prepared for, simply because it is very similar to unnecessary work ...

I would be very interested to see how others handle this, though.

Hooray!

0
source share

Mouk mentions the repository template in NerdsDinner above, there is also a tutorial on how to quickly set it up here:

http://www.asp.net/learn/mvc/tutorial-10-cs.aspx

This is a good little series of tutorials released before NerdsDinner. The concept here can also be installed in a traditional asp.net web form application.

In addition, I would suggest using Linq To Entities (newer and more support for other databases), and some basics here: http://www.asp.net/learn/mvc/tutorial-16-cs.aspx

-one
source share

All Articles