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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T01:35:06+00:00 2026-06-08T01:35:06+00:00

In my ASP.NET MVC application, I am using unit of work and repository patterns

  • 0

In my ASP.NET MVC application, I am using unit of work and repository patterns for data access.

Using the unit of work class and the repository defined inside it I am fetching the related set of entities in my controller. With my beginner knowledge, I can think of two ways to fetch the business model and convert it to view model.

  • Repository returns the business model to controller, this model than mapped to view model, or
  • Repository itself converts business model to view model and then it is returned to controller.

Currently I am using first approach, but my controller code started to look ugly and long for view models with lots of properties.

On the other hand, I am thinking, since my repository is called UserRepository (for example), it should be returning the business model directly, instead of some model that is useful only for single view.

Which one of these do you think is better practice for large projects ? Is there an alternative way ?

  • 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-08T01:35:07+00:00Added an answer on June 8, 2026 at 1:35 am

    Repositories should return domain models, not view models. As far as the mapping between the models and the view models is concerned, personally I use AutoMapper so I have a separate mapping layer but this layer is called from the controller.

    Here’s how a typical GET controller action might look like:

    public ActionResult Foo(int id)
    {
        // the controller queries the repository to retrieve a domain model
        Bar domainModel = Repository.Get(id);
    
        // The controller converts the domain model to a view model
        // In this example I use AutoMapper, so the controller actually delegates
        // this mapping to AutoMapper but if you don't have a separate mapping layer
        // you could do the mapping here as well.
        BarViewModel viewModel = Mapper.Map<Bar, BarViewModel>(domainModel);
    
        // The controller passes a view model to the view
        return View(viewModel);
    }
    

    which of course could be shortened with a custom action filter to avoid the repetitive mapping logic:

    [AutoMap(typeof(Bar), typeof(BarViewModel))]
    public ActionResult Foo(int id)
    {
        Bar domainModel = Repository.Get(id);
        return View(domainModel);
    }
    

    The AutoMap custom action filter subscribes to the OnActionExecuted event, intercepts the model passed to the view result, invokes the mapping layer (AutoMapper in my case) to convert it to a view model and substitutes it for the view. The view is of course strongly typed to the view model.

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

Sidebar

Related Questions

I have been using http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application as guidance to help me create a repository.I have
few days ago i read tutorial about GenericRepository and Unit Of Work patterns http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application
If an ASP.NET MVC application using Data Annotations... <%= Html.ValidationSummary(Things broke...) %> <% Html.EnableClientValidation();
In my ASP.Net MVC application I am using IoC to facilitate unit testing. The
I have an ASP.NET MVC application using Authorization Attributes on Controllers and Actions. This
I have a ASP.NET MVC application using NHibernate and the application runs fine when
I've made some graphs in my ASP.Net MVC application using the ASP.Net MSChart control.
I am creating an editor in asp.net MVC application using ckeditor. In textarea i
I am uploading a file in my ASP.NET MVC application using Uploadify. Controller: public
In an ASP.NET MVC 4 application using the .NET 4.5 framework in conjunction with

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.