Entity classes decoupled from LINQ to SQL provider for implementing the Repository pattern. How?

前端 未结 9 516
不知归路
不知归路 2021-02-02 02:57

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 applicat

9条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-02 03:36

    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

提交回复
热议问题