我想用这些模式创建一个应用程序,但我不知道我是否正确行事。例如,我想从联系人中删除地址:
ContactController.cs (UI)
public void RemoveAddress(int contactId, int addressId)
{
using (UnitOfWork unitOfWork = new UnitOfWork())
{
ContactService contactService = new ContactService(unitOfWork);
contactService.RemoveAddress(contactId, addressId);
}
}
ContactService.cs (服务层)
public void RemoveAddress(int contactId, int addressId)
{
// Check if users contact
Contact contact = _contactRepository.GetByUser(contactId, _userId);
if (contact == null)
throw new Exception("Contact not exists!");
AddressService addressService = new AddressService(_unitOfWork);
Address address = addressService.GetAddress(addressId);
// Check if contact has the right address
if (contact.Address.Contains(address))
addressService.RemoveAddress(address);
_unitOfWork.SaveChanges();
}
AddressService.cs (服务层)
public Address GetAddress(int addressId)
{
return _addressRepository.GetById(addressId);
}
public void RemoveAddress(Address address)
{
address.Contact = null;
_addressRepository.Remove(address);
}
存储库没有逻辑,它们只传输对象(没有dtos)。我实现了这个权利,我将实体映射到dtos吗?目前我在服务中映射它,但如果我这样做,我将dtos与实体混合。