Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8240003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T20:20:18+00:00 2026-06-07T20:20:18+00:00

my mvc3 project has following layers. controller -> service -> repository. I need to

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T20:20:22+00:00Added an answer on June 7, 2026 at 8:20 pm

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a MVC3 project. Right now I have a table which has
I am writing a MVC3 project. Right now I have a table which has
I've come to an MVC3 project I wrote just a week ago which has
I've inherited an MVC3 project that has a large number of ASPX views that
I have MVC3 project & Valums file uploader . The code is following <script
I'm trying to implement ServiceStack into my MVC3 project, and am following the tutorial
I have an asp.net mvc3 project, it has some reports in aspx web pages.
So here's the issue, my mvc3 project uses Dependency Injection and has a base
For a current MVC3 project I have a model that has multiple pages for
in my ASP.NET MVC3 Project, I've got an action which runs a certain amount

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.