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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T09:04:04+00:00 2026-06-01T09:04:04+00:00

I have EF 4 implemented in the project. Within it, there are tables customer

  • 0

I have EF 4 implemented in the project. Within it, there are tables customer and order. Which has relationship one (customer) to many (order).

I’m creating a viewmodel for both (CustomerViewModel and OrderViewModel) to be passed from my domain layer to interface layer (MVC in this case).

Now the question is “do I need to reference both viewmodel? for example in customerviewmodel has IEnumerable<OrderViewModel> and in orderviewmodel has CustomerViewModel. If so how do I design it (as a best practice) so that IEnumerable<OrderViewModel> and CustomerViewModel is populated with the correct reference?

  • 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-01T09:04:06+00:00Added an answer on June 1, 2026 at 9:04 am

    I would always drive the design of ViewModels with the specific view in mind, never from the viewpoint of the domain model (= the entities). How a ViewModel looks depends on what you want to display and what you want to modify in a view.

    As a result you don’t have THE OrderViewModel and THE CustomerViewModel because you have different views which will display or edit an order or customer or parts of these. So, you have those ViewModels for a specific purpose and view and therefore multiple times in different variations.

    Suppose, you have an OrderEditView and this view will allow to edit order information and display the customer of that order. You would have an OrderEditViewModel like this:

    public class OrderEditViewModel
    {
        public int OrderId { get; set; }
    
        public DateTime? ShippingDate { get; set; }
    
        [StringLength(500)]
        public string Remark { get; set; }
        //...
    
        public OrderEditCustomerViewModel Customer { get; set; }
    }
    
    public class OrderEditCustomerViewModel
    {
        [ReadOnly(true)]
        public string Name { get; set; }
    
        [ReadOnly(true)]
        public string City { get; set; }
        // ...
    }
    

    This OrderEditCustomerViewModel doesn’t need a reference to the OrderEditViewModel.

    You can populate this ViewModel like so:

    var orderEditViewModel = context.Orders
        .Where(o => o.OrderId == 5)
        .Select(o => new OrderEditViewModel
        {
            OrderId = o.OrderId,
            ShippingDate = o.ShippingDate,
            Remark = o.Remark,
            Customer = new OrderEditCustomerViewModel
            {
                Name = o.Customer.Name,
                City = o.Customer.City
            }
        })
        .SingleOrDefault();
    

    On the other hand, if you have a CustomerEditView which allows editing customer information and displays the orders of the customer in a list, the ViewModel might be:

    public class CustomerEditViewModel
    {
        public int CustomerId { get; set; }
    
        [Required, StringLength(50)]
        public string Name { get; set; }
    
        [Required, StringLength(50)]
        public string City { get; set; }
        //...
    
        public IEnumerable<CustomerEditOrderViewModel> Orders { get; set; }
    }
    
    public class CustomerEditOrderViewModel
    {
        [ReadOnly(true)]
        public DateTime? ShippingDate { get; set; }
    
        [ReadOnly(true)]
        public string Remark { get; set; }
        // ...
    }
    

    Here CustomerEditOrderViewModel doesn’t need a reference to the CustomerEditViewModel and you can create the ViewModel from the database this way for example:

    var customerEditViewModel = context.Customers
        .Where(c => c.CustomerId == 8)
        .Select(c => new CustomerEditViewModel
        {
            CustomerId = c.CustomerId,
            Name = c.Name,
            City = c.City,
            Orders = c.Orders.Select(o => new CustomerEditOrderViewModel
            {
                ShippingDate = o.ShippingDate,
                Remark = o.Remark
            })
        })
        .SingleOrDefault();
    

    The Customer(*)ViewModels and the Order(*)ViewModels are different – regarding the necessary references, the properties and the data annotations, depending on the view where they are used.

    With these considerations in mind the question for mutual correct references between the OrderViewModel and the CustomerViewModel disappears because you normally don’t need such a bidirectional reference for your views.

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

Sidebar

Related Questions

I have a TabActivity implemented for my project which is running fine up to
In my project I have the following three interfaces, which are implemented by classes
I have implemented Unity within my Asp.Net MVC2 project. I am currently registering my
I'm using JQgrid, with a select/dropdown in my project, and have implemented it in
I have successfully implemented slider to my Cocos2D project using this resource . The
I have a school project in which I have to implement a chat application,
I have BaseView which implement UIViewController. Every view in project must implement this BaseView.
Suppose I have a project implemented in a specific programming language whose use is
Does anyone have any good references for equations which can be implemented relatively easily
I have a Class Library project which houses some shared code between other projects

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.