Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DDD, AutoMapper and Factories

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:

  1. Api
  2. Application Services (ViewModel is here)
  3. Domain (Domain Service and Domain Model are here)
  4. Infra

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.

like image 621
Felipe Avatar asked Dec 25 '22 05:12

Felipe


1 Answers

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.

like image 155
jgauffin Avatar answered Jan 05 '23 17:01

jgauffin