【发布时间】:2012-06-08 02:45:52
【问题描述】:
致领域驱动开发专家...
我正在尝试真正掌握 DDD 的概念。到目前为止,我一直在将我的模型设计为数据驱动而不是域驱动。我已经阅读了几篇关于 DDD 的文章,看起来非常有趣,尤其是对于大型应用程序。所以我正在尝试定制我的模型来做到这一点。
我说过一个客户实体公开了一个 FreezeAccounts 方法,该方法将禁用所有客户帐户。此方法与数据访问无关(基于 Persistence Ignorance 规则)。它更新每个客户帐户上的标志(按需加载)并将更改保存到数据库。基于这个模型是正确的吗?我已经读过,在 DDD 中,每个表实体不一定只有一个类。这个功能应该在一个单独的类中吗?下面是一些示例代码:
public class Customer : ICustomer, ICustomerAction
{
#region Initialization
public Customer()
{
}
internal Customer(ICustomer view)
{
this.CustomerId = view.CustomerId;
this.Name = view.Name;
this.Email = view.Email;
this.IsActive = view.IsActive;
}
#endregion
#region Instances
private AccountCollection _accounts;
#endregion
#region Properties
#region ICustomer
public int CustomerId { get; private set; }
public string Name { get; set; }
public string Email { get; set; }
#endregion
#region Derived
public AccountCollection Accounts
{
get
{
if (_accounts == null)
_accounts = Account.GetListByCustomerId(CustomerId);
return _accounts;
}
}
#endregion
#endregion
#region Methods
#region CRUD
// Data Access Object accepts interface ICustomer
internal void Update()
{
CustomerDb.Update(this);
}
#endregion
#region ICustomerAction
// Exposed business Persistence Ignorance action
internal void FreezeAccounts()
{
foreach (Account account in this.Accounts)
{
account.IsEnabled = false;
account.Update();
}
}
#endregion
#endregion
}
【问题讨论】:
-
现在一切都清楚了。我的例子是遵循传统的 Active Record 模式,而不是 DDD。业务对象与存储库交互,而不是让应用程序服务与存储库和域(服务/实体)交互。关键词:工作单元、实体根和服务帮助清晰了画面。谢谢!
-
那你为什么不接受正确的答案呢? :-)
标签: architecture domain-driven-design