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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:28:24+00:00 2026-05-27T04:28:24+00:00

This is a common thing that I have been needing to do recently and

  • 0

This is a common thing that I have been needing to do recently and I was looking for any common patterns to make this a little easier.

The main gist of it all is that I have some data models, which are modelled to satisfy the ORM and purely do CRUD operations to objects. These models are currently exposed via repositories/factories (dependant upon if its C or RUD).

I then have a view model, which is a bit more readable, and is sprinkled with UI concerns, such as validation and mapping data between the view (this is an ASP.MVC scenario but this situation can be abstracted to most situations).

So lets say I go to localhost/user/1, that should go and get me the user with an Id 1 in the DB and then display it on the UI. Ultimately this has to pull data down from the data domain and then map it to a ui model for display purposes.

Here is an example scenario:

public class OrmUser
{
    public int Id {get;set;}
    public string Name {get;set;}
    public IList<Permission> Permissions {get;set;}
}

public class UiUser
{
    [Required]
    public int Id {get;set;}
    [Required]
    public string Name {get;set;}
    public bool IsUserAdmin {get;set;}
}

public class UserMapper : IMapper<UiUser>
{
    public UiUser Get(int id)
    {
        var ormUser = UserRepository.Get(id);
        var uiUser = new UiUser
        {
            Id = ormUser.Id,
            Name = ormUser.Name,
            IsUserAdmin = IsUserAdmin(ormUser.Permissions)
        }
    }

    private bool IsUserAdmin(IList<Permission> permissions)
    {
        return permissions.SomeLinq(ToFindIfTheyAreAnAdmin);
    }
}

This is a simple example but shows how a data model, contains a lot of the same sort of information, but at this view in question you do not care about all the information just a subset of it. This way having a mapper you get to abstract not only the mapping but the communication with the data domain, however you need to write a mapper class for each type, and the above assumes it is a one way mapping, not a 2 way mapping which would require some more code.

So how do you all go about carrying out this mapping? As currently I have just been writing abstraction mappers which basically allow the UI layer to run a query, and get back a view model, abstracting the repositories and the copying data from one model to another, and it just feels like there should be a better way to do this.

  • 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-05-27T04:28:25+00:00Added an answer on May 27, 2026 at 4:28 am

    In .net you should probably take a look at Automapper

    https://github.com/AutoMapper/AutoMapper

    It essentially allows you to do:

    CreateMap<Domain.Customer, ViewModel.Customer>()
    

    And you can then map between the two by saying:

    var vmCustomer = Mapper.Map<Domain.Customer, ViewModel.Customer>(domainCustomer);
    

    It will likely save you a ton of boiler plate code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a common problem for all developers, I am looking for the best
Perhaps I'm missing something simple, as I believe that this is a common scenario...
Some Background I have been using Git for a while now. The projects that
I have been in this situation quite a few times where visual studio does
I've been hunting for a long time for this, but despite my intuition that
I know this must be a pretty common problem, but I haven't been able
This is a rather simple question, I have a base class which implements common
I have been trying to figure this out for a while now and even
I've been using manual constructor injection DI for a little bit now. One thing
I have this scenario, which I think must be pretty common: class Parameter {

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.