Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

automapper, where do you put your code to map View Model to Entity

my mvc3 project has following layers.

controller -> service -> repository.

I need to map ViewModel to Entity, not sure which layer is the right one to put the code in.

I know its either controller or service, please let me know which one I should use, and if you could please let me know why.

thank you.

like image 534
qinking126 Avatar asked Jul 16 '12 21:07

qinking126


People also ask

How do I use AutoMapper to list a map?

Once you have your types, and a reference to AutoMapper, you can create a map for the two types. Mapper. CreateMap<Order, OrderDto>(); The type on the left is the source type, and the type on the right is the destination type.

What is AutoMapper in Entity Framework?

AutoMapper is a conventions-based mapping system that allows you to copy values from an instance of one class to an instance of another class using pre-defined maps.

How does AutoMapper work in C#?

AutoMapper in C# is a library used to map data from one object to another. It acts as a mapper between two objects and transforms one object type into another. It converts the input object of one type to the output object of another type until the latter type follows or maintains the conventions of AutoMapper.


1 Answers

I need to map ViewModel to Entity, not sure which layer is the right one to put the code in.

Controller of course. The service and repository layers don't know what a view model means. They manipulate only domain models.

So inside the controller you use the .Map<TSource, TDest> call to do the mapping back and forth between a domain model and a view models. But the mapping definition itself (.CreateMap<TSource, TDest> call) is done once per AppDomain lifetime, ideally in a Profile.

So let's consider a couple of typical workflows within a controller action in RESTful terms

GET (SELECT in RDBMS terms):

  1. controller queries the service layer to retrieve a domain model (an aggregate root in most situations)
  2. controller calls the mapping layer to map the domain model to a view model
  3. controller passes the view model to the view

PUT (INSERT in RDBMS terms):

  1. controller receives a view model from the view as action argument
  2. controller maps the view model to a domain model
  3. controller passes the domain model to the service layer for processing
  4. controller redirects to a GET action

DELETE (DELETE in RDBMS terms)

  1. controller receives an id as action parameter
  2. controller passes the id to the service layer for processing (delete)
  3. controller redirects to a GET action

POST (UPDATE in RDBMS terms):

  1. controller receives a view model from the view as action argument
  2. controller queries the service layer to obtain a domain model that we want to update using the unique Id contained in the view model
  3. controller updates only the properties of the domain model that was retrieved that are also present in the view model. For example the domain model might consist of a Username and IsAdmin properties and the view model will obviously consist only of a Username property. So we leave the IsAdmin property on the domain model untouched and update the Username property. In AutoMapper terms this translates to the following void overload of the .Map<TSource, TDest> method: Mapper.Map<ADomain, ViewModel>(domainInstanceControllerRetrievedUsingTheId, viewModelInstancePassedAsArgument);
  4. controller passes the updated domain model to the service layer for processing (update)
  5. controller redirects to a GET action

Armed with those 4 workflows you are ready for the CRUD world.

P.S. A REST reminder:

Create = PUT
Retrieve = GET
Update = POST
Delete = DELETE
like image 120
Darin Dimitrov Avatar answered Sep 22 '22 03:09

Darin Dimitrov