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);
}
}
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.
Should i add this operation to CustomerService?
Like:
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();
}
}
Or create a new Service:
public class CityService
{
private readonly ICityRepository cityRepository;
public CityService(ICityRepository cityRepository)
{
this.cityRepository= cityRepository;
}
public List<City> ListCities()
{
return cityRepository.GetAll();
}
}
In the latter case the consumer should have references two services to be able to complete one operation: RegisterCustomer.
Which approach to follow? What are the advantages and disadvantages of this?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…