c# 将类属性标记为脏 [英] c# marking class property as dirty

查看:27
本文介绍了c# 将类属性标记为脏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是一个简单的枚举示例,它定义了一个对象的状态和一个类,它显示了这个枚举的实现.

The following is a simple example of an enum which defines the state of an object and a class which shows the implementation of this enum.

public enum StatusEnum
{
    Clean = 0,
    Dirty = 1,
    New = 2,
    Deleted = 3,
    Purged = 4
}


public class Example_Class
{
    private StatusEnum _Status = StatusEnum.New;

    private long _ID;
    private string _Name;

    public StatusEnum Status
    {
        get { return _Status; }
        set { _Status = value; }
    }

    public long ID
    {
        get { return _ID; }
        set { _ID = value; }
    }

    public string Name
    {
        get { return _Name; }
        set { _Name = value; }
    }
}

当用数据库中的数据填充类对象时,我们将枚举值设置为clean".为了将大部分逻辑保留在表示层之外,我们如何在更改属性时将枚举值设置为脏".

when populating the class object with data from the database, we set the enum value to "clean". with the goal of keeping most of the logic out of the presentation layer, how can we set the enum value to "dirty" when a property is changed.

我在想一些事情;

public string Name
{
    get { return _Name; }
    set 
    {
        if (value != _Name)
        {
               _Name = value; 
           _Status = StatusEnum.Dirty;
        }
    }   
}

在类的每个属性的setter中.

in the setter of each property of the class.

这听起来是个好主意吗,有没有人有更好的想法来说明如何在不在表示层中分配脏标志的情况下分配脏标志.

does this sound like a good idea, does anyone have any better ideas on how the dirty flag can be assigned without doing so in the presentation layer.

推荐答案

当你真的想要在类级别设置一个脏标志(或者,就此而言,通知) - 你可以使用下面的技巧来最大限度地减少混乱您的属性(此处显示 IsDirtyPropertyChanged,只是为了好玩).

When you really do want a dirty flag at the class level (or, for that matter, notifications) - you can use tricks like below to minimise the clutter in your properties (here showing both IsDirty and PropertyChanged, just for fun).

显然,使用枚举方法是一件小事(我不这样做的唯一原因是保持示例简单):

Obviously it is a trivial matter to use the enum approach (the only reason I didn't was to keep the example simple):

class SomeType : INotifyPropertyChanged {
    private int foo;
    public int Foo {
        get { return foo; }
        set { SetField(ref foo, value, "Foo"); }
    }

    private string bar;
    public string Bar {
        get { return bar; }
        set { SetField(ref bar, value, "Bar"); }
    }

    public bool IsDirty { get; private set; }
    public event PropertyChangedEventHandler PropertyChanged;
    protected void SetField<T>(ref T field, T value, string propertyName) {
        if (!EqualityComparer<T>.Default.Equals(field, value)) {
            field = value;
            IsDirty = true;
            OnPropertyChanged(propertyName);
        }
    }
    protected virtual void OnPropertyChanged(string propertyName) {
        var handler = PropertyChanged;
        if (handler != null) {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

您也可以选择将其中的一些推送到抽象基类中,但这是一个单独的讨论

You might also choose to push some of that into an abstract base class, but that is a separate discussion

这篇关于c# 将类属性标记为脏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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