DDD and Domain Models with a Web Api PUT / POST
The company I'm working at has strict policies that insist that all business logic is contained in Domain Models as part of DDD. I can see how this approach could work well for something like a desktop app, but I'm having issues with making this fit with building a web app and was hoping someone could suggest some patterns / techniques for making it fit better. Specifically, I'm having issues with PUT/POST APIs. The DTO models received by the API have almost exactly the same properties as the domain models (with just a few missing) - the JSON is automatically deserialized into the DTO models. I then need to load, update and save the domain models. In the past, I've tended to put the domain logic into a service class, which allowed me to load the domain model, map the updated properties across from the DTO using AutoMapper (while leaving the properties that don't exist in the API models alone) and then saving the model again. This approach isn't allowed as everything has to be done in the domain models, doubly so as the company has banned the use of AutoMapper and other mapping tools. When creating objects, I have to call constructors, manually mapping all the API properties to constructor parameters, which I don't see any way round given the restrictions imposed by company patterns as all objects have to be valid at all times - we can't have an intermediate stage while building the object where the object is invalid, i.e. everything has to be created and set in one go. The only approach that I can see for updates is to write code to manually copy each property in the DTO model across to the domain model one at a time, but this is extremely time consuming / tedious / error prone / a maintenance nightmare as some of our models have a few hundred properties across multiple layers of nested classes. It's made worse as we are also banned from using a DTO model with two APIs so they often have copies of identical classes that then need to be maintained as we add / update / remove properties from the domain classes. We often have to keep setters on properties on the domain models private so that it can't get into an invalid state so it means calling methods with many parameters on the domain models that update multiple properties at the same time to keep things valid (I used to handle this by simply validating my DTO models before mapping). Are there any patterns that I can use to help relieve some of this boilerplate hell? If it makes a difference to any answers - I'm developing in C# / ASP.Net 4.7.2 (Framework)
The company I'm working at has strict policies that insist that all business logic is contained in Domain Models as part of DDD.
I can see how this approach could work well for something like a desktop app, but I'm having issues with making this fit with building a web app and was hoping someone could suggest some patterns / techniques for making it fit better.
Specifically, I'm having issues with PUT/POST APIs.
The DTO models received by the API have almost exactly the same properties as the domain models (with just a few missing) - the JSON is automatically deserialized into the DTO models.
I then need to load, update and save the domain models.
In the past, I've tended to put the domain logic into a service class, which allowed me to load the domain model, map the updated properties across from the DTO using AutoMapper (while leaving the properties that don't exist in the API models alone) and then saving the model again.
This approach isn't allowed as everything has to be done in the domain models, doubly so as the company has banned the use of AutoMapper and other mapping tools.
When creating objects, I have to call constructors, manually mapping all the API properties to constructor parameters, which I don't see any way round given the restrictions imposed by company patterns as all objects have to be valid at all times - we can't have an intermediate stage while building the object where the object is invalid, i.e. everything has to be created and set in one go.
The only approach that I can see for updates is to write code to manually copy each property in the DTO model across to the domain model one at a time, but this is extremely time consuming / tedious / error prone / a maintenance nightmare as some of our models have a few hundred properties across multiple layers of nested classes. It's made worse as we are also banned from using a DTO model with two APIs so they often have copies of identical classes that then need to be maintained as we add / update / remove properties from the domain classes.
We often have to keep setters on properties on the domain models private so that it can't get into an invalid state so it means calling methods with many parameters on the domain models that update multiple properties at the same time to keep things valid (I used to handle this by simply validating my DTO models before mapping).
Are there any patterns that I can use to help relieve some of this boilerplate hell?
If it makes a difference to any answers - I'm developing in C# / ASP.Net 4.7.2 (Framework)