1

While working in ASP.NET MVC, I often find myself defining a basic ViewModel which all properties are but a small subset of the actual Entity model. I then use AutoMapper to transform my objects properly into and out of the Entity model. This works great, and separates my concerns nicely. However, I'm thinking that someone by now has made a tool to make this process easier! All the repetitive typing while creating my ViewModels inevitably leads to typos and some frustration.

What tools, if any, do you use to address this issue?

Thanks!

Edit: I don't mind decorating my properties with the appropriate UI hints, validators, etc. I just hate defining the same propery names again and again!

1 Answer 1

3

You could consider using T4 templating, see this MSDN magazine article to get started. You could create a template that uses reflection to get the properties of your Model, and generate ViewModel from this.

Sign up to request clarification or add additional context in comments.

2 Comments

This is a very good idea! I've messed with the templates for new Views so they fit the structure of my site and included scripts to initialize client-side table sorting, etc. I never thought of leveraging templates in this way. Nice link as well. Thanks!
+1 - i have created a 'bespoke' mvc project template (using T4) that does exactly this - creates viewmodels and uses automapper to create the viewmodels as part of the process, so can't speak highly enough about the benefits of going down this path

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.