问题描述
在我的应用程序中有几层。
在本主题中,重点是域和基础结构层。
In my application a few layers.In this topic will focus on Domain and Infrastructure layers.
我在域层中具有存储库接口ClientRepositoryInterface。
并且我已经在基础结构层中实现了此接口ClientRepositoryImpl。
I have repository interface ClientRepositoryInterface in Domain layer.And I have implementation of this interface ClientRepositoryImpl in Infrastructure layer.
但是要在对象存在的周期的中间重新构造该对象,我需要工厂(ReconstitutionClientFactory )。
致电工厂将在资源库中。
Eric Evans的书被描述为正常做法。
But to reconstitute the object in the middle of the cycle of its existence I need factory(ReconstitutionClientFactory).Call the factory will be in the repository.The book by Eric Evans is described as a normal practice.
但是该工厂(ReconstitutionClientFactory)应位于何处?在域还是在基础结构层?
But where this factory(ReconstitutionClientFactory) should be located? In Domain or in Infrastructure layer?
我认为在域...
但是!但是随后较低的层将直接调用较高的层!
这是错误的,但是怎么做正确呢?
I think in Domain...BUT! But then the lower layer will directly call a higher layer!This is wrong, but how to do right?
推荐答案
首先,分层方法已经过时了。在交谈图层时,请考虑上下文,谁在谁之上并不重要。
First of all, the layers approach is kinda obsolete. When talking layers think 'context', who's on top of who isn't important.
存储库负责还原一个对象。一家工厂只是创建一个新对象。注意不同的语义。存储库知道如何进行持久性保存/恢复,这取决于存储和访问方法。
The repository is in charge of restoring an object. A factory just creates a new object. Note the different semantics. The repository knows how saving/restoring to/from persistence is done and that depends on the storage and the method of access.
因此,一切都在存储库内部完成,即基础架构。如果序列化了事情,则只需要反序列化即可(这正是文档数据库执行任务的方式)。如果您使用的是ORM或将其存储在表中,则将执行获取数据并重新填充对象所需的所有查询。 ORM是最简单的方法,因为它可以使用反射来填充私有属性。在这种情况下,ORM本身就是工厂。
So, everything is done inside the repository i.e in the Infrastructure. If you serialize things, then you need just to deserialize back (this is how a document db does things anyway). If you're using an ORM or store things in tables then you'll do all the query required to get the data and repopulate the object. An ORM is the easiest way since it can use reflection to populate private properties. In this case the ORM itself is the factory.
还有另一件事,即恢复,尽管从技术上讲可以由域工厂完成,但这并不是工厂的目的因为它打破了层边界。我们希望将所有与持久性相关的内容保留在基础架构中。
One more thing, restoring, while technically can be done by a domain factory, it isn't the factory's purpose to do that because it breaks the layer boundaries. We want to keep everything persistence related in the Infrastructure.
这篇关于DDD资料库和工厂的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!