Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to mutate editmodel/postmodel to domain model

In an ASP.NET MVC project we are using AutoMapper to map from domain model to viewmodel - and sometimes also flattening a hierarchy while doing so. This works like a charm and makes the rendering logic of our views very lean and simple.

The confusion starts when we want to go the other way from viewmodel (or postmodel or editmodel) to domain model, especially when updating objects. We can't use automated/two-way mapping because:

  1. we would have to unflat the flattened hierarchy
  2. all properties on the domain model would have to be mutable/have public setters
  3. the changes coming from the view isn't always just flat properties being mapped back to the domain, but sometimes need to call methods like "ChangeManagerForEmployee()" or similar.

This is also described in Jimmy Bogards article: The case for two-way mapping in AutoMapper, but the solution to this isn't described in detail, only that they go:

From EditModel to CommandMessages – going from the loosely-typed EditModel to strongly-typed, broken out messages. A single EditModel might generate a half-dozen messages.

In a similar SO question there is an answer by Mark Seeman where he mentions that

We use abstract mappers and services to map a PostModel to a Domain Object

but the details - the conceptual and technical implementation - is left out.

Our idea right now is to:

  1. Recieve a FormCollection in the controller's action method
  2. Get the original domain model and flatten it to viewModelOriginal and viewModelUpdated
  3. Merging the FormCollection into viewModelUpdated using UpdateModel()
  4. Use some generic helper method to compare viewModelOriginal with viewModelUpdated
  5. Either A) Generate CommandMessages a la Jimmy Bogard or B) Mutate the differences directly into the domain model through properties and methods (maybe mapping the 1-1 properties directly through AutoMapper)

Can someone provide some examples of how they come from FormCollection through editmodel/postmodel to domain model? "CommandMessages" or "abstract mappers and services"?

like image 964
Jakob Engelbrecht Olesen Avatar asked Jul 03 '12 15:07

Jakob Engelbrecht Olesen


2 Answers

I use the following pattern:

[HttpPost]
public ActionResult Update(UpdateProductViewModel viewModel)
{
    // fetch the domain model that we want to update
    Product product = repository.Get(viewModel.Id);

    // Use AutoMapper to update only the properties of this domain model
    // that are also part of the view model and leave the other properties unchanged
    AutoMapper.Map<UpdateProductViewModel, Product>(viewModel, product);

    // Pass the domain model with updated properties to the DAL
    repository.Update(product);

    return RedirectToAction("Success");
}
like image 79
Darin Dimitrov Avatar answered Nov 04 '22 13:11

Darin Dimitrov


You might want to consider CQRS(Command Query Responsibility Segregation - I think this might be the concept you were missing), possibly even with Event Sourcing.

It is basically a practice of separating the logic of reading from a data source and writing to a data source, might even mean having different data models for reading and writing.

This might be a good place to start: http://abdullin.com/cqrs/

like image 26
Paweł Sokołowski Avatar answered Nov 04 '22 15:11

Paweł Sokołowski