实现'dirty'flag功能的不同方法[英] Different ways to implement 'dirty'-flag functionality

本文是小编为大家收集整理的关于实现'dirty'flag功能的不同方法的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

几乎每个程序员都在他的生活中完成了一次:如果变量的值更改,则设置一些标志.总是有很多属性,您想跟踪是否发生了变化

  1. 在任何财产中
  2. 在特定属性中
  3. 或在某些属性中

我对上述情况实现"肮脏的"功能的不同方式感兴趣,除了在每个属性更改中更新标准对象宽脏标志.必须有比在每个设置中放置" dirty = true" 更好的东西:它看起来很丑陋,而且是一件乏味的作品.

推荐答案

对于我的dao,我保留了从数据库中检索的原始值的副本.当我发送它以进行更新时,我只需将原始值与当前值进行比较.它的处理成本有所成就,但比每个物业拥有肮脏的国旗要好得多.

编辑以进一步证明没有肮脏的标志:如果属性返回其原始价值,则无法反映这一点,因为原始值丢失了,肮脏的标志会继续肮脏.

其他推荐答案

我曾经有一个基本实体类,提供脏/删除的逻辑.

编写实体 - 班级时,您可以做类似的事情:

public string Name
{
    get { return name; }
    set { setValue("Name", value); }
}

这很好,但是患有"丑陋的弦"疾病...

今天,您可以使用lambda表达式排除字符串:

set {setValue(x => x.Name, value);}

or,我认为这是最好的解决方案,您可以使用 aop :

https://www.postsharp.net/

这样,您可以通过属性定义操作.您可以创建一个属性,并指定用户更改关联属性时,实体会变脏.

此外,您可以保留类(基本实体)中的属性列表,该属性会记住更改的属性,并从AOP代码中访问该列表.

其他推荐答案

我创建了一个名为DirtyValue<T>的类,该类具有原始值和当前值.在首次使用时,它同时设置了原始值和当前值.连续的呼叫仅设置当前值.

您可以通过比较两者与称为isdirty()的Readonly bool属性进行比较.使用此技术,您也可以访问原始值.

本文地址:https://www.itbaoku.cn/post/627616.html

问题描述

Almost every programmer did it once in his life: setting some flag if a variable's value changed. There's always lots of properties and you want to keep track if something changed

  1. in any property
  2. in a specific property
  3. or in some set of properties

I'm interested in different ways to implement the "dirty-flag" functionality for the above situations, besides the standard object wide dirty flag being updated on each property change. There must be something better than putting "dirty = true" in each setter: it just looks ugly and is a tedious work.

推荐答案

For my DAO I keep a copy of the original values as retrieved from the database. When I send it to be updated, I simply compare the original values with the current. It costs a little in processing but it is a lot better than having a dirty flag per property.

EDIT to further justify not having a dirty flag: if the property returns to its original value, there is no way to reflect that, the dirty flag continues dirty because the original value was lost.

其他推荐答案

I used to have a base Entity class, providing Dirty/Removed Logic.

When writing Entity-subclasses, you could do something like:

public string Name
{
    get { return name; }
    set { setValue("Name", value); }
}

This works fine, but has the 'ugly string' disease...

Today you can use Lambda Expressions to exclude the strings:

set {setValue(x => x.Name, value);}

Or, and I think that this is the best solution, you could use AOP:

https://www.postsharp.net/

This way, you can define actions by Attributes. You create an attribute and specify that when the user changes the associated property, the entity becomes dirty.

Additionally, you can keep a list of properties in your class (base Entity) which will remember the changed properties, and access that list from your AOP Code.

其他推荐答案

I've created a class called DirtyValue<T> which has an original value and a current value. On its first use it sets both the original value and the current value. Successive calls only set the current value.

You can tell if it has changed by comparing the two, with a readonly bool property called IsDirty(). Using this technique you can also get access to the original value as well.