实体VS ID作为参数 [英] Entity VS Id as Parameter

查看:71
本文介绍了实体VS ID作为参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用DDD。

我有以下界面:

interface ICustomerRepository 
{
    void Disable(int customerId);
}

interface ICustomerService 
{
    void Disable(int customerId);
}

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

The application will work on a WebService.

我想知道,我应该使用id还是整​​个Customer实体?

I wonder, should I use id's as parameter or the entire Customer entity?

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

What are the pros and cons of each approach?

推荐答案

好吧,事实是这种行为不应出现在存储库中。行为应放在实体中。

Well, the fact is that this behavior shouldn't be on the repository. Behavior should be placed in entities.

但是,您的应用程序服务合同可能应免除域类。

However, your application service contract should probably be exempt of domain classes.

eg

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

这篇关于实体VS ID作为参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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