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

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

问题描述

以下是定义对象状态的枚举的简单示例,以及显示此枚举的实现的类。

  public enum StatusEnum 
{
Clean = 0,
Dirty = 1,
New = 2,
删除= 3,
清除= 4
}


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

private long _ID;
private string _Name;

public StatusEnum状态
{
get {return _Status; }
set {_Status = value; }
}

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

public string名称
{
get {return _Name; }
set {_Name = value; }使用数据库中的数据填充类对象时,
}
}



,我们将枚举值设置为clean。目标是保持大部分逻辑不在表示层之外,当属性改变时,我们如何将枚举值设置为dirty。



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

的类。



这听起来像是个好主意,任何人都有更好的想法如何可以分配脏标志,而不这样做在表示层。 / p>

解决方案

当你真的想在类级别(或者说,通知) (这里显示 IsDirty PropertyChanged ,只是为了乐趣)。 p>

显然,使用枚举方法是一件很简单的事情(唯一的原因是我没有保持这个例子简单):

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

私有字符串bar;
public string Bar {
get {return bar; }
set {SetField(ref bar,value,Bar); }
}

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

一些进入抽象基类,但这是一个单独的讨论


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; }
    }
}

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.

i was thinking something along the lines of;

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

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.

解决方案

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#标记类属性为dirty的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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