Domain versus DTO and ViewModel - how and when to use them?

In a multi-level project with a domain level level (DL) / Business (Service) Layer (BL) / Layer Layer (PL), the best approach for delivering objects to the presentation level?

DO => Domain Object; DTO = Domain Transfer Object; VM => View Model; V => View; 

Option 1:

 DL => DO => BL => DTO => PL => VM => V 

This option seems to be best practice, but also seems hard to work with.

Option 2:

 DL => DO => BL => DTO => PL => V 

This option does not seem to be very good practice, but since the DTO is almost identical to the virtual machine, we can pass it directly to the view, and this is less painful for implementation and maintenance.

Is this option reliable for several layouts, for example, for mobile devices, I may need less information from BL, so for this particular layout I need another virtual machine?

+23
asp.net-mvc entity-framework viewmodel dto automapper
Oct 13 '12 at 15:03
source share
4 answers

OK to pass the DTO to the view. If you need to modify or improve the DTO, then create a ViewModel. A common scenario is to add links. It is also good that ViewModel refers to the DTO as a complex property.

+8
Oct 13
source share

If you have different views that require different data from your D, it sounds like you can use different viewing models for them and map them to Dto.

One of the ideas behind this is to allow more freedom to change your presentation model, knowing that it will not affect any other part of your application. If your Dto is used in multiple views, each change to your Dto will require you to check each kind.

+1
Oct 13
source share

See my answer here: stack overflow

You say: This option seems to be best practice, but also seems hard to work with.

hard to implement, perhaps juste a few lines of code to duplicate most of the time, but certainly not support.

0
Dec 27 '12 at 18:24
source share

For me, this solution is based on where I put my validation logic.

Scenario # 1: Adding data annotation in view mode (in the user interface layer) greatly simplifies programming. Basically it will be displayed one on one between the DTO and the viewing model. In such cases, option 1 is good. DL => DO => BL => DTO => PL => VM => V

Scenario # 2) If the check is not tied to ViewModels, then the DTO is replaced by the ViewModel, and the ViewModel is in the business layer. DL => DO => BL => VM => PL => V

Scenario # 2 may be ideal in situations where the validation logic is in domain models. And these models are used by many user interface applications. The user interface simply lists the errors in the list that are specified by the business layer (although not very user-friendly). Since the application is undergoing business rule changes, we only change the domain model. Again, database-related checks can be generated automatically via EF (when using .net), which is even less room for change.

0
Dec 05 '13 at 21:32
source share



All Articles