Mohsen
Mohsen

Reputation: 4266

DDD: (Domain And ViewModel)=> common Behaviors

I'm new to DDD and I don't know what to do with common behaviors of domain which should be done in ViewModel .

say I have an Order.Ship() which should be called during data entry. It seems that this operation should be done by my ViewModel (during data entry).

I'm wrapping the DTO by a ViewModel And each DTO got from an ApiController , should I place the Ship() inside the Order Entity or ViewModel ?

What is the usage of Domain Behaviors here while I'm just moving state to the ViewModel ?

Am I in a right road?

Upvotes: 2

Views: 1677

Answers (1)

Tomasz Jaskuλa
Tomasz Jaskuλa

Reputation: 16013

I'm new to DDD and I don't know what to do with common behaviors of domain which should be done in ViewModel .

No, Domain behaviour should stay in the domain and should never leak out to other layers. Especially to the UI.

say I have an Order.Ship() which should be done while data entry. It seems this operation should be done by my ViewModel (while data entry).

ViewModel is just a DTO and should not have other logic than just formatting the data for the view, validating it, etc. So no, no domain logic should be invoked inside your ViewModel. You should have instead a kind of Application services layer (it's not mandatory but often adds cleaner separation of responsibilities) which sits between the UI and the domain layer. This application layer could take Cross cutting concerns responsibilities like Logging, Tranasactions, etc. Generally this application layer is made of business uses cases like "Shipping Order" in which you can then invoke you business logic Order.Ship(). If you don't have this application layer, so it's the role of ApiControllers take care of that responsibility.

I'm wrapping the DTO by a ViewModel And each DTO got from an ApiController , should I place the Ship() inside the Order Entity or ViewModel ?

If the Ship() method is a domain logic it should be on the Domain entity or domain service.

What is the usage of Domain Behaviors here while I'm just moving state to the ViewModel ?

I think that you mean moving the state to ViewModel by querying the state of the domain. Even if I don't think it's a good idea to query the domain model which is mostly designed for "commands" and not for queries. However if you do so, it's the responsibility of the ApiController (or application service if you have one) to "transfrom" the state of the domain to the ViewModels (DTO).

Upvotes: 3

Related Questions