Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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.</p> <p>As a result you don't have THE <code>OrderViewModel</code> and THE <code>CustomerViewModel</code> 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. </p> <p>Suppose, you have an <code>OrderEditView</code> and this view will allow to edit order information and display the customer of that order. You would have an <code>OrderEditViewModel</code> like this:</p> <pre><code>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; } // ... } </code></pre> <p>This <code>OrderEditCustomerViewModel</code> doesn't need a reference to the <code>OrderEditViewModel</code>.</p> <p>You can populate this ViewModel like so:</p> <pre><code>var orderEditViewModel = context.Orders .Where(o =&gt; o.OrderId == 5) .Select(o =&gt; new OrderEditViewModel { OrderId = o.OrderId, ShippingDate = o.ShippingDate, Remark = o.Remark, Customer = new OrderEditCustomerViewModel { Name = o.Customer.Name, City = o.Customer.City } }) .SingleOrDefault(); </code></pre> <p>On the other hand, if you have a <code>CustomerEditView</code> which allows editing customer information and displays the orders of the customer in a list, the ViewModel might be:</p> <pre><code>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&lt;CustomerEditOrderViewModel&gt; Orders { get; set; } } public class CustomerEditOrderViewModel { [ReadOnly(true)] public DateTime? ShippingDate { get; set; } [ReadOnly(true)] public string Remark { get; set; } // ... } </code></pre> <p>Here <code>CustomerEditOrderViewModel</code> doesn't need a reference to the <code>CustomerEditViewModel</code> and you can create the ViewModel from the database this way for example:</p> <pre><code>var customerEditViewModel = context.Customers .Where(c =&gt; c.CustomerId == 8) .Select(c =&gt; new CustomerEditViewModel { CustomerId = c.CustomerId, Name = c.Name, City = c.City, Orders = c.Orders.Select(o =&gt; new CustomerEditOrderViewModel { ShippingDate = o.ShippingDate, Remark = o.Remark }) }) .SingleOrDefault(); </code></pre> <p>The <code>Customer(*)ViewModel</code>s and the <code>Order(*)ViewModel</code>s are different - regarding the necessary references, the properties and the data annotations, depending on the view where they are used.</p> <p>With these considerations in mind the question for mutual correct references between the <code>OrderViewModel</code> and the <code>CustomerViewModel</code> disappears because you normally don't need such a bidirectional reference for your views.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload