应用服务组合 [英] Application Services Composition
问题描述
我有一个客户实体,该实体具有到城市的推荐人:
I've a Customer entity that has an referente to City:
public class Customer
{
public int CustomerId { get; set; }
public int CityId { get; set;}
}
然后,提供以下应用程序服务:
Then, the following application service:
public class CustomerService
{
private readonly ICustomerRepository customerRepository;
public CustomerService(ICustomerRepository customerRepository)
{
this.customerRepository = customerRepository;
}
public void RegisterCustomer(Customer customer)
{
//Do another things...
customerRepository.Save(customer);
}
}
要注册新客户,服务消费者必须可以访问要放入CityId的城市列表,例如获取要填充组合框的城市列表。
To register a new customer the service consumer would have to have access to a listing of cities to put in CityId, such as get the list of cities to fill a combobox.
因此,有必要提供列表城市操作。
Thus it would be necessary provide a list city operation.
我应该将此操作添加到CustomerService吗?
Should i add this operation to CustomerService?
就像:
public class CustomerService
{
private readonly ICustomerRepository customerRepository;
private readonly ICityRepository cityRepository;
public ServicoCliente(
ICustomerRepository customerRepository,
ICityRepository cityRepository)
{
this.customerRepository= customerRepository;
this.cityRepository= cityRepository;
}
public void RegisterCustomer(Customer customer)
{
customerRepository.Save(customer);
}
public List<City> ListCities()
{
return cityRepository.GetAll();
}
}
或创建新服务:
public class CityService
{
private readonly ICityRepository cityRepository;
public CityService(ICityRepository cityRepository)
{
this.cityRepository= cityRepository;
}
public List<City> ListCities()
{
return cityRepository.GetAll();
}
}
在后一种情况下,消费者应该引用两项服务才能完成一项操作:RegisterCustomer。
In the latter case the consumer should have references two services to be able to complete one operation: RegisterCustomer.
采用哪种方法?
推荐答案
这是一个代码组织问题,与DDD无关。
This is a code organization issue and has little to do with DDD.
公共类客户服务{
公共列表列表城市()
{
return cityRepository.GetAll();
}
public List ListCities() { return cityRepository.GetAll(); }
}
这里有明显的不匹配- -您不希望通过客户服务公共界面方法返回城市。这打破了最不令人惊讶的原则,并会引起很多搜索和开发者的不满。
There's an obvious mismatch here -- you don't expect from a Customer service public interface method to return cities. This breaks the principle of least surprise and will cause a lot of searching and head scratching for generations of developers to come.
专用服务对我来说似乎是个好主意。
A dedicated service seems like a better idea to me.
这篇关于应用服务组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!