3

I am developing a new MVC4 project using Entity Framework for the first time. I really like being able to use code first models and update the database with migrations. I would like to be able to just have one place to change my model (the entity class) and for changes to this such as new properties to be reflected not only in the DB after a migration, but also in my view models.

So, what I would like to do, is to be able to generate a dynamic view model class using my entity class. The view model should copy all the properties and values from my entity class with some special logic defined in my entity class attributes.

For example, for a simple enity framework model like this:

public class UsersContext : DbContext
{
      public UsersContext()
          : base("DefaultConnection")
      {
      }

      public DbSet<UserProfile> UserProfiles { get; set; }

      [Table("UserProfile")]
      public class UserProfile
      {
          [Key]
          [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
          public int UserId { get; set; }
          public string FirstName { get; set; }
          public string LastName { get; set; }
       }
}

I would like to generate a dynamic class that looke like this:

public class UserProfileView
{
      [ScaffoldColumn(false)]
      public int UserId { get; set; }

      public string FirstName { get; set; }

      public string LastName { get; set; }
}

The pseudo code could look something like this but I don't know how I can achieve it:

function dynamic GeneraveViewModel(object entity)
{
    Type objectType = entity.GetType();
    dynamic viewModel = new System.Dynamic.ExpandoObject();

    //loop through the entity properties
    foreach (PropertyInfo propertyInfo in objectType.GetProperties())
    {
         //somehow assign the dynamic properties and values of the viewModel using the property info.

         //DO some additional stuff based on the attributes (e.g. if the entity property was [Key] make it [ScaffoldColumn(false)] in the viewModel.
     }

     return viewModel;
}

Can anyone offer any advice?

6
  • 1
    The main point of ViewModels is that they are not duplicates of the Model entities... This seems kind of self defeating. Commented Aug 12, 2013 at 6:51
  • I would like to do more complicated things with attributes so that it would not be a duplicate, but would mean I could control everything from one file. Commented Aug 12, 2013 at 6:52
  • Did you consider the usage of T4? Commented Aug 12, 2013 at 6:57
  • I had never even heard of it but it looks promising (it's T4 Text Templates for any future readers like me that didn't know). Commented Aug 12, 2013 at 7:04
  • 2
    Using T4 you can create partial classes for the ViewModel, add your additional logic in a separate file and maybe use AutoMapper to map the object types Commented Aug 12, 2013 at 8:47

1 Answer 1

2

I created Dynamic MVC to do this.

http://dynamicmvc.com

Install-package dynamicmvc

The main benefit to this approach is it allows you to focus on your business logic and have the UI logic built automatically for simple scenarios.

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

Comments

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.