C# 和 ASP.NET 自定义属性特性以及确定属性是否更改

发布于 2024-10-08 09:30:00 字数 506 浏览 0 评论 0原文

我正在开发一个项目,我们想要保留特定对象的历史记录。保存时,我想要对象上的一个方法来确定它是否已更改,以便我可以调用一个方法将其当前状态保存到历史记录中。例如,我从用户进行更改(或可能不更改)的对象填充表单并提交。我想获取原始对象和已从表单更新的该对象的副本,并确定它是否已更改。此外,我可能会在某些时候决定某些属性并不重要(例如,如果名称更改,我将不会跟踪它)。

我认为实现此目的的最简单/最灵活的方法是,如果我可以为我关心的属性提供自定义属性 [ChangeTracked],然后我可以使用反射来获取具有该属性的所有属性的列表并循环遍历它们比较 A.property == B.property 以确定是否有任何更改。

这行得通吗?是否有一种更好/更简单的方法来处理这个问题,例如可以添加到对象中以确定任何属性的值是否已更改的某种内置方法?无论解决方案是什么,一些伪代码将不胜感激。作为澄清一点,解决方案需要确定我关心的值是否实际上发生了变化,而不仅仅是它自创建以来是否已被分配,即如果我设置 Name="bob" 并且在分配之前它已经是“bob”这不算作更改。

I am working on a project where we want to keep a history of a particular object. On save I want a method on the object that will determine if it has changed so that I can call a method to save its current state to history. E.g. I populate a form from an object the user makes changes (or possibly not) and submits the from. I want to take my original object and a copy of that object that has been updated from the form and determine if it has changed at all. Additionally I may decide at some point that certain properties don't matter (e.g. if Name changes I won't track it).

I'm thinking the easiest/most flexible way to accomplish this would be if I could give the properties I care about a custom attribute [ChangeTracked] and then I could use reflection to get a list of all properties with that attribute and loop through them comparing A.property == B.property to determine if any have changed.

Would this work? Is there a significantly better/easier way to handle this, like some sort of built in method you can add to an object to determine if the values of any properties have changed? Whatever the solution some psudo code would be appreciated. Just as a point of clarification the solution needs to determine if the value I care about has actually changed not just if it has been assigned since it was created i.e. if I set Name="bob" and it was already "bob" before my assignment this does not count as a change.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ぃ弥猫深巷。 2024-10-15 09:30:00

这并不花哨,但这是经过考验的真正的蛮力方法。只需向名为 IsDirty 的对象添加一个私有属性即可。对于要跟踪的属性,只需将 IsDirty=True 添加到属性 Set 例程即可。对于更复杂的“我关心吗”规则,只需将它们编码到属性集中即可。

页面按钮的单击事件可以触发 Save 事件,该事件将文本框和下拉列表中的所有值写入对象属性,然后调用对象 Save 方法,该方法在执行任何操作之前测试 IsDirty 属性。

It ain't fancy, but this is the tried and true brute force method. Just add a private property to the object named IsDirty. For properties that you want to track, just add IsDirty=True to the property Set routine. For more complicated "do I care" rules, just code them into the property set.

The page button's click event can fire a Save event that writes all the values from the textboxes and dropdowns into the object properties, then calls the object Save method, which tests the IsDirty property before doing anything.

你的背包 2024-10-15 09:30:00

一种可能的方法是在加载对象时添加对象的深层副本作为对象的私有属性。 (深度复制的一种方法

保存时,您可以将复制对象与您的对象进行比较“live”对象以查看是否发生任何更改。

One possible method would be to add a deep copy of the object as a private property of the object when it is loaded. (One method of deep copy)

On save you can compare the copy object to your "live" object to see if any changes have occurred.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文