Entity classes decoupled from LINQ to SQL provider

2019-03-10 05:20发布

I have looked over the Repository pattern and I recognized some ideas that I was using in the past which made me feel well.

However now I would like to write an application that would use this pattern BUT I WOULD LIKE TO HAVE THE ENTITY CLASSES DECOUPLED from the repository provider.

I would create several assemblies :

  1. an "Interfaces" assembly which would host common interfaces including the IRepository interface
  2. an "Entities" assembly which would host the entity classes such as Product, User, Order and so on. This assembly would be referenced by the "Interfaces" assembly since some methods would return such types or arrays of them. Also it would be referenced by the main application assembly (such as the Web Application)
  3. one or more Repository provider assembly/assemblies. Each would include (at least) a class that implements the IRepository interface and it would work with a certain Data Store. Data stores could include an SQL Server, an Oracle server, MySQL, XML files, Web / WCF services and so on.

Studying LINQ to SQL which looks very productive in terms of time taken to implement all seems well until I discover the deep dependency between the generated classes and the CustomDataContext class.

How can I use LINQ to SQL in such a scenario?

9条回答
Animai°情兽
2楼-- · 2019-03-10 06:02

I did something similar with WCF

1 On your DBML, set you Serialization mode to Unidirectional

2 Set ALL columns on your tables to UpdateCheck=false

3 Write your service something like the following:

   public class Service1 : IService1
    {
        public Company GetCompany(int companyId)
        {
            using (DataClasses1DataContext dc = new DataClasses1DataContext())
            {
                return (from c in dc.Companies where c.CompanyId == companyId select c).Single();
            }
        }

    public void SaveCompany(Company company)
    {
        using (DataClasses1DataContext dc = new DataClasses1DataContext())
        {
            dc.Companies.Attach(company, true);
            dc.SubmitChanges();
        }
    }

    public void InsertCompany(Company company)
    {
        using (DataClasses1DataContext dc = new DataClasses1DataContext())
        {
            dc.Companies.InsertOnSubmit(company);
            dc.SubmitChanges();
        }
    }
}

4 Add a service reference

查看更多
\"骚年 ilove
3楼-- · 2019-03-10 06:07

Could your Entity classes implement IProduct, IUser, IOrder etc. interfaces that would be declared in your "Interfaces" assembly? This way the IRepository interface references only the business object interfaces (i.e., returns collections of IProduct etc.) and the "Interfaces" assembly is decoupled from your other implementation-specific assemblies.

查看更多
爷的心禁止访问
4楼-- · 2019-03-10 06:09

I think you want POCO (Plain Old CLR Objects) support. LINQ to SQL has a adapter called Close2Poco.

But I would advise making the switch to Entity Framework, at the moment they also have a POCO adapter, but in v2 its expected to be supported out of the box.

查看更多
登录 后发表回答