ASP.NET MVC: ViewModels versus Domain Entities

后端 未结 6 422
太阳男子
太阳男子 2020-12-29 12:04

I\'m building a concept application with MVC 3 in an attempt to learn its ways. I\'ve previously done some very heavy-duty applications in WebForms, using an n-tier approach

相关标签:
6条回答
  • 2020-12-29 12:33

    I think the best approach is to use view models ALWAYS. These are about presentation concerns and should be where basic input validation is handled. Domain objects are not appropriate for this.

    I use specific view models per view and only include the information that is needed in the view - keeping view models totally view-centric makes for nice clean views.

    You can use Automapper to help remove the drudgery of moving between view and domain models.

    Can I recommend ASP.NET MVC 2 in Action as a great book for strong ASP.NET MVC patterns. This covers using Automapper in detail.

    0 讨论(0)
  • 2020-12-29 12:34

    Do you have any advice for where to draw the line between domain objects and mere ViewModels?

    Personally I always use View Models. All UI validation logic is done on the view models (required fields, ...) and business logic on the domain models (username already exists, ...). I also use AutoMapper in order to not get tired of mapping between the domain models and the view models that are passed to the view.

    0 讨论(0)
  • 2020-12-29 12:37

    Domain models and ViewModels will look very much like each other. However, ViewModels usually contain view logic attributes and properties. Also sometimes data type can be different, for example you might need to define a DateTime property as string just get the validation working without raising any errors.

    I am using AutoMapper to convert from Model to ViewModels/vice versa.

    0 讨论(0)
  • 2020-12-29 12:55

    I generally default to using View Models, though for read-only views, I have been known to use the domain model (no reason to go through the overhead of mapping if I am only going to read data out of it).

    If you do decide to use domain models, I would never let MVC bind directly to them, because if someone knows your domain well enough, they can post values that bind to properties you do not want the user to be able to edit. You can define white and black list of properties of what model binder can and cannot bind to, but utilizing that is something else you'll have to maintain and something that can easily be forgotten about.

    0 讨论(0)
  • 2020-12-29 12:57

    My approach is that a ViewModel model should be associated to a single view (or least a set of related views), and is usually a sub-set of your domain model. I see a ViewModel as responsible for UI validation, where as your domain object is responsible for business rule validation.

    As for mapping between the two, highly recommend using Automapper which can automatically map properties based on conventions, it's a huge time saver.

    0 讨论(0)
  • 2020-12-29 12:58

    ViewModel are good but don't forget Validation attributes are not only limited to MVC projects, you can use them in any .net project. Because of this it can make sense to apply the validation attributes to your domain objects, preferably by using a partial class and/or a Validator class http://weblogs.asp.net/scottgu/archive/2010/12/10/class-level-model-validation-with-ef-code-first-and-asp-net-mvc-3.aspx

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