我使用DDD。
我有以下接口:
interface ICustomerRepository
{
void Disable(int customerId);
}
interface ICustomerService
{
void Disable(int customerId);
}
该应用程序将在WebService上运行。
我想知道,我应该使用id作为参数还是整个Customer实体?
每种方法的优缺点是什么?
事实上,这种行为不应该出现在存储库中。行为应放置在实体中。
但是,您的应用程序服务契约可能不包含域类。
例如
//Application service (runs in a transaction)
public void Disable(int customerId) {
var customer = this.customerRepository.FindById(customerId);
customer.Disable(); //execute business logic
this.customerRepository.Save(customer); //persist the state
}
虽然plalx提供的答案可能是实现这一点的纯粹方法,但我也发现,在某些情况下,完全保存可能会过于致命。
两者混合如何:
interface ICustomerRepository
{
void SaveDisable(Customer customer);
}
interface ICustomerService
{
void Disable(int customerId);
}
那么代码可能是:
public void Disable(int customerId) {
var customer = _customerRepository.Get(customerId);
customer.Disable();
_customerRepository.SaveDisable(customer);
}
这将需要对附加功能非常小心,因为我们对持久化的内容是明确的。
使用CudtomerId是一个更好的主意。因为当您传递Customer实体(通常我们使用传递值)时,它会复制它并使用它;默认情况下,它将是一个空实体
所以,我认为你的方式是最好的。