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 9116855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:46:22+00:00 2026-06-17T04:46:22+00:00

What is the best practise in mapping your database entities to your models and

  • 0

What is the best practise in mapping your database entities to your models and performing business logic? I’ve seen considerably different implementations of both. I have noticed a number of implementations where the Repository(in the Data Layer) itself is responsible for mapping the database entities to the domain models. For example, a repository that would do this:

public IQueryable<Person> GetPersons()
{
      return DbSet.Select(s => new Person
                    {
                        Id = s.Id,
                        FirstName= s.FirstName,
                        Surname= s.Surname,
                        Location = s.Location,
                    });
}

But having searched comprehensively around SO on N Tier design, I’ve noticed that while there is no silver bullet, in most situations it’s advisable to perform the mapping inside the controller in the MVC project either manually or using a Mapper. It’s also been reiterated that the Service layer should never perform the mapping and that it’s responsibility should be to perform business logic. A couple of questions here:

  1. Which method is advisable in regards to where to map the entities to models and vice versa? Should the repository do this or should the mapping be done in the controller?
  2. Suppose I want to perform some business logic on the entities that I have retrieved from the database, for example, return the full name of the Person enities, or increase the age of all Persons by 10 years, where should this operation be performed. On the model itself? For example would I have a FullName property on the model which would compute the full name and the age? Or do I define some service inside my service layer to perform business logic?

EDIT

Wow so many close votes. Apologies, I didn’t search comprehensively enough. The ‘where to perform business logic’ issue I’ve raised here can already be found on SO and elsewhere (although conveyed somewhat cryptically at times):

Validating with a Service Layer by Stephen Walther

Skinny Controllers

Another great, but more generic answer here on SO

Where Should I put My Controller Business Logic in MVC

Does a Service Map Entities to a View Model

However I’m yet to find a standard solution to the mapping question I had, and I think I could have perhaps expressed my question more eloquently. So the general consensus seems to be that business logic goes in the service layer, and mapping the domain models to view models should take place in the controller/presentation layer. And since it’s advisable not to surface your DB entities to any layers other than the Data layer, it’s recommended to map your entities to domain models at the data layer either manually or via a mapper such as Auto Mapper (This is what I have gathered from reading many articles). My confusion arose from the question of where should mapping entities to domain models and mapping domain models to view models take place. However as I previously alluded to I could have expressed my question more clearly. The reason for my confusion was that I had read that mapping entities to to domain models should happen in the controller, this should rather be rephrased to say “Mapping entities to domain models should happen at the data later, and mapping domain models to view models should take place in the Controller.

  • 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-17T04:46:23+00:00Added an answer on June 17, 2026 at 4:46 am
    1. Which method is advisable in regards to where to map the entities to
      models and vice versa? Should the repository do this or should the
      mapping be done in the controller?

    I strongly prefer to see the mapping happen in the repository and not the controller. The controller needs to act purely as a coordinator as Suhas mentions in his answer. As an alternative, maybe you can utilize a mapping class in the repository that passes the entity and return a mapped model – kind of like Auto Mapper

    1. Suppose I want to perform some business logic on the entities that I
      have retrieved from the database, for example, return the full name of
      the Person enities, or increase the age of all Persons by 10 years,
      where should this operation be performed. On the model itself? For
      example would I have a FullName property on the model which would
      compute the full name and the age? Or do I define some service inside
      my service layer to perform business logic?

    If it’s possible, perform the business logic on the service. Why put the burden on the application to do what the service layer should have done? I believe this is the service’s domain, not the app’s. Also, I don’t think returning concatenated or derived properities from the model is a bad thing either.

    Summary:

    • Controller handles requests from views and forwards them to
      repository
    • Repository is the conduit to your datastores
    • Service handles requests from repository, processes business logic,
      and returns mapped models
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the best practise for returning an error of a business rule in
i'm looking for best practise on designing database schema for content management system. any
What's the best practise to store IP's with PHP in MySQL database? There's a
what is the best practice of deleting a parent in a unidirectional hibernate mapping
Just wondering what the best practise advice would be on the architecture for a
What is the best-practise way of Identifying dynamically generated element on page? Let me
It is a best practise to initialise a variable at the time of declaration.
What is the best practise for using the this keyword in Java? For example,
What's the best practise when adapting C-style functions which return a true/false to Java?
I have a question regarding the best practise of handling formated text when using

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.