DTO = ViewModel?

前端 未结 8 563
别跟我提以往
别跟我提以往 2020-12-04 07:11

I\'m using NHibernate to persist my domain objects. To keep things simple I\'m using an ASP.NET MVC project as both my presentation layer, and my service layer.

I wa

相关标签:
8条回答
  • 2020-12-04 07:47

    First, the major difference is that ViewModel can have behaviour or methods that DTO Must Not !!!

    Second, Using DTO as a ViewModel in ASP.NET MVC make your application tightly coupled to DTO and that's exactly the opposite purpose of using DTO. If you do so, what's the difference using your domain Model or DTO, more complexity to get an anti-pattern ?

    Also ViewModel in ASP.NET can use DataAnnotations for validation.

    The same DTO can have different ViewModels Mapping, and One ViewModel can be composed from differents DTO (always with object mapping not composition) . because i think it is even worse if you have a ViewModel that contains a DTO, we will have the same problem.

    From your presentation layer, think about DTO as a contract, you will receive an object that you have to consider as stranger to your application and don't have any control on it (even if you have ex the service, the dto and presentation layers are yours).

    Finally if you do this clean separation, developpers can work together with ease. The person who design ViewModels, Views and Controllers don't have to worry about the service layer or the DTO implementation because he will make the mapping when the others developpers finish their implementation... He can even use Mocking tool or manual mocking to fill the presentation layer with data for test.

    0 讨论(0)
  • 2020-12-04 07:50

    The canonical definition of a DTO is the data shape of an object without any behavior.

    ViewModels are the model of the view. ViewModels typically are full or partial data from one or more objects (or DTOs) plus any additional members specific to the view's behavior (methods that can be executed by the view, properties to indicate how toggle view elements etc...). You can look at the viewmodel as all the data for a view plus behaviors. ViewModels may or may not map one to one to business objects or DTOs.

    By the way, NHibernate projections come in handy if a certain viewmodel needs a subset of the data from a persisted object.

    0 讨论(0)
提交回复
热议问题