Repository pattern: One repository class for each entity?

前端 未结 4 1099
忘掉有多难
忘掉有多难 2020-12-05 00:50

Say you have the following entities defined in a LINQ class:

Product
Customer
Category

Should I have one repository class for all:

4条回答
  •  感动是毒
    2020-12-05 01:11

    Here's my point of view. I'm a strict follower of the Repository pattern. There should be 3 methods that take a single entity. Add, Update, Delete, generically defined.

    public interface IRepository
    {
         void Add(T entity);
         void Update(T entity);
         void Delete(T entity);
    }
    

    Beyond those methods, you're dealing with a "Query" or a service method. If I were you, I'd make the repository genrically defined as above, add a "QueryProvider" as shown below and put your business logic where it belongs in either "Services" or in "Commands/Queries" (comes from CQRS, Google it).

    public interface IQueryProvider
    {
         TResult Query(Func, TResult> query);
    }
    

    (Hope my opinion is somewhat useful :) )

提交回复
热议问题