かなり単純な質問だと思いますが、それに対するより良い解決策をまだ見つけることができません。そこで、この件について調査した後、専門家の意見を得るためにここでこの質問をすることを考えました.
基本的に、私は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 に適応することに混乱していました。
後で調査した後、このシナリオを扱っているいくつかの優れた記事に出くわしました.
EF の要件としてChildrenStorage保持するアプリケーション ドメインの作成にも同じアプローチを適用しました。ICollection<GenericRule>
現在、アイテムが追加および/または削除されたときに、コレクションChildrenStorageとコレクションの両方を同期させるためのスマートでエレガントなアプローチを探しています。Childrencollection は UI を介して変更されるものであるためChildren、変更を追跡し、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); }
}
}