如何设计ViewModel [英] how to design ViewModel
问题描述
我在项目中实施了EF 4。其中包含客户和订单表。其中一个(客户)与多个(订单)的关系。
I have EF 4 implemented in the project. Within it, there are tables customer and order. Which has relationship one (customer) to many (order).
我正在为这两个视图(CustomerViewModel和OrderViewModel)创建一个视图模型,以将其从我的域层传递到接口层(在本例中为MVC)。
I'm creating a viewmodel for both (CustomerViewModel and OrderViewModel) to be passed from my domain layer to interface layer (MVC in this case).
现在的问题是我是否需要同时引用两个视图模型?例如,在customerviewmodel中具有 IEnumerable< OrderViewModel>
并在orderviewmodel中具有 CustomerViewModel
。如果是这样,我应该如何设计(作为最佳做法),以便 IEnumerable< OrderViewModel>
和 CustomerViewModel
填充了正确的引用?
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?
推荐答案
我会始终要在考虑特定视图的情况下驱动ViewModel的设计,而不要从域模型(=实体)的角度出发。ViewModel的外观取决于您要显示的内容和要在视图中进行修改的内容。
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.
结果是您没有 OrderViewModel
和THE CustomerViewModel
因为您具有不同的视图,这些视图将显示或编辑订单或客户或其中的一部分,因此,您具有用于特定目的和视图的ViewModel,因此多次t变化。
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.
假设您有一个 OrderEditView
,此视图将允许编辑订单信息并显示该订单的客户。您将具有一个 OrderEditViewModel
:
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; }
// ...
}
此 OrderEditCustomerViewModel
不需要引用 OrderEditViewModel
。
您可以填充这个ViewModel就像这样:
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();
另一方面,如果您有 CustomerEditView
允许编辑客户信息并在列表中显示客户的订单,ViewModel可能是:
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; }
// ...
}
此处 CustomerEditOrderViewModel
不需要引用 CustomerEditViewModel
,您可以通过这种方式从数据库创建ViewModel,例如:
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();
Customer(*)ViewModel
和 Order(*)ViewModel
有所不同-关于必需的引用,属性和数据注释,具体取决于使用它们的视图。
The Customer(*)ViewModel
s and the Order(*)ViewModel
s are different - regarding the necessary references, the properties and the data annotations, depending on the view where they are used.
考虑到这些注意事项,在 OrderViewModel
和 CustomerViewModel $ c $之间相互正确引用的问题c>消失是因为您通常不需要这种双向引用来获取视图。
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.
这篇关于如何设计ViewModel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!