存储库模式 - 聚合根
我试图了解实体框架数据模型中聚合根的位置,以便我知道需要创建哪些存储库。
如果我用关系数据库术语来谈谈,我有一个 ExceptionGroup 对象和一个 Exception 对象(不是 system.exception!)。异常属于异常组,没有异常组就无法存在。
我应该为每个对象创建一个存储库,还是为两个对象创建一个包含方法的存储库?如果我有一个存储库,方法如下......
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)
I am trying to get my head around where the aggregates 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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我正确理解您的模型,听起来您将拥有 ExceptionGroup 的存储库,并且 ExceptionGroup 对象将封装对 Exception 实例的访问和操作(例如,通过公开它们的集合)。这样,两个阶级之间的强迫关系就变得非常明显了。
Jeff Sternal 对类似问题有一个很好的答案:什么是聚合根? 他的示例Order / LineItem 似乎类似。
If I understand your model correctly, it sounds like you would have a repository for
ExceptionGroup
and theExceptionGroup
object would encapsulate access and operations onException
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.