8

我目前正在重构一个即将结束的项目的一些代码,我最终将大量业务逻辑放入服务类而不是域对象中。此时,大多数域对象只是数据容器。我决定在服务对象中编写大部分业务逻辑,然后将所有内容重构为更好、更可重用和更易读的形状。这样我就可以决定哪些代码应该放在域对象中,哪些代码应该分拆到它们自己的新对象中,以及哪些代码应该留在服务类中。所以我有一些代码:

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;
}

这段代码似乎可以很好地添加到域对象中,因为它的唯一输入参数是域对象本身。似乎是一些重构的完美候选者。但唯一的问题是这个对象调用了另一个对象的存储库。这让我想把它留在服务课上。

我的问题是:

  1. 你会把这段代码放在哪里?
  2. 你会打破这个功能吗?
  3. 遵循严格的领域驱动设计的人会把它放在哪里?
  4. 为什么?

谢谢你的时间。

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

编辑注2:我不能改变构造函数来接受参数,因为可能的数据层如何使用反射来实例化域对象(不是我的想法)。

编辑注3:我不认为批处理对象应该能够汇总任何应用程序列表,似乎它应该只能汇总该特定批次中的应用程序。否则,将函数留在服务类中对我来说更有意义。

4

5 回答 5

5

我不是 DDD 方面的专家,但我记得伟大的 Jeremy Miller 的一篇文章为我回答了这个问题。您通常需要与域对象相关的逻辑 - 在这些对象中,但您的服务类将执行包含此逻辑的方法。这帮助我将特定领域的逻辑推送到实体类中,并使我的服务类不那么笨重(因为我发现自己像你提到的那样在服务类中放置了很多逻辑)

编辑:示例

我使用企业库进行简单的验证,所以在实体类中我将设置一个属性,如下所示:

 [StringLengthValidator(1, 100)]
 public string Username {
     get { return mUsername; }
     set { mUsername = value; }
 }

该实体继承自具有以下“IsValid”方法的基类,该方法将确保每个对象都满足验证条件

     public bool IsValid()
     {
         mResults = new ValidationResults();
         Validate(mResults);

         return mResults.IsValid();
     }

     [SelfValidation()]
     public virtual void Validate(ValidationResults results)
     {
         if (!object.ReferenceEquals(this.GetType(), typeof(BusinessBase<T>))) {
             Validator validator = ValidationFactory.CreateValidator(this.GetType());
             results.AddAllResults(validator.Validate(this));
         }
         //before we return the bool value, if we have any validation results map them into the
         //broken rules property so the parent class can display them to the end user
         if (!results.IsValid()) {
             mBrokenRules = new List<BrokenRule>();
             foreach (Microsoft.Practices.EnterpriseLibrary.Validation.ValidationResult result in results) {
                 mRule = new BrokenRule();
                 mRule.Message = result.Message;
                 mRule.PropertyName = result.Key.ToString();
                 mBrokenRules.Add(mRule);
             }
         }
     }

接下来我们需要在服务类的保存方法中执行这个“IsValid”方法,像这样:

 public void SaveUser(User UserObject)
 {
     if (UserObject.IsValid()) {
         mRepository.SaveUser(UserObject);
     }
 }

一个更复杂的例子可能是银行账户。存款逻辑将存在于帐户对象中,但服务类将调用此方法。

于 2009-02-27T19:48:49.237 回答
5

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

您可以做的是让服务为域对象提供适当的信息,或者在由服务或构造函数设置的域对象中拥有一个委托。

public DomainObject(delegate getApplicationsByBatchID)
{
    ...
}
于 2009-02-27T20:00:23.903 回答
2

为什么不传入 IList<VendorApplication>作为参数而不是 VendorApplicationBatch?对此的调用代码可能来自可以访问 AppRepo 的服务。这样一来,您的存储库访问权限将在它所属的地方进行,而您的域功能可以保持对数据的来源一无所知。

于 2009-02-27T19:45:34.143 回答
1

据我了解(没有足够的信息知道这是否是正确的设计) VendorApplicationBatch 应该在域对象内包含一个延迟加载的 IList,并且逻辑应该保留在域中。

例如(航空代码):

public class VendorApplicationBatch  {

    private IList<VendorApplication> Applications {get; set;};   

    public decimal CaculateBatchTotal()
    {
        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;
    }
}

使用像 NHibernate 这样的 ORM 很容易做到这一点,我认为这将是最好的解决方案。

于 2009-02-27T19:58:30.540 回答
0

在我看来,您的 CalculateTotal 是 VendorApplication 集合的服务,并且为 Batch 返回 VendorApplication 集合自然适合作为 Batch 类的属性。因此,其他一些服务/控制器/任何会从批处理中检索适当的 VendorApplication 集合并将它们传递给 VendorApplicationTotalCalculator 服务(或类似的东西)。但这可能会破坏一些 DDD 聚合根服务规则或一些我不知道的东西(DDD 新手)。

于 2009-12-22T23:43:40.327 回答