C# IsDirty标志图案
我有一个包含以下函数的BLL基类:C# IsDirty标志图案,c#,.net,C#,.net,我有一个包含以下函数的BLL基类: public bool IsDirty { get; protected set; } internal void SetField<TParam>(ref TParam field, TParam value) { if (EqualityComparer<TParam>.Default.Equals(field, value) == false) {
public bool IsDirty { get; protected set; }
internal void SetField<TParam>(ref TParam field, TParam value)
{
if (EqualityComparer<TParam>.Default.Equals(field, value) == false)
{
field = value;
IsDirty = true;
}
}
我使用IsDirty属性来测试是否需要发布更新。如果至少有一个属性发生更改,则保存到数据库。这适用于大多数类型,但集合和列表可以在不使用set的情况下更改。我为集合编写了一个包装器,以便在列表上有一个IsDirty标志,可以对其进行更改测试:
public class CollectionChangeTracked<T> : Collection<T>
{
public bool IsDirty {get; set;}
public CollectionChangeTracked()
{
IsDirty = false;
}
protected override void InsertItem(int index, T newItem)
{
base.InsertItem(index, newItem);
IsDirty = true;
}
protected override void SetItem(int index, T newItem)
{
base.SetItem(index, newItem);
IsDirty = true;
}
protected override void RemoveItem(int index)
{
base.RemoveItem(index);
IsDirty = true;
}
protected override void ClearItems()
{
base.ClearItems();
IsDirty = true;
}
}
}
公共类集合ChangeTracked:集合
{
公共布尔值为{get;set;}
公共集合更改跟踪()
{
IsDirty=false;
}
受保护的重写void插入项(int索引,T newItem)
{
base.InsertItem(索引,newItem);
IsDirty=true;
}
受保护的覆盖无效SetItem(int索引,T newItem)
{
base.SetItem(索引,newItem);
IsDirty=true;
}
受保护的覆盖void removietem(int索引)
{
基本删除项(索引);
IsDirty=true;
}
受保护的覆盖无效ClearItems()
{
base.ClearItems();
IsDirty=true;
}
}
}
问题是,我现在必须测试Classe的IsDirty属性和任何CollectionChangeTracked.IsDirty更新标志。我可以创建在一个点执行测试的方法,例如:
public CollectionChangeTracked<ApplicationRole> RolesList
{
get { return _rolesList; }
set { SetField(ref _rolesList, value); }
}
public override bool IsDirty
{
get { return ResolveIsDirty(); }
protected set { _isDirty = value; }
private bool ResolveIsDirty()
{
bool returnValue;
if (_isDirty || RolesList.IsDirty)
returnValue = true;
else
returnValue = false;
return returnValue;
}
公共集合更改跟踪角色列表
{
获取{return\u rolesList;}
set{SetField(ref _rolesList,value);}
}
公共覆盖布尔IsDirty
{
获取{return ResolveIsDirty();}
受保护集{u isDirty=value;}
私人文件解析文件()
{
布尔返回值;
如果(_isDirty | | RolesList.isDirty)
returnValue=true;
其他的
returnValue=false;
返回值;
}
但似乎我应该能够想出一个更干净的解决方案,允许包含集合的类订阅CollectionChangeTracked对象的IsDirty更改,并基于该更改更新IsDirty。这是更好的方法吗?我将如何实现?您可以使用
ObservableCollection
注册到CollectionChanged
事件,并在引发事件时标记IsDirty标志
...
ObservableCollection<int> myCollection = new ObservableCollection<int>();
myCollection.CollectionChanged += OnCollectionChanged;
...
public void OnCollectionChanged( Object sender, NotifyCollectionChangedEventArgs e )
{
IsDirty = true;
}
。。。
ObservableCollection myCollection=新的ObservableCollection();
myCollection.CollectionChanged+=OnCollectionChanged;
...
CollectionChanged(对象发送方,NotifyCollectionChangedEventArgs e)上的公共无效
{
IsDirty=true;
}
@jgauffin让我为您解决这个问题:jgauffin=!jgauffin。ObservableCollection的问题是,它的所有使用者都必须支持INotifyPropertyChanged,因此它可以在这种情况下工作,但我会限制集合的用途,这就是我创建CollectionChangeTracked()的原因。使用者不必支持INotifyPropertyChanged,他们可以像使用任何其他集合一样使用它。如果要对集合中的项使用绑定,或者要绑定到类中的集合属性,则只需要INotifyPropertyChanged接口。因此,如果有人将其用作GridView的源,它仍然是fe?如果可以的话。我应该试着更新基类吗?因为IsDirty标志继承自基类,所以如果基类正在侦听任何会在继承基类的类中引发该事件的属性,那就太好了。如果您将ObservaleCollection
用作itemsource
当从集合中添加或删除项时,网格视图将更新。但是,如果ObservableCollection
中的项不是依赖对象,并且未实现INotifyPropertyChnaged
,则当集合中的项的属性更改时,网格视图中的项将不会更新表示可以添加或删除项,但在不替换现有项的情况下无法更新
...
ObservableCollection<int> myCollection = new ObservableCollection<int>();
myCollection.CollectionChanged += OnCollectionChanged;
...
public void OnCollectionChanged( Object sender, NotifyCollectionChangedEventArgs e )
{
IsDirty = true;
}