How to create a ViewModel

I have EF 4 implemented in a project. It has customer tables and an order. Which relates one (customer) to many (order).

I am creating a viewmodel for both (CustomerViewModel and OrderViewModel) that will be passed from my domain level to the interface level (MVC in this case).

Now the question arises: "Do I need to refer to both types? For example, in the customerviewmodel there is an IEnumerable<OrderViewModel> , and in the orderviewmodel it has a CustomerViewModel . If so, how can I create it (as a best practice) so that the IEnumerable<OrderViewModel> and the CustomerViewModel populated right link?

+7
entity-framework viewmodel
source share
1 answer

I will always manage the design of ViewModels based on a specific view, never in terms of a domain model (= entity). What the ViewModel looks like depends on what you want to display and what you want to change in the view.

As a result, you do not have THE OrderViewModel and THE CustomerViewModel , because you have different views that will display or edit the order or the customer or parts thereof. Thus, you have these ViewModels for a specific purpose and view, and therefore several times in different ways.

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

 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 does not need a reference to the OrderEditViewModel .

You can populate this ViewModel as follows:

 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 that allows you to edit customer information and display customer orders in a list, the ViewModel could 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 does not require a reference to CustomerEditViewModel , and you can create a 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(); 

Customer(*)ViewModel and Order(*)ViewModel are different - regarding the necessary links, properties and data annotations depending on the type in which they are used.

Given these considerations, the question of OrderViewModel correct links between OrderViewModel and CustomerViewModel disappears because you usually don't need such a bi-directional link for your views.

+23
source share

All Articles