【发布时间】:2011-11-09 10:50:17
【问题描述】:
我有一个 WCF 服务类 PersonService,它可以将 Person 和他/她的 Address 信息保存到数据库中的 Person 和 Address 表中。
以下是实现的两个版本:
版本 1(存储库版本)
[ServiceContract]
class PersonService
{
private IPersonRepository _personRepository;
//by referencing other **repository**
private IAddressRepository _addressRepository;
private PersonService(IPersonRepository personRepository, IAddressRepository addressRepository)
{
_personRepository = personRepository;
_addressRepository = addressRepository;
}
public void Add(Person person){
_personRepository.Add(person);
//by calling method from the **repository** layer
if(!_addressRepository.Contains(person.Address))
{
_addressRepository.Add(person.Address);
}
}
}
版本 2(服务版本)
[ServiceContract]
class PersonService
{
private IPersonRepository _personRepository;
//by referencing other **service**;
private IAddressService _addressService;
private PersonService(IPersonRepository personRepository, IAddressService addressService)
{
_personRepository = personRepository;
_addressService = addressService;
}
public void Add(Person person)
{
_personRepository.Add(person);
//by calling method from the **service** layer
if (!_addressService.Contains(person.Address))
{
_addressService.Add(person.Address);
}
}
}
其中哪一个是更好的做法?像 PersonService 这样的 Service 类应该与同一层的接口更多地耦合,还是像 Repository 层这样的较低层?为什么?
【问题讨论】:
标签: c# wcf architecture repository