My observations on data transfer objects
- They should not be a one to one representation of your domain classes.
Sometimes you want a subset of the information of a single instance, sometimes your DTO will collect data from various instances (starting with an aggregate root). You might create different types of DTO from the same domain classes based on what the user needs to do. Sometimes the user only wants OrderNumber + DateRaised + TotalValue (to show as a list of orders), sometimes they want the entire order details (including lines) for editing.
- The domain classes should have no knowledge of your DTO classes.
So you shouldn’t have any methods such as
pubic PersonDto CreateDto();
DTO’s are not part of the business domain, so you should never do this!
- The DTO you send out might not be the same type as you get back.
For example you don’t want the user to edit the order number or the date raised. If there are only a couple of properties like this you might opt to use the same DTO class but just ignore those properties when updating the domain instances, or you might decide on Request/Response DTOs
- The DTOs should have no knowledge of the domain classes.
This is because the DTO classes will be shared between the client and the server, and the client will have no domain class knowledge. If you have domain classes on the client then you probably don’t need DTOs.
- There is only one way to update domain instances from a specific DTO.
A DTO doesn’t always update domain classes, but when you receive a specific kind of DTO and need it to update domain instances it will always update in the same fasion.
So, how do you create DTOs from domain instances and how do you update domain instances from DTOs? The first thing to be aware of is that this code belongs in a layer above the domain layer, in a services layer or the app layer itself. Then we need some way of saying
“I want to convert this Order to an OrderSummaryDto”
“I want to convert this Order to an OrderDto, which includes its order lines”
and then finally
“I have received a DTO, I need to update the relevant domain instances”
We need to do this in a reusable way, because the same DTOs may be reused in various parts of the application layer. To achieve this I used the Dependency Injection Container from Microsoft named “Unity”.
Naming conventions I used are
- xxxDtoFactory – Creates a DTO of a specific type from a specific domain instance.
- xxxDtoTranslater – Takes a DTO of a specific type and translates its values back into the domain instances (updating existing instances, creating new instances, or whatever is required.)
The example model has a class named “Template” which has only a “string Name” property. It is an aggregate root so it owns multiple TemplateProperty instances. TemplateProperty is an abstract class with two concrete descendants; TemplateStringProperty and TemplateBooleanProperty.
When I create a DTO I want a TemplateDto and added to its Properties I want TemplateStringPropertyDto and TemplateBooleanPropertyDto. When I translate TemplateDto I want to update the existing object.
To register a factory
container.RegisterType<IDtoFactory<Template, TemplateDto>, TemplateDtoFactory>(
The structure is IDtoFactory<TDomainClass, TDataTransferObject> to identify the domain class and desired DTO class, followed by the type that implements the interface to perform this DTO creation. This code registers a factory that takes a Template domain class and creates a TemplateDto data transfer object.
To register a translater
The structure is more simple. A DTO can only be translated in one way, so the generic IDtoTranslater interface only requires a single type, TDataTransferObject. This code registers a translater that takes a data transfer object and maps it to the domain.
Once registered the services are used like so:
//Create a template
Template template1 = new Template();
//Create a DTO from the template
TemplateDto template1Dto = container.Resolve<IDtoFactory<Template, TemplateDto>>().CreateDto(template1);
//Update the template from the DTO
container.Resolve<IDtoTranslater<TemplateDto>>().UpdateBusinessObjects(null, null, template1Dto);
Note that the “container.Resolve” code wouldn’t really be there, you would use dependency injection. I have used it in this example to avoid turning it into a dependency-injection-container blog 🙂
The “null, null” here are
- The object space that the app layer has created and is using for your domain instances to work inside. Also known as your unit of work, transaction, or other.
- An addition context. For example when updating a Template the TemplateDtoTranslater will resolve services to translate each PropertyDto it encounters, and pass the Template as the context so that we know which aggregate root we are working with.
For now I will link to the full source code for the example. If I get time I will blog some more explaining how the code works.