简体   繁体   English

ASP.NET MVC中ViewModel属性的往返转换

[英]Round trip transformation of ViewModel property in ASP.NET MVC

I'm working on an ASP.NET MVC application and I'm trying to figure out how to support optimistic concurrency. 我正在研究ASP.NET MVC应用程序,并且试图弄清楚如何支持开放式并发。 The approach I'm working through right now is to have the web app retrieve an instance of an entity (just a POCO), and build a ViewModel from it. 我现在正在研究的方法是让Web应用程序检索一个实体的实例(只是POCO),并从中创建一个ViewModel。 The ViewModel will include an object UnderlyingEntity property that holds the instance of the entity itself. ViewModel将包含一个object UnderlyingEntity属性,该属性包含实体本身的实例。 I can put the UnderlyingEntity property in a hidden field in the view, then when the view is submitted, apply the changes to the UnderlyingEntity , and send it off to be updated. 我可以将UnderlyingEntity属性放在视图的隐藏字段中,然后在提交视图时,将更改应用于UnderlyingEntity ,然后将其发送以进行更新。 Since I've persisted my original entity instance in the hidden field, when I save I have the original timestamp to be used for the concurrency check. 由于我将原始实体实例保留在隐藏字段中,因此在保存时,我具有用于并发检查的原始时间戳。

The hurdles I'm trying to overcome with this are: 我要克服的障碍是:

  1. How do I do round-trip serialization/deserialization of the UnderlyingEntity property. 我该如何做UnderlyingEntity属性的往返序列化/反序列化。 I could serialize it easily enough in the view but how do I get it back to the original type when the form is posted? 我可以在视图中轻松地对其进行序列化,但是在发布表单时如何将其恢复为原始类型? I think I need a ModelBinder or ValueProvider but I'm not sure exactly where to start. 我认为我需要ModelBinder或ValueProvider,但我不确定从哪里开始。
  2. I'd also like to encrypt/decrypt (edit: or hashing) the value so it's not sitting in the hidden field in plain text. 我还想对值进行加密/解密(编辑:或散列),以使其不位于纯文本的隐藏字段中。 If it were in plain text, it would basically be editable by a user through developer tools. 如果是纯文本格式,则基本上用户可以通过开发人员工具对其进行编辑。 I think if I solve #1, I can do this along with it. 我认为,如果我解决了#1,我可以做到这一点。

Nope. 不。 This is not how to do deal with concurrency in EF. 这不是在EF中处理并发的方法。

The best practice is the following steps: 最佳做法是以下步骤:

  • Add a new property of type row version or timestamp on your entity. 在您的实体上添加行类型或时间戳类型的新属性。 This column will be used by EF to check the version of the row into your database when updating. EF将使用此列在更新时将行的版本检查到数据库中。 If the value differs with the current value stored into the database then it will throw an exception. 如果该值与存储在数据库中的当前值不同,则它将引发异常。
  • In your Razor wiew just store the value of your row version into a hidden field. 在Razor中,只需将行版本的值存储到隐藏字段中即可。 (It will generate a Base 64 string like Stephen Muecke spotted in comment) (它将生成一个Base 64字符串,例如在评论中发现的Stephen Muecke)
  • when user send back the data after editing to your application, you map your view model to your entity. 当用户在编辑完应用程序后将数据发送回时,您会将视图模型映射到您的实体。
  • When saving the data EF will throw an exception DbUpdateConcurrencyException , catch that exception. 保存数据时,EF将引发异常DbUpdateConcurrencyException ,捕获该异常。 In the catch block, you do what you want. 在catch块中,您可以执行所需的操作。

To learn more about that go to this link => https://docs.microsoft.com/en-us/aspnet/mvc/overview/getting-started/getting-started-with-ef-using-mvc/handling-concurrency-with-the-entity-framework-in-an-asp-net-mvc-application 要了解有关此内容的更多信息,请转到此链接=> https://docs.microsoft.com/zh-cn/aspnet/mvc/overview/getting-started/getting-started-with-ef-using-mvc/handling-concurrency -with最实体框架功能于一个-ASP净MVC-应用

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

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