I have an WPF application where I want to apply MVVM for the presentation layer and DDD for the whole application. I am very confused with how I should apply the architecture. Is it possible? Can you give me some advices as I completely messed up at the moment with the following design try.
I have simply 4 layers:
-
Presentation Layer: where my WPF application lies in. -
Application Layer: where I have my services that's supposed communicate with domain. this is simply an anti-corruption layer betweenPresentationandDomainlayer. -
Domain Layer: Where my aggregates and domain objects lies in. My aggregates does not consumed by services in Domain layer they only exist in order to be able to use the database, it works with Domain objects that's almost the same as entities but has some additional functionality. -
Infrastructure Layer: lowest layer, infrastructure lies in here,IRepositoryetc..
A simple scenario. I want to have a Person object in a database, I want to map it, CRUD the database, check persons' birthday and show the person.
First of all I want to show some data regarding to a person. Then I create the following classes :
-
PersonView: XAML view of the person -
PersonViewModel:ViewModelthat provides functionality for thePersonView.PersonViewbinds to this and thisViewModelprovides values from PersonModel -
PersonModel: This is the model that my PersonViewModel is thightly coupled to. Please observe that this is not a domain model as it lies in Presentation layer and it's not an entity either.
This is enough for the presentation layer. I now want to connect to database to get a person object where I want to get values for my presentation values.
I must create:
-
PersonEntityinDomain Layer: An aggregate root for the entity, used for mappings with the database. It lies inDomainlayer. -
PersoninDomain Layer: This is the domain model, as DDD suggests I shouldn't send around entity objects.
Ok I already have 3 person models that are pretty similiar to each other. How about the repositories and the services?
-
PersonServiceinApplication Layer: My presentation layer needs to convert itsPersonModel(MVVM model) toPerson(domain model) when it wants to communicate with application layer. Application layer then convertsPerson(domain object) it intoPersonEntity(entity object) and does CRUD with the database. It's role becomes anti-corruption layer between the entity and domain object. This service uses also anotherPersonService(see below) in Domain layer to check/apply some business rules. -
PersonServiceinDomain Layer: This layer only works withPersondomain object. It has business-related rules like boolIsTooOld(Person person).
This is a complete chaos. How should I re-structure it and benefit both domain driven design and MVVM patterns?
1. Should I remove models from presentation layer (MVVM models) and use only models from domain layer (DDD models) ? Isn't it violation of MVVM at this point?
2. Should I merge my entity model with my domain model? Isn't it violation of DDD if I do that?
3. Where should my Validation logic lie here?
Those two were my concrete questions but I'm really stuck and I'm really looking forward for any advises for applying both MVVM and domain driven design.
Aucun commentaire:
Enregistrer un commentaire