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

  • Home
  • SEARCH
  • 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 3976088
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T04:45:46+00:00 2026-05-20T04:45:46+00:00

I am new to both MVC and Entity Framework and I have a question

  • 0

I am new to both MVC and Entity Framework and I have a question about the right/preferred way to do this.

I have sort of been following the Nerd Dinner MVC application for how I am writing this application. I have a page that has data from a few different places. It shows details that come from a few different tables and also has a dropdown list from a lookup table.

I created a ViewModel class that contains all of this information:

class DetailsViewModel {
    public List<Foo> DropdownListData { get; set; }

    // comes from table 1
    public string Property1 { get; set; } 
    public string Property2 { get; set; }

    public Bar SomeBarObject { get; set; } // comes from table 2
}

In the Nerd Dinner code, their examples is a little too simplistic. The DinnerFormViewModel takes in a single entity: Dinner. Based on the Dinner it creates a SelectList for the countries based on the dinners location.

Because of the simplicity, their data access code is also pretty simple. He has a simple DinnerRepository with a method called GetDinner(). In his action methods he can do simple things like:

Dinner dinner = new Dinner();

// return the view model
return View(new DinnerFormViewModel(dinner));

OR

Dinner dinner = repository.GetDinner(id);

return View(new DinnerFormViewModel(dinner));

My query is a lot more complex than this, pulling from multiple tables…creating an anonymous type:

var query = from a in ctx.Table1
            where a.Id == id
            select new { a.Property1, a.Property2, a.Foo, a.Bar };

My question is as follows:

What should my repository class look like? Should the repository class return the ViewModel itself? That doesn’t seem like the right way to do things, since the ViewModel sort of implies it is being used in a view. Since my query is returning an anonymous object, how do I return that from my repository so I can construct the ViewModel in my controller actions?

  • 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-20T04:45:47+00:00Added an answer on May 20, 2026 at 4:45 am

    You are correct a repository should not return a view model. As changes to your view will cause you to change your data layer.

    Your repository should be an aggregate root. If your property1, property2, Foo, Bar are related in some way I would extract a new class to handle this.

    public class FooBarDetails
    {
       public string Property1 {get;set;}
       public string Property2 {get;set;}
       public Foo Foo {get;set;}
       public Bar Bar {get;set;}
    }
    
    var details = _repo.GetDetails(detailId);
    

    If Foo and Bar are not related at all it might be an option to introduce a service to compose your FooBarDetails.

    FooBarDetails details = _service.GetFooBar(id);
    

    where GetFooBar(int) would look something like this:

    _fooRepo.Get(id);
    _barRepo.Get(id);
    
    return new FooBarDetails{Foo = foo, Bar = bar, Property1 = "something", Property2 = "something else"};
    

    This all is conjecture since the design of the repository really depends on your domain. Using generic terms makes it hard to develop potential relationships between your objects.

    Updated
    From the comment if we are dealing with an aggregate root of an Order. An order would have the OrderItem and also the customer that placed the order.

    public class Order
    {
        public List<OrderItem> Items{get; private set;}
        public Customer OrderedBy {get; private set;}
        //Other stuff
    }
    
    public class Customer
    {
      public List<Orders> Orders{get;set;}
    }
    

    Your repo should return a fully hydrated order object.

    var order = _rep.Get(orderId);
    

    Since your order has all the information needed I would pass the order directly to the view model.

    public class OrderDetailsViewModel
    {
      public Order Order {get;set;}
      public OrderDetailsViewModel(Order order)
      {
        Order = order;
      }
    }
    

    Now having a viewmodel with only one item might seem overkill (and it most likely will be at first). If you need to display more items on your view it starts to help.

    public class OrderDetailsViewModel
    {
      public Order Order {get;set;}
      public List<Order> SimilarOrders {get;set;}
      public OrderDetailsViewModel(Order order, List<Order> similarOrders)
      {
        Order = order;
        SimilarOrders = similarOrders;
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm very new to both the Mvc framework as well as JavaScript and JQuery.
I am relatively new to MVC, so this is likely a novice question. I
I'm new to both ASP.Net MVC and jQuery and what I'm trying to do
At a new job I started, we have both a Java application that handles
We have a large enterprise consisting of many apps both old and new backed
This is driving me crazy. Hopefully my question makes sense... I'm using MVC 2
I am using Entity Framework with asp.net mvc, but I don't think mvc plays
I know this has been asked several times and I have read all the
I am building an ASP.Net MVC 3 application with Entity Framework 4. When the
New to both Ruby and Rails but I'm book educated by now (which apparently

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.