0

我认为这是一个相当简单的问题,但我仍然无法找到更好的解决方案。所以在研究了这个问题之后,我想在这里问这个问题以获得专家意见。

基本上,我正在研究 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) { }
}

最初,一切都很好,但后来当实体框架出现时,我不得不彻底改变域设计,因为 EF 需要公开ICollection<T>映射。那时,我很困惑保持最小的变化并适应 EF,因为我是新手。

后来经过研究,我遇到了一些处理这种情况的好文章。

我在我的应用程序域中应用了相同的方法来创建ChildrenStorage作为ICollection<GenericRule>EF 的要求。

现在我正在寻找一种聪明而优雅的方法来保持我的两个集合,即在添加和/或删除项目时ChildrenStorage与集合同步。Children由于Childrencollection 将通过 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); }
    }
}
4

1 回答 1

0

我认为我不完全理解您的要求,但是当您将“更改跟踪集合”作为集合包装器谈论时,这个可能适合您。我目前正在将它用于我的最终学位项目。我在最后放了一个链接。

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/GamaPFC/Core/ChangeTrackingCollection.cs

于 2017-06-28T18:13:52.853 回答