views:

246

answers:

3

Hi there, I have a load of ADO.NET Entities in my MVC project. I was going to use these entities directly from my views/controllers... however, I think it's probably best to use ViewModels which more accurately reflect what the View requires.

I'm looking for a way that I can auto-generate a ViewModel from an existing Entity, i.e., auto-generate the wrapper or adapter pattern from an existing member... Or a T4 template that would loop through the public properties of an Entity, and output properties for the ViewModel... then I can delete the properties I don't need or create aggregate view models etc.

I cannot seem to find anywhere a way to auto-gen a wrapper or adapter pattern class from an existing type?

The idea is then at runtime, use AutoMapper to map between the ViewModel and the Entity.

thanks

A: 

You could use AutoMapper to convert from your domain model to a view model. There's a great post from Jimmy Bogard explaining how you could integrate this within your controller actions.

Darin Dimitrov
Hi there, sure - AutoMapper is great for that... but I'd like to find a way to generate the ViewModel from entities. AutoMapper solves the problem of the tedium of mapping properties from viewmodels to domain models, but I'd like to cut out the tedium of duplicating manually 100s of properties into viewmodels.
Boomerangertanger
@boom DRY http://en.wikipedia.org/wiki/Don%27t_repeat_yourself
mxmissile
A: 

Or you could use ValueInjecter where you don't need to create maps for each individual pair of types

Omu
A: 

http://weblogs.asp.net/rajbk/archive/2010/05/04/a-basic-t4-template-for-generating-model-metadata-in-asp-net-mvc2.aspx

That can help. It is actually for metadata generation for existing entity types. But you can use it to generate clean view models with data annotations as well. Maybe with a little modification.

AhmetC