OOD:谁应该从持久层创建对象?
在我的软件中,我有 5 个主要层:
- UI
- CONTROLLER
- DOMAIN(业务规则)
- PERSISTENCE
- MYSQL (db)
我应该在哪一层以及向谁分配从持久层获取域对象的责任?我决定在域层中创建一个新分区,但通过控制器层这样做会使如何创建域对象变得模糊。
我正在寻找一种优雅的解决方案,通过从持久状态获取域对象来创建域对象。
In my software, I have 5 main layers:
- UI
- CONTROLLER
- DOMAIN (business rules)
- PERSISTENCE
- MYSQL (db)
In which layer and to whom should I assign the responsibility to get the domain objects from the persistence layer? I decided to create a new partition in the domain layer, but doing so by the controller layer makes ambiguity about how to create domain objects.
I'm looking for an elegant solution for creating domain objects by taking them from the persistent state.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该从了解上下文/用例的持久层中检索对象。
换句话说:在实现用例的层中,您应该调用可以从数据库检索(或保存)对象的存储库(或其他持久层对象)。
恕我直言,这是唯一正确的做法。该层了解“上下文”(了解更大情况的层),也了解交易的“生命周期”。该层还负责您的事务管理。
You should retrieve the objects from your persistence layer from the layer that has knowledge of the context / use-case.
In other words: in the layer where you've implemented your use-case, you should call the repository (or other persistence-layer objects) that can retrieve (or save) your objects from (or to) the database.
IMHO, that's the only correct thing to do. It is the layer that has knowledge of the 'context' (the layer that knows the bigger picture) that also has knowledge of the 'lifetime' of your transaction. That layer is responsible for your transaction management as well.
如果您的持久层已准备就绪,那么可以使用一些工具从 DB 架构创建类,
其中之一是 NetBeans 持久插件。
现在,您可以将任何 ORM 工具用于域模型 [从数据库架构创建的类] 和实际数据库。
Well if your persistence layer is ready , There are some tools available which will create Classes from DB Schema,
One of them is NetBeans persistence plugin.
Now you can use any ORM tool for Domain Model [classes created from DB Schema] and actual DB.