DDD - 域模型,服务和存储库之间的依赖条件 [英] DDD - Dependecies between domain model, services and repositories

查看:342
本文介绍了DDD - 域模型,服务和存储库之间的依赖条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是想知道其他人如何分层他们的架构。说我有我的层如下:

Just wanted to know how others have layered their architecture. Say i have my layers as follows:

领域层结果
- 产品搜索
--ProductService(如果小鬼进入这一层?)结果
--IProductService结果
--IProductRepository结果

Domain Layer
--Product
--ProductService (Should the imp go into this layer?)
--IProductService
--IProductRepository

基础设施层结果
--ProductRepository(IProductRepository的小鬼在我的域)结果

Infrastructure Layer
--ProductRepository (Imp of IProductRepository in my domain)

现在,当一个新产品,创建我有打电话到ProductService.GetNextProductId()方法来分配产品ID的要求。

Now when a new product is created i have a requirement to assign a product id by calling into the ProductService.GetNextProductId() method.

由于该服务对我成立了ProductService构造函数与可稍后注射IProductRepository的接口库的依赖。是这样的:

Because the service has a dependency on the repository i set up the ProductService ctor with an interface of IProductRepository which can be injected later. something like this:

    public class ProductService : IProductService
    {
        private IProductRepository _repository;

        public ProductService(IProductRepository repository)
        {
            _repository = repository;
        }

        public long GetNextProductId()
        {
            return _repository.GetNextProductId();
        }
    }

我的问题是,当我在产品类别我正在实例化一个新ProductService类时参照在构造函数存储库使用该服务。在DDD的一个很大的不,不能够有这样一个参考。我甚至有些不知道如果我的产品领域一流正在建立正确调用服务,可以有人请提醒:

My issue is that when i use the service in the Product Class i am making reference to the Repository in the ctor when instantiating a new ProductService class. In DDD its a big no no to have such a reference. I' am not even sure if my product domain class is being set up correctly to call the service, can someone pls advise:

public class Product : Entity
    {
        private ProductService _svc;
        private IProductRepository _repository;

        public Product(string name, Address address) //It doesnt seem right to put parm for IProductRepository in the ctor?
            : base(_svc.GetNextProductId) // This is where i pass the id
        {
            // where to create an instance of IProductRepository?
        }
    }

我怎样才能优雅地解决这一设计问题?我愿意接受建议,由经验丰富的DDD'ers

How can i elegantly solve this design issue? I am open to suggestions from experienced DDD'ers

编辑:

感谢您的意见。我也怀疑,如果服务应该从产品类别进行调用。我没有使用工厂模式(还)为对象的建设仍然是简单的。我不觉得它值得一工厂方法了吗?

Thanks for you comments. I also doubted if the service should be called from the Product Class. I have not used a factory pattern (yet) as the construction of the object is still simple. I dont feel it warrants a factory method yet?

我很困惑......把产品编号一边,如果我的产品需要一流的服务如GetSystemDateTime()其他一些数据(我知道,坏的榜样,但试图展示一个非分贝调用)这种服务方法会在哪里叫什么名字?

I' am confused...Putting the ProductId aside if my Product class needed some other data from a Service e.g GetSystemDateTime() (i know, bad example but trying to demonstrate a non db call) where would this service method be called?

在DDD服务的逻辑转储,逻辑不natrual域对象,对不对?因此,它是如何粘合在一起?

Services in DDD are logic dumps where the logic is not natrual to the domain object, right? So How does it glue together?

推荐答案

要你的最后一点,在DDD服务是把我形容为笨拙的逻辑的地方。如果你有一些类型的逻辑或具有其他实体的依赖关系的工作流程,这是通常不适合的域对象本身的内部逻辑的类型。例如:如果我有我的业务对象的方法来执行一些类型的验证,服务类可能执行此方法(仍保持相关实体同级车内的实际验证逻辑)

To your last point, services in DDD are a place to put what I describe as "awkward" logic. If you have some type of logic or work flow that has dependencies on other entities this is the type of logic that usually doesn't "fit" inside a domain object itself. Example: If I have a method on my business object to perform some type of validation, the service class might execute this method (still keeping the actual validation logic related to the entity inside its class)

另外一个真正的好例子,我一直提的就是资金转移法。你不会从一个对象中的帐户对象转移到另一个,而是你会,是以到帐户和从帐户的服务。然后里面的服务,你会调用你的,从账户计提方法和你到帐户的存款方式。如果你试图把这个账户实体本身里面会觉得尴​​尬。

Another really good example I always mention is the funds transfer method. You wouldn't have an account object transfer from one object to another, but instead you would have a service that takes the "to" account and the "from" account. Then inside the service you would invoke the withdrawal method on your "from" account and the deposit method on your "to" account. If you tried to put this inside the account entity itself it would feel awkward.

在这个非常的话题深入交换一个伟大的播客,可以发现<一个href=\"http://deepfriedbytes.com/podcast/episode-6-talking-domain-driven-design-with-david-laribee-part-1/\">here.大卫Laribee做了很好的工作,现在只解释如何,但DDD为什么的了。

A great podcast that talks in depth about this very topic can be found here. David Laribee does a really good job explaining now only the "how" but the "why" of DDD.

这篇关于DDD - 域模型,服务和存储库之间的依赖条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆