什么是实现一个对象更改跟踪的最佳方式 [英] What would be the best way to implement change tracking on an object

查看:96
本文介绍了什么是实现一个对象更改跟踪的最佳方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含5个属性的类。

如果任何值assingned到任何这些字段,向另一值(例如IsDIrty)将改变涂真。的

 公共类的Class1
{
    布尔IsDIrty {获取;集;}

    字符串为prop1 {获取;集;}
    字符串Prop2 {获取;集;}
    字符串Prop3 {获取;集;}
    字符串Prop4 {获取;集;}
    字符串Prop5 {获取;集;}
}
 

解决方案

要做到这一点,你真的不能使用自动吸气和放大器;制定者,你需要在每一个二传手设定IsDirty。

我一般有的SetProperty泛型方法,需要一个ref参数,属性名和新值。 我称这种现象为二传手,允许一个地步,我可以设置isDirty并提高更改通知事件,例如

 保护布尔的SetProperty< T>(字符串名称,裁判牛逼的属性oldValue,T为newValue)其中T:System.IComparable< T>
    {
        如果(属性oldValue == NULL || oldValue.CompareTo(newValue)以!= 0)
        {
            属性oldValue =为newValue;
            如果(的PropertyChanged!= NULL)
                的PropertyChanged(这一点,新System.ComponentModel.PropertyChangedEventArgs(名称));
            isDirty = TRUE;
            返回true;
        }
        返回false;
    }
//对于可空类型
保护无效的SetProperty< T>(字符串名称,裁判可空< T>属性oldValue,可空< T> newValue)以其中T:结构,System.IComparable< T>
{
    如果(oldValue.HasValue = newValue.HasValue ||(newValue.HasValue&安培;!&安培;!oldValue.Value.CompareTo(newValue.Value)= 0))
    {
        属性oldValue =为newValue;
        如果(的PropertyChanged!= NULL)
            的PropertyChanged(这一点,新System.ComponentModel.PropertyChangedEventArgs(名称));
    }
}
 

I have a class which contains 5 properties.

If any value is assingned to any of these fields, an another value (for example IsDIrty) would change tu true.

public class Class1
{
    bool IsDIrty {get;set;}

    string Prop1 {get;set;}
    string Prop2 {get;set;}
    string Prop3 {get;set;}
    string Prop4 {get;set;}
    string Prop5 {get;set;}
}

解决方案

To do this you can't really use automatic getter & setters, and you need to set IsDirty in each setter.

I generally have a "setProperty" generic method that takes a ref parameter, the property name and the new value. I call this in the setter, allows a single point where I can set isDirty and raise Change notification events e.g.

protected bool SetProperty<T>(string name, ref T oldValue, T newValue) where T : System.IComparable<T>
    {
        if (oldValue == null || oldValue.CompareTo(newValue) != 0)
        {
            oldValue = newValue;
            if (PropertyChanged != null)
                PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(name));
            isDirty = true;
            return true;
        }
        return false;
    }
// For nullable types
protected void SetProperty<T>(string name, ref Nullable<T> oldValue, Nullable<T> newValue) where T : struct, System.IComparable<T>
{
    if (oldValue.HasValue != newValue.HasValue || (newValue.HasValue && oldValue.Value.CompareTo(newValue.Value) != 0))
    {
        oldValue = newValue;
        if (PropertyChanged != null)
            PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(name));
    }
}

这篇关于什么是实现一个对象更改跟踪的最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆