开发者

C# - Same Repository, or split?

I have a table named "Case" and therefore also an object named "Case". I also got a table named "CaseReplies" which has many-to-one relationship with the "Case" table - the "CaseReplies" table also have its own object named "CaseReply"

Should I create different repositories for those two, or should they both operate under the repository originally dedicated to the "Cas开发者_如何转开发e" table/object?


Repositories should be per aggregate root. 1 to many relationship is super strong sign that Case is aggregate root and case reply is entity (case reply does not exist w/o case).

So - I believe You should have only 1 repository - CaseRepository.

This of course applies only if You are following domain driven design. If not and just trying to abstract away persistence - do as You like.


Most likely they will have separate repositories as CaseReply can have operations not involving its parent (update, delete).

Also when a case is deleted this needs to be cascaded to the CaseReply. If cascading is not set at the database level, this has to be implemented as a transaction hence you need a single UnitOfWork passed to both repositories.

Now I can imagine CaseManager class to have access to both repositories and in fact, this could be just one class for both Case and CaseReply handling all operations related to case or CaseReply.


(Update)

I was pointed out to a post to avoid using generic repositories. Well I do find using repositories useful and here is how I use it:

What I do is to have a generic repository IRepository<T>, then have a BaseRepository<T> to implement common repository patterns and do all the database related stuff. This would in fact expose a protected IQueryable<T> so that individual repositories inheriting it, have convenient access to Linq side of the repo. Now, most likely my individual repository would have very specific interface such as "give me 10 latest orders", "give me orders for this customer only" which I will have as IOrderRepository. If IOrderRepository needs all or most of IRepository<T> interface, I will have IOrderRepository : IRepository<T>.

Now my OrderRepository will be

  public class `OrderRepository` : `IOrderRepository`,  `BaseRepository<T>`
  {
      // ...

so that it reuses all the benefits of base repo and also implement a specific repository.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜