One tool that I’ve found particularly useful in an MVC project is AutoMapper.
“A convention-based object-object mapper. 100% organic and gluten-free. Takes out all of the fuss of mapping one object to another.” AutoMapper
In my experience, in almost every get action method, a view model representation of the domain object needs to be constructed to be displayed in the view. With AutoMapper this is done automatically for you.
With AutoMapper our action method in the 2nd code snippet looks so much simpler and neater!
- Thin controller methods
- Configuration fully unit testable
- Reusable mappings (don't need to populate view models every time where needed)
- Null substitution
- Mapping inheritance
AutoMapper is very robust with the most complex mappings achievable where custom classes can be specified in your mapping as converters or resolvers. Wire this all up with dependency injection and you've gotta a mean combination.
Here's the sample project, enjoy!