Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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.

like image 622
Code Jammr Avatar asked Nov 04 '22 23:11

Code Jammr


1 Answers

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.
like image 70
TJB Avatar answered Nov 10 '22 15:11

TJB