我认为这很简单,但我仍然找不到更好的解决方案。因此,在研究了这个主题之后,我想在这里问这个问题以具有专家意见。
基本上,我正在从事WPF应用程序,并且已经定义了GenericObserableCollection<T>
实施ObservableCollection<T>
,并且大多数集合在整个项目中都具有标准方法。
[Serializable]
[CollectionDataContract]
public class GenericObservableCollection<T> : ObservableCollection<T>
{
public GenericObservableCollection() { }
public GenericObservableCollection(IEnumerable<T> collection)
: base(collection) { }
}
[Serializable]
[CollectionDataContract]
public class GenericRuleCollection : GenericObservableCollection<IRule>
{
public GenericRuleCollection() { }
public GenericRuleCollection(IEnumerable<IRule> collection)
: base(collection) { }
}
最初,一切都很好,但是后来 Entity Framework 进入图片时,我必须大量更改域设计,因为EF需要将ICollection<T>
公开用于映射。那时,我很困惑地保持最小的更改并适应EF,因为我是新手。
研究后,我遇到了一些很好的文章来处理这种情况。
- 将私人收集属性暴露于实体框架
- 映射但不暴露实体框架中的偶像
我在我的应用程序域中应用了相同的方法,创建ChildrenStorage
将ICollection<GenericRule>
作为EF的要求。
现在,我正在寻找一种智能而优雅的方法来保留我的收藏,即当添加和/或删除项目时,ChildrenStorage
和Children
集合在同步中。由于Children
集合是将通过UI修改的收集,因此我想跟踪对Children
进行的任何更改并希望同步ChildrenStorage
。
[Serializable]
[DataContract]
public abstract class GenericContainerRule : GenericRule
{
protected GenericContainerRule() : this(null)
{
ChildrenStorage = new List<GenericRule>();
}
protected GenericContainerRule(string name) : base(name)
{
ChildrenStorage = new List<GenericRule>();
}
public void AddChild(IRule rule)
{
ChildrenStorage.Add(rule as GenericRule);
_children = new GenericRuleCollection(ChildrenStorage);
OnPropertyChanged(nameof(Children));
}
public class OrMappings
{
public static Expression<Func<GenericContainerRule, ICollection<GenericRule>>> ChildrenAccessor = t => t.ChildrenStorage;
}
[DataMember]
protected ICollection<GenericRule> ChildrenStorage { get; set; }
private GenericRuleCollection _children;
public GenericRuleCollection Children => _children ?? (_children = new GenericRuleCollection(ChildrenStorage));
private GenericRuleCollection _children;
[DataMember]
public virtual GenericRuleCollection Children
{
get { return _children; }
private set { SetProperty(ref _children, value); }
}
}
我不认为我完全了解您在问什么,但是当您谈论"更改跟踪收藏"作为收藏包装纸时,这可能适合您。我目前正在将其用于我的最终学位项目。我在最后提到了它。
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
namespace Core
{
public interface IValidatableTrackingObject :
IRevertibleChangeTracking,
INotifyPropertyChanged
{
bool IsValid { get; }
}
public class ChangeTrackingCollection<T> : ObservableCollection<T>, IValidatableTrackingObject
where T : class, IValidatableTrackingObject
{
private IList<T> _originalCollection;
private ObservableCollection<T> _addedItems;
private ObservableCollection<T> _removedItems;
private ObservableCollection<T> _modifiedItems;
public ChangeTrackingCollection(IEnumerable<T> items)
: base(items)
{
_originalCollection = this.ToList();
AttachItemPropertyChangedHandler(_originalCollection);
_addedItems = new ObservableCollection<T>();
_removedItems = new ObservableCollection<T>();
_modifiedItems = new ObservableCollection<T>();
this.AddedItems = new ReadOnlyObservableCollection<T>(_addedItems);
this.RemovedItems = new ReadOnlyObservableCollection<T>(_removedItems);
this.ModifiedItems = new ReadOnlyObservableCollection<T>(_modifiedItems);
}
public ReadOnlyObservableCollection<T> AddedItems { get; private set; }
public ReadOnlyObservableCollection<T> RemovedItems { get; private set; }
public ReadOnlyObservableCollection<T> ModifiedItems { get; private set; }
public bool IsChanged => AddedItems.Count > 0
|| RemovedItems.Count > 0
|| ModifiedItems.Count > 0;
public bool IsValid => this.All(t => t.IsValid);
public void AcceptChanges()
{
_addedItems.Clear();
_removedItems.Clear();
_modifiedItems.Clear();
foreach (var item in this)
{
item.AcceptChanges();
}
_originalCollection = this.ToList();
OnPropertyChanged(new PropertyChangedEventArgs(nameof(IsChanged)));
}
public void RejectChanges()
{
foreach (var removedItem in _removedItems.ToList()) this.Add(removedItem);
foreach (var addedItem in _addedItems.ToList()) this.Remove(addedItem);
foreach (var modifiedItem in _modifiedItems.ToList()) modifiedItem.RejectChanges();
OnPropertyChanged(new PropertyChangedEventArgs(nameof(IsChanged)));
}
protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
{
var added = this.Where(current => _originalCollection.All(orig => orig != current));
var removed = _originalCollection.Where(orig => this.All(current => current != orig));
var modified = this.Except(added).Except(removed).Where(item => item.IsChanged).ToList();
AttachItemPropertyChangedHandler(added);
DetachItemPropertyChangedHandler(removed);
UpdateObservableCollection(_addedItems, added);
UpdateObservableCollection(_removedItems, removed);
UpdateObservableCollection(_modifiedItems, modified);
base.OnCollectionChanged(e);
OnPropertyChanged(new PropertyChangedEventArgs(nameof(IsChanged)));
OnPropertyChanged(new PropertyChangedEventArgs(nameof(IsValid)));
}
private void ItemPropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == nameof(IsValid))
{
OnPropertyChanged(new PropertyChangedEventArgs(nameof(IsValid)));
}
else
{
var item = (T)sender;
if (_addedItems.Contains(item))
{
return;
}
if (item.IsChanged)
{
if (!_modifiedItems.Contains(item))
{
_modifiedItems.Add(item);
}
}
else
{
if (_modifiedItems.Contains(item))
{
_modifiedItems.Remove(item);
}
}
OnPropertyChanged(new PropertyChangedEventArgs(nameof(IsChanged)));
}
}
private void AttachItemPropertyChangedHandler(IEnumerable<T> items)
{
foreach (var item in items)
{
item.PropertyChanged += ItemPropertyChanged;
}
}
private void DetachItemPropertyChangedHandler(IEnumerable<T> items)
{
foreach (var item in items)
{
item.PropertyChanged -= ItemPropertyChanged;
}
}
private void UpdateObservableCollection(ObservableCollection<T> collection, IEnumerable<T> items)
{
collection.Clear();
foreach (var item in items)
{
collection.Add(item);
}
}
}
}
https://github.com/pfc-acl-amg/gamapfc/blob/master/master/gamapfc/core/changetrackingcollection.cs