ViewModel Questions

For simplicity, let's say I have a CustomerViewModel, and I want to display their basic information (name, email address, phone, etc.), as well as a list of recent orders.

Should I have a collection property, such as Orders of type OrderListViewModel, or use POCO objects returned from the store?

I guess I am really asking if I should have a view model for each object, and then have collections of those that are attached to the "parent" view models?

+3
source share
4 answers

, ViewModels, . DDD, Entity, View. , , , .

View Model Automapper, : http://weblogs.asp.net/shijuvarghese/archive/2010/02/01/view-model-pattern-and-automapper-in-asp-net-mvc-applications.aspx

http://www.bengtbe.com/blog/post/2009/04/14/Using-AutoMapper-to-map-view-models-in-ASPNET-MVC.aspx

ViewModel :

public class CustomerModel {
    public string Name {get; set;}
    public string Email {get; set;}
    public string Phone {get; set;}
    public OrderModel[] Orders {get; set;}

  public class OrderModel {
    public int OrderNumber {get; set;}
    public double OrderTotal {get; set;}
  }
}

Automapper (SoC)

+3

, . , .

, , :

class ViewModel
{
    Customer Customer { get; set; }
    IEnumerable<Order> Orders { get; set; }
}

View Model, - . , , , .

AutoMapper - , View View.

+3

. , ( ..), , . 1 1 .

, .

, - , POCOs , . , . , , , , , .

+2

View Model , . , , , .

0
source

Source: https://habr.com/ru/post/1796687/


All Articles