I'm planning to refactor a legacy .NET project that I'm maintaining, there are mainly 4 projects involved:
My.Company.Web
- The presentation layer. ASP.NET MVC.My.Company.Service
- The service layer, defines service interfaces and contains command handlers. Invoked by the web layer.My.Company.DTO
- The assembly that contains DTO classes for data transfer between the domain and web layers.My.Company.Domain
- The domain layer.My.Company.Data
- The persistence layer.
When user submits a form to update a person's details, the process looks somewhat like this:
- Using the submitted data, controller constructs a
PersonDetailsDTO
which consists of multiple simple fields to holdFirstName
,LastName
,Age
and etc, and a complex type object calledAddressDTO
that holds address details:var dto = new PersonDetails(...);
-
Controller calls the service layer with the DTO:
_personService.UpdateDetails(dto);
-
In the service:
public void UpdatePerson(PersonDetailsDTO dto) { var person = _personRepository.GetById(dto.PersonId); person.Update(dto); _personRepository.Update(person); }
-
In the Person domain:
public class Person : AggregateRoot { private string FirstName; .... // other fields private Address Address; public void Update(PersonDetailsDTO dto) { FirstName = dto.FirstName; .... // other fields Address.Update(dto.AddressDTO); // update the value object - address } }
As you can see here, the Person
domain references the PersonDetailsDTO
. This feels wrong to me as DTOs are means to present the data in the domain to its clients while not exposing the domain itself. and the domain should not be concerned with how it's presented to the clients.
Also IMO the DTOs should live in the My.Company.Service
project as it's the service that defines the operation/data transfer contracts.
But then how would the service pass the details on to the domain? Would it have to be a loooong list of parameters: person.Update(firstName, lastName, age, gender, street1, street2, state, postcode...)
. This certainly looks wrong to me.
Or does the domain have to define yet another set of it's own "DTOs" to receive client data?
I have gone through a LOT of tutorials and SO posts and no one seem to share my concern, so I'm starting to think I'm missing something here, could someone point me to the right direction?
Aucun commentaire:
Enregistrer un commentaire