So, I have these classes that expose a collection of child objects.
I do not want other classes to add or remove objects from collections, because I need to connect to events of child objects, since they are added or deleted, I want to be able to perform additional processing. But I really like to easily manipulate generics inside.
I mentioned that this is a WPF application, so do I need INotifySupport?
The best I can come up with is something like this.
public class foo : INotifyPropertyChanged { protected List<ChildFoo> _Children = new List<ChildFoo>(); public foo() { } public void AddChild(ChildFoo newChild) { DoAttachLogic(newChild); _Children.Add(newChild); NotifyPropertyChange("Children"); } public void RemoveChild(ChildFoo oldChild) { DoRemoveLogic(oldChild); _Children.Remove(oldChild); NotifyPropertyChange("Children"); } public ChildFoo[] Children { get { return _Children.ToArray(); } } }
Are there any serious flaws in this design that I donβt see?
Each time access to the "Children" resource, we get the overhead of converting the list into an array.
Any advice on this would be great.
generics c # design-patterns architecture wpf
Joel barsotti
source share