2

Scenario:

A huge Patient class/entity with around 100 properties. But in the webpage I only need to display the contact information (only around 10 properties) to update. Do/Can I create a ViewModel to do that?

I have only used ViewModel to read data before. Can it be used to update data?


EDIT: Okay, to summarize what I have found so far.

  • Use AutoMapper to map the ViewModel (only contains the properties to be updated) back to the original big EntityClass. An example is:


    [HttpPost]
    public virtual ActionResult Edit(EditUser user)
    {

     var domain = uow.Users.Create();
    domain.Id = user.Id;
    uow.Users.Update(domain);            
    AutoMapper.Mapper.Map<EditUser, Example.Core.Data.User>(user, domain);
    uow.Save();
    return View();
}
  • Create another entity called PatientSummary in the EDMX model, which only contain the necessary properties. Manually do the mapping in the EDMX designer. Then use this reduced entity for the Patient contact update web page.

Personally, I feel there must be a better way to do that. Any comment?

Blaise
  • 21,314
  • 28
  • 108
  • 169

2 Answers2

0

I actually posted a couple of questions a little while back on a related topic - question 1 and more specific question 2.

I wasn't a hundred percent happy with the one answer that I got so far on question 2 but the experts seemed to agree that it is a good idea to use view models. For one it saves you from manually making sure that the "other" 90 properties don't get overwritten in the database by the generated UPDATE statement.

Community
  • 1
  • 1
ralfonso
  • 157
  • 2
  • 10
  • Looks like your posting is not really related. But thank you for the info. – Blaise Feb 10 '12 at 20:27
  • @Blaise: sorry! I just wanted to point out that I was (and still am) confused about the advantages of using view models in the scenario of data coming back to the server in an HTTPPost. In asking my question 2 as linked above I was hoping to get a good explanation in favour of view models that make up for the obvious disadvantage of having to map back to the domain model. Same as you I didn't feel too comfortable to jump to Automapper without further information. I'll just keep watching your question so I can benefit from the answers that you get. – ralfonso Feb 10 '12 at 21:03
0

Yes upon save simply load your object and then use automapper to copy the properties back to your object or manually set them on your EF class.

EF will know which props changed and only send updatea for those changed properties.

Adam Tuliper
  • 29,982
  • 4
  • 53
  • 71
  • I have checked AutoMapper but am still a little hesitant. Is it a widely used, safe and good practice? There gotta be a better way I guess. – Blaise Feb 10 '12 at 20:12
  • automapper rocks and is widely used. It simply copies property values. You could write your own code templates to do something similar, other code generation tools, or just write the copying by hand. – Adam Tuliper Feb 11 '12 at 05:08
  • Adam, I have checked AutoMapper after you and many other posts suggested it. It works great when mapping entity into viewmodel. (from big class to small class). However, when saving the ViewModel, the reverse mapping seems impossible a task. The view model only contains a few properties. Many other properties need to be kept intact. And some of them are in complex entities also. Is AutoMapper able to map from a small class into a big, complicated class? I guess if we can figure that out, we will embrace automapper with no hesitance. – Blaise Feb 13 '12 at 16:13
  • In that case you load your entity and the. Use automapper to map back the other direction or call TryUpdateModel to merge your posted values with the model and then save it. – Adam Tuliper Feb 14 '12 at 06:00
  • I've recently come across ValueInjecter which may be a bit easier to use for this case: http://stackoverflow.com/questions/4663577/automapper-vs-valueinjecter – Adam Tuliper Feb 16 '12 at 04:29