ViewModel Do's and Don'ts
I am now at the fun part of my journey in building an MVC application. I have spent the last 3 weeks researching architecture, ONION specifically, and learning about开发者_StackOverflow社区 IOC/DI and such.
So my question is this: What is the best way to implement ViewModels? I have seen some terrible examples so far.
I recommend reviewing this article which outlines different 'tactics' for handling view models. http://blogs.msdn.com/b/simonince/archive/2010/01/26/view-models-in-asp-net-mvc.aspx
Some recommendations I can give you for view models is:
- Base them directly on your view & what the user interface needs,
- Prefer creating custom view models for seperate pages instead of generalizing them to be re-used across different views.
- Keep them simple & flat, don't go overboard with inheritance etc.
- If you are mapping from database models, adopt an existing method for mapping between your models and view models such as AutoMapper
- Consider using dynamic in some cases, its more flexible and can have less friction.
精彩评论