views:

54

answers:

1

I'm using AutoMapper to map between a Linq Domain object and a ViewModel to display an Edit Form to the user which works perfectly.

When they click submit I'd like to know the best way to map the ViewModel back to a Linq entity and persist it to the database.

The Linq entity I'm using has multiple collections of other entities (ie one-to-many references).

I was trying:

  1. build up my custom view model using UpdateModel
  2. get the previous state of the Linq entity by using the passed in id
  3. map the view model onto the Linq entity (using automapper)
  4. call SubmitChanges() on the data context

This method works when I'm only updating properties that are not collections, but throws errors when trying to modify properties that are collections.

Any help/thoughts would be much appreciated :)

A: 

I've taken an approach that is very similar to that used by Jimmy Bogard in the CodeCampServer project (http://codecampserver.codeplex.com/)

I have a general Mapper class that I inherit from where I just need to override a MapToModel method that maps from the ViewModel to the domain Model, and a GetIdFromViewModel method that returns the proper Id form the ViewModel so that the Service layer can grab the domain model from the database.

I had to change a little from the CodeCampServer examples because some of my Models used Guid and some used int as the Id for the Model.

You should be able to grab the code from the codeplex link above and that should help get you going in that direction.

Here is what one of my Mappers for a Member looks like:

public class MemberMapper : AutoFormMapper<Member, MemberFormViewModel, Guid>, IMemberMapper
{
    public MemberMapper(IMemberService service) : base(service) { }

    protected override Guid GetIdFromViewModel(MemberFormViewModel viewModel)
    {
        return viewModel.MemberId;
    }

    protected override void MapToModel(MemberFormViewModel viewModel, Member model)
    {
        // if the need arises, we will need to map the full objects as Foreign Key properties
        //  by using the proper repositories
        //  right now for loading the object to save back to the DB we don't have that need, so let's not waste the call

        model.MemberId = viewModel.MemberId;
        model.FirstName = viewModel.FirstName;
        model.LastName = viewModel.LastName;
        model.Title = viewModel.Title;
        model.EmailAddress = viewModel.EmailAddress;
        model.DirectPhone = viewModel.DirectPhone;
        model.MobilePhone = viewModel.MobilePhone;
        model.ElectronicId = viewModel.ElectronicId;
        model.ProjectRoleTypeId = viewModel.ProjectRoleTypeId;
        model.DepartmentId = viewModel.DepartmentId;
    }
}

Then you can use this MemberMapper to map both directions. It uses AutoMapper to go from the domain Model to the View Model and then uses the MapToModel method that you implement to map from the View Model back to the domain Model.

Jeff T
Thanks, I took a closer look at http://codecampserver.codeplex.com/there's a lot than can be learned from there.I went with something similar to what you suggested, thanks for pointing me in the right direction.
Chris