How to avoid needing a VIewModel for every Model

后端 未结 3 914
不思量自难忘°
不思量自难忘° 2021-01-16 11:36

I\'m using ASP.NET 4 and MVC3.

Often, I find that I need a ViewModel to display information for my Model. For example, take the following model

class         


        
3条回答
  •  别那么骄傲
    2021-01-16 12:26

    I usually have multiple ViewModels per model - the tradeoff you have to make comes down to this:

    1. Are you comfortable coupling business logic (data annotations, display information, etc...) with your (persistence) models?
    2. Are you comfortable doing all of the hide / display business logic purely within the View and not use the Controller + scaffolding to make those decisions for you?

    The downside of creating all of those ViewModels of course is sub-class explosion, but the right way to think about it is in terms of the questions I listed IMHO.

提交回复
热议问题