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.
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 aProfile.So let’s consider a couple of typical workflows within a controller action in RESTful terms
GET(SELECT in RDBMS terms):PUT(INSERT in RDBMS terms):DELETE(DELETE in RDBMS terms)POST(UPDATE in RDBMS terms):.Map<TSource, TDest>method:Mapper.Map<ADomain, ViewModel>(domainInstanceControllerRetrievedUsingTheId, viewModelInstancePassedAsArgument);Armed with those 4 workflows you are ready for the CRUD world.
P.S. A REST reminder: