开发者

Repository Pattern - aggregate root

开发者 https://www.devze.com 2023-03-16 18:24 出处:网络
I am trying to get my head around where the aggrega开发者_开发百科tes roots lie in my entity framework data model so I know what repositories I need to create.

I am trying to get my head around where the aggrega开发者_开发百科tes roots lie in my entity framework data model so I know what repositories I need to create.

If I talk in relational database terms for a second, I have an ExceptionGroup object and an Exception object (not system.exception!). An Exception belongs to an ExceptionGroup and cannot exist without an ExceptionGroup.

Should I have a repository for each object or a single repository containing methods for both? If I was to have a single repository the methods would be as follows...

FindAllExceptionsByExceptionGroup(int GroupID)
AddExceptionGroup(ExceptionGroup ExceptionGroup) - because an exception cannot exist without a group.
AddException(DataAccess.Exception Exception)
DeleteExceptionGroupByID(int GroupID)
DeleteExceptionByID(int ExceptionID)
DeleteExceptionByGroup(int GroupID)


If I understand your model correctly, it sounds like you would have a repository for ExceptionGroup and the ExceptionGroup object would encapsulate access and operations on Exception instances (for ex., by exposing a collection of them). In this way, the forced relationship between the two classes becomes very apparent.

Jeff Sternal has an excellent answer to a similar question here: What's an Aggregate Root? His example of Order / LineItem seems analogous.

0

精彩评论

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

关注公众号