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

查看:150
本文介绍了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; }
    }
}

填充从数据库中数据的类对象时,我们设置枚举值干净。在保持大部分的逻辑出了presentation层的目标,我们如何设置枚举值脏当一个属性更改。

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.

这听起来像一个好主意,有没有人对脏标志如何分配更好的想法,而不在presentation层这样做。

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.

推荐答案

当你确实想在类级别脏标志(或者,对于这个问题,通知) - 您可以使用技巧如下面,以尽量减少杂波你的属性(这里同时显示 IsDirty 的PropertyChanged ,只是为了好玩)。

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天全站免登陆