I've been studying DDD and seen a lot of code to be able to build a new api in my current job.
Let's assume the following layered architecture:
The Application Service uses Automapper to create a Domain Model Object, and passes it to Domain Service Layer. Is that correct?
If I already have a Domain Model Object ready to use there, why should I use a Factory? Would I be wrong ignoring factories at all?
Since I use Automapper to map view model objects to domain model objects , where does the factory appears? I have a feeling that i'm missing something big here.
The Application Service uses Automapper to create a Domain Model Object, and passes it to Domain Service Layer. Is that correct?
No. That's how you write a CRUD system. The domain entity should protect it's own state and all modifications on the domain entity should be made through methods.
Something like:
var user = repos.Get(userId);
user.ActivateAccount();
repos.Update(user);
Thus the application services should be modeled around actions and not to just pass along DTOs which look exactly like the domain entities.
using your example, how would I pass a new user? Should I use AutoMapper to map to a DTO User, and in my Domain Layer call a factory
I would create am user DTO which would contain a subset of the information in the domain entity user, just the information which is required to successfully create a user.
In the application service you can use a factory to create an entity and then fill it with the information from the DTO. I personally would create an entity without a factory, but provide mandatory information in the user constructor.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With