DTO = ViewModel?

前端 未结 8 562
别跟我提以往
别跟我提以往 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:26

    If you will use DTO as ViewModel, that means you are making high dependency on DTO because of some reason you are changing DTO then it could impact on ViewModel.

    Better use DTO & convert into viewmodel.

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

    ViewModel in ASP.NET MVC practice is the same as the DTO, however ViewModel in MVVM pattern is different from DTO because ViewModel in MVVM has behaviors but DTO does not have.

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

    DTO - Data Transfer Objects are exactly as it says, containers for transferring data. They have no behaviour but merely a bunch of setters and getters. Some people make them immutable and just create new ones when needed rather than updating existing ones. They should be serializable to allow transfer across the wire.

    Generally DTOs are used to ship data from one layer to another layer across process boundries as calls to a remote service can be expensive so all the required data is pushed into a DTO and transferred to the client in one chunk (coarse grained).

    However, some people use the notion of screen bound DTOs (nothing to do with crossing process boundries). Again these are populated with the required data (generally the data required for a particular screen and could be an aggregation of data from various sources) and sent to the client.

    http://blog.jpboodhoo.com/CommentView,guid,21fe23e7-e42c-48d8-8871-86e65bcc9a50.aspx

    In simple cases as has already been stated this DTO can be used for binding to the view but in more complex cases it would require the creation of a ViewModel and unloading of data from DTO to ViewModel which is obviously more work (when applying MVVM pattern).

    So again as already stated DTO!=ViewModel

    and

    DTO and ViewModel have different purposes in life

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

    DTO != ViewModel

    In the MVVM pattern the ViewModel is used to isolate the Model from the View. To represent the Model you could use simple DTO classes, which again is mapped to a database through e.g. NHibernate. But I've never seen a ViewModel class which is modelled as a DTO.. ViewModel classes mostly have behavior, which DTOs don't have.

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

    We can use DTO same as Model class and we can use viewmodel when we needs to show/use multiple models data/property in a single view. Example: I create some model using entity framework database first. So, now all the model generate based on the database. and now we need data annotation, for those data annotation we can create a folder name DTO, In this DTO folder, we can keep all the models exact which already generate and add data annotation above the property. Then we can use any operation(use controller , views) using this DTO classes. And when we need complex view, I mean when we need multiple classes data in one view there we can use viewmodel. For viewmodel we can create a folder name viewmodel, then create a custom class and keep that property which we need. I tried to clear myself. Any suggestion highly appreciated.

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

    For some simple views I'll use my DTO as my models, but as Views become more complex I'll create ViewModels.

    For me it is a balance between quickness (using DTO, since I already have 'em) and flexibility (creating ViewModels means more separation of concerns).

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