Automapper where you put your code to display View Model to Entity

My mvc3 project has the following layers.

controller -> service -> repository.

I need to map the ViewModel to Entity, not sure which layer is right for code input.

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

thanks.

+6
source share
1 answer

I need to map the ViewModel to Entity, not sure which layer is right for code input.

The controller, of course. Service and repository levels do not know what the view model means. They only manipulate domain models.

Thus, inside the controller, you use the .Map<TSource, TDest> to perform the mapping between the domain model and the view models. But the mapping definition itself ( .CreateMap<TSource, TDest> call) is performed once during the lifetime of the AppDomain, ideally in Profile .

So, consider a couple of typical workflows inside a controller action in terms of RESTful

GET (SELECT in terms of RDBMS):

  • Controller
  • requests a service level to retrieve the domain model (in most cases, the common root is used). Controller
  • calls the display layer to map the domain model to the view model. Controller
  • passes view model to view

PUT (INSERT in terms of RDBMS):

  • the controller receives the view model from the view as an argument to the action
  • maps the view model to the domain model
  • transfers the domain model to the service level for processing
  • the controller redirects to the GET action

DELETE (DELETE in terms of RDBMS)

  • the controller receives an identifier as a parameter of the Controller action
  • passes the identifier to the service level for processing (delete)
  • the controller redirects to the GET action

POST (UPDATE in terms of RDBMS):

  • the controller receives the view model from the view as an argument to the action
  • requests the level of service to obtain the domain model that we want to update using the unique identifier contained in the controller view model
  • only updates the properties of the found domain model, which are also present in the view model. For example, a domain model may consist of the Username and IsAdmin properties, and a view model will obviously consist only of the Username property. Therefore, we leave the IsAdmin property on the domain model intact and update the Username property. In AutoMapper terms, this means the following overload of the void .Map<TSource, TDest> Mapper.Map<ADomain, ViewModel>(domainInstanceControllerRetrievedUsingTheId, viewModelInstancePassedAsArgument); : Mapper.Map<ADomain, ViewModel>(domainInstanceControllerRetrievedUsingTheId, viewModelInstancePassedAsArgument); Controller
  • transfers the updated domain model to the service level for processing (updates)
  • the controller redirects to the GET action

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

PS REST Reminder:

 Create = PUT Retrieve = GET Update = POST Delete = DELETE 
+10
source

Source: https://habr.com/ru/post/920631/


All Articles