实体VS Id作为参数

Vin*_*ves 1 c# performance domain-driven-design

我正在使用DDD.

我有以下接口:

interface ICustomerRepository 
{
    void Disable(int customerId);
}

interface ICustomerService 
{
    void Disable(int customerId);
}
Run Code Online (Sandbox Code Playgroud)

该应用程序将在WebService上运行.

我想知道,我应该使用id作为参数还是整个Customer实体?

每种方法的优缺点是什么?

pla*_*alx 5

嗯,事实是这种行为不应该在存储库中.行为应该放在实体中.

但是,您的应用程序服务合同应该可以免除域类.

例如

//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
}
Run Code Online (Sandbox Code Playgroud)