Say goodbye to the wrong Repository mode !, Farewell to repository Mode

Source: Internet
Author: User

Say goodbye to the wrong Repository mode !, Farewell to repository Mode
Introduction

 

 

I have worked in two workshops! The framework used by the project manager is based on the EF Repository (warehouse) design model. I would like to say that this model is simply incorrect! What's more terrible is that there are many such tutorials in the blog Park!

The reason is wrong! Because EF itself is the design of Repository (warehouse! Why should I find another package for myself? It's also ugly! Let's take a look at the Repository code idea first!

The following code extracts the public operations of an object as the IRepository interface, such as the common CRUD Method

 

 public interface IRepository<T> where T : class    {         IEnumerable<T> FindAll(Func<T, bool> exp);        void Add(T entity);        void Delete(T entity);        void Update(T entity);    }

 

Then, the generic class is used to implement the method of the above interface.

 1 public class Repository<T> : IRepository<T> where T:class 2     { 3         public DataContext context; 4  5         public Repository(DataContext context) 6         { 7             this.context = context; 8         } 9         public IEnumerable<T> FindAll(Func<T, bool> exp)10         {11             return context.GetTable<T>().Where(exp);12         }13         public void Add(T entity)14         {15             context.GetTable<T>().InsertOnSubmit(entity);16         }17         public void Delete(T entity)18         {19             context.GetTable<T>().DeleteOnSubmit(entity);20         }21         public void Update()22         {23             context.SubmitChanges();24         }25 26     }

 

Then, in The BLL business layer, call this Repository object through dependency injection to insert and delete the object! I have implemented CRUD in a simple way! I have to set more interfaces! Why? Why? EF itself implements the warehouse model!

I believe someone will say"The biggest advantage of using this mode is to decouple the domain model code from the data ing layer. ",But you can use an interface! Decoupling is also possible!

 

Interface IDbProvide {// return db service model DbContext GetDb ();} // implement interface public class ModelContext: DbContext, IDbProvide {public ModelContext (): base ("name = DefaultContext ") {Database. setInitializer (new DropCreateDatabaseIfModelChanges <ModelContext> ();} DbContext IDbProvide. getDb () {return this ;}}

 

 

In this way, I can still decouple the business logic and data ing layers through dependency injection on The BLL layer!

 

 

// Student business public class StudentBll: IStudentBll {IDbProvide IDal {get; set;} public StudentBll () {// The dependency injection business logic layer does not depend on the data layer IDal = IocDal <IDbProvide>. provide;} // public students getting the highest score public Student CalculateScroebyTop01 () {var stus = IDal. getDb (). orderByDescending (s => s. totalScore ). first (); return stus. orderByDescending (s => s. totalScore ). first ();}}

 

 

Disagree!

 

 

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.