简体   繁体   English

在ASP.net MVC中使用EF对象的设计注意事项

[英]Design considerations for using EF objects in ASP.net MVC

I am using EF code first in one of my mvc 3 projects. 我在我的一个mvc 3项目中首先使用EF代码。 I have a question about what patterns to use when passing a complex EF POCO object to and from the views. 我有一个问题,即在将复杂的EF POCO对象传入和传出视图时要使用哪些模式。

For example, a customer object has a list of orders and each order has a list of items. 例如,客户对象具有订单列表,每个订单都有一个项目列表。 The customer object will be sent to the view. 客户对象将被发送到视图。 The view updates the customer object and its inside objects (orders, items), then send it back the controller. 视图更新客户对象及其内部对象(订单,项目),然后将其发送回控制器。 The controller have EF to persist the customer object. 控制器具有EF以持久保存客户对象。

My questions are following: 我的问题如下:

  1. Should I serialize the EF poco object to a JSON object so I can use it inside the view? 我应该将EF poco对象序列化为JSON对象,以便在视图中使用它吗?

  2. How can I re-construct the customer object when I recieve updates from view? 当我从视图接收更新时,如何重新构建客户对象?

  3. After the customer object is reconstructed, Is it possible to save the entire object graph (customer, orders, items) in one shot? 重建客户对象后,是否可以一次性保存整个对象图(客户,订单,项目)?

Thanks 谢谢

I tend to stay away from using the EF POCO objects as the model for my views. 我倾向于远离使用EF POCO对象作为我的视图的模型。 I generally will create View Models from one or more POCO objects as what I need in a view never matches exactly a single EF POCO object. 我通常会从一个或多个POCO对象创建视图模型,因为我在视图中需要的东西永远不会与单个EF POCO对象完全匹配。 The view models will then create the EF objects that are then saved to the DB. 然后,视图模型将创建EF对象,然后将其保存到DB。

  1. Should I serialize the EF poco object to a JSON object so I can use it inside the view? 我应该将EF poco对象序列化为JSON对象,以便在视图中使用它吗? No. 没有。
  2. How can I re-construct the customer object when I recieve updates from view? 当我从视图接收更新时,如何重新构建客户对象? Don't. 别。 Let the default modelbinder materialize the POSTed data into a viewmodel (or editmodel), and use that data to issue commands to a lower layer. 让默认的模型绑定器将POSTed数据具体化为viewmodel(或editmodel),并使用该数据向较低层发出命令。
  3. After the customer object is reconstructed, Is it possible to save the entire object graph (customer, orders, items) in one shot? 重建客户对象后,是否可以一次性保存整个对象图(客户,订单,项目)? It is, but you shouldn't. 它是,但你不应该。 Instead, deal with each update individually based on your use cases. 而是根据您的用例单独处理每个更新。

Follow mojo722 and Pluc's advice here. 在这里关注mojo722和Pluc的建议。 Don't use EF POCO entities in your MVC layer. 不要在MVC层中使用EF POCO实体。 Use viewmodels. 使用viewmodels。 Here's how it would work: 以下是它的工作原理:

  • Controller needs data, it asks a lower layer. 控制器需要数据,它要求较低层。 The lower layer gets the data and returns entities (or better yet, entity views). 较低层获取数据并返回实体(或更好的实体视图)。
  • Controller converts entities to viewmodels (AutoMapper is good for this, but you can map manually as well). 控制器将实体转换为视图模型(AutoMapper适用于此,但您也可以手动映射)。
  • Controller passes viewmodels to view. 控制器传递视图模型进行查看。
  • View sends HTTP POST data from HTML form. View从HTML表单发送HTTP POST数据。
  • Default model binder converts HTTP POSTed form data to a viewmodel. 默认模型绑定器将HTTP POSTed表单数据转换为viewmodel。
  • Controller receives viewmodel data, issues a command to the lower layer. Controller接收视图模型数据,向下层发出命令。
  • Lower layer uses EF to save new entity state. 下层使用EF来保存新的实体状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM