views:

47

answers:

2

Hi all,

Currently I'm trying to implement LINQ to SQL in my project. I'm trying to use LINQ to SQL with the famous repository pattern. here is what I was trying to do:

  1. Create table, for example customer table.
  2. Generate LINQ to SQL (.dbml) for my customer table.
  3. It generated partial Customer class complete with all the properties and some partial method.

After that I need to put some custom method for ex: GetCustomerById(int id), GetCustomerByName(string name), etc. So I create a partial class of Customer and implement all the function that I need.

Hmmm, from this step I realized that there is something weird that I haven't implemented the repository pattern.. T_T

What I was trying to do was create a business layer that access repository layer, and then repository layer use LINQ to SQL as data model and data access to SQL Server. Is this the best practice? and how to add repository pattern to my customer partial class generated by LINQ to SQL?

Thanks.

+1  A: 

There are many variations of the Repository pattern. A simple definition of it is a module that encapsulates/hides the persistence logic from other components.

So you could have a class called LinqToSqlRepository.cs, with a method like so:

public class LinqToSqlRepository
{
   private YourDataContext _ctx;

   public LinqToSqlRepository()
   {
      _ctx = new YourDataContext();
   }

   public Person GetPersonById(int id)
   {
      return _ctx.Persons.SingleOrDefault(p => p.Id == id);
   }
}

And call it from your Business Layer like so:

var db = new LinqToSqlRepository();
var person = db.GetPersonById(int id);

Ideally you don't want to add your repository to your partial class. It should be seperate. Your Repository class should work off your partial classes.

To be honest though, (IMO) this kind of design pattern is better suited for EntityFramework than Linq2Sql, as it offers much more versatility in terms of POCO's.

There is no way with Linq2Sql to completely de-couple the business entities from the persistence layer. In the above example, you are returning a Person object, which is actually the entity in the L2S designer, therefore the UI will need a reference to this, defeating the point of 'persistence-ignorance'.

However as i said, create your Repository seperate, don't put the methods in the partial class, work with them in your Repository.

HTH.

RPM1984
Wow nice reply RPM1984, I really get what you have explained. Thanks for the Info. Yup, before I try to learn linq-to-sql I've learned EF, and from EF I got the idea of repository pattern that I want to implement in linq-to-sql. Thanks a lot
VinkyH
No worries @VinkyH, glad to help!
RPM1984
A: 

Anyway, use LINQ to Entities.

LINQ to SQL is obsolete

abatishchev
Sorry, but this should be a comment, not an answer. And that question is 2 years old. I wouldn't say LinqToSql is dead, for a lot of simple applications it is very useful. (of course for me, it sucks, EF FTW). Be careful when making a statement like that.
RPM1984
@RPM: I think it's simple too to make L2E simple application like L2S simple application. BUT - it's much more correct to start with L2E from the beginning. (my statement is not unsubstantiated, i gave a link with a lot of opinions and discussions)
abatishchev
@abatishchev - Yes i agree, but there is a BIG difference between opinions and a term like 'obsolete'. Perhaps you should clarify your "answer" with some more detail to the link?
RPM1984
@RPM: Agree if technology's development is stopped by the manufacturer, it's very close to be obsolete. I think L2S should be marked so. "Support old but don't begin new development, use EF instead"
abatishchev