在领域驱动设计,这将是DDD的侵犯投入到其他对象的repostiories调用域对象? [英] In domain-driven design, would it be a violation of DDD to put calls to other objects' repostiories in a domain object?

查看:265
本文介绍了在领域驱动设计,这将是DDD的侵犯投入到其他对象的repostiories调用域对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在重构是结束了一个项目的一些代码,我结束了投入大量的业务逻辑到服务类,而不是在域中的对象。在这一点上大部分的域对象都只是数据容器。我决定写在大部分服务对象的业务逻辑,事后重构一切都变成更美好,更可重复使用,并更具可读性的形状。这样,我可以决定哪些代码应放入域对象,哪些代码应该被分拆到自己的新对象,并且应该在服务类中留下什么代码。所以,我有一些代码:

I'm currently refactoring some code on a project that is wrapping up, and I ended up putting a lot of business logic into service classes rather than in the domain objects. At this point most of the domain objects are data containers only. I had decided to write most of the business logic in service objects, and refactor everything afterwards into better, more reuseable, and more readable shapes. That way I could decide what code should be placed into domain objects, and which code should be spun off into new objects of their own, and what code should be left in a service class. So I have some code:

public decimal CaculateBatchTotal(VendorApplicationBatch batch)
{
     IList<VendorApplication> applications = AppRepo.GetByBatchId(batch.Id);

     if (applications == null || applications.Count == 0)
          throw new ArgumentException("There were no applications for this batch, that shouldn't be possible");
     decimal total = 0m;
     foreach (VendorApplication app in applications)
          total += app.Amount;
     return total;
}

这代码似乎将成为一个好除了域对象,因为这是唯一的输入参数是域对象本身。好像一些重构一个完美的候选人。但唯一的问题是,这个对象调用另一个对象的存储库。这让我想将它留在服务类。

This code seems like it would make a good addition to a domain object, because it's only input parameter is the domain object itself. Seems like a perfect candidate for some refactoring. But the only problem is that this object calls another object's repository. Which makes me want to leave it in the service class.

我的问题是这样的:


  1. 会在哪里你把这个代码?

  2. 你会打破这种功能呢?

  3. 会在哪里人谁是遵循严格的领域驱动设计把它?

  4. 为什么?

  1. Where would you put this code?
  2. Would you break this function up?
  3. Where would someone who's following strict Domain-Driven design put it?
  4. Why?

感谢您的时间。

编辑注:不能在这一个使用ORM,所以我不能用一个延迟加载解决方案

Edit Note: Can't use an ORM on this one, so I can't use a lazy loading solution.

编辑注2:我可以。'T改变构造采取的参数,因为可能的数据层如何实例使用反射(不是我的主意)域对象

Edit Note2: I can't alter the constructor to take in parameters, because of how the would-be data layer instantiates the domain objects using reflection (not my idea).

编辑注3:我不相信一批对象应该能够公正共应用程序的任何名单,现在看来似乎应该只能以总价是在那个特定的批处理应用程序。否则,它更有意义,我离开了服务类的功能。

Edit Note3: I don't believe that a batch object should be able to total just any list of applications, it seems like it should only be able to total applications that are in that particular batch. Otherwise, it makes more sense to me to leave the function in the service class.

推荐答案

您甚至不应该访问存储库从域对象。

You shouldn't even have access to the repositories from the domain object.

您可以做的是要么让服务给域对象相应的信息或者在由设置的域对象的委托服务或在构造函数中。

What you can do is either let the service give the domain object the appropriate info or have a delegate in the domain object which is set by a service or in the constructor.

public DomainObject(delegate getApplicationsByBatchID)
{
    ...
}

这篇关于在领域驱动设计,这将是DDD的侵犯投入到其他对象的repostiories调用域对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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