質問

I have a class that looks like the following

class ModelCollection : ObservableCollection<MyModel>
{
    public ModelCollection ()
    {
        this.CollectionChanged += (s, e) =>
        {
            if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
            {
                //do something
            }
            if(e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Remove)
            {
                //do something
            }
        };
    }
    //etc..
}

and the class MyModel

public class MyModel
{
    public int id {get;set;}
    public string Name {get;set;}
    public MyModel Parent {get;set;}
    public ModelCollection Descendants {get;set;}
    public ModelCollection Children {get;set;}
    //etc..
}

and in general i have a collection that have the root MyModel which has Children of type MyModel and those children have children of that type and it can reach up to 20 levels of nesting, so its basicly a tree structure.

as you can see in the ModelCollection i have an event listener, my question is is it ok to have this amount of event listeners considering that i have thousands of MyModel items and multiple nested levles per item, if not is there a better suggestion?

役に立ちましたか?

解決

You can use so match event listeners, but then you have to maintain it. In addition it can cause memory leaks.

Another option is to use INotifyPropertyChanged on all of your model objects, for example:

public class MyModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public int id { get {...} set { OnPropertyChanged("id"); } }
    public string Name { get {...} set { OnPropertyChanged("Name"); } }
    public MyModel Parent { get {...} set { OnPropertyChanged("Parent"); } }
    public ModelCollection Descendants { get {...} set { OnPropertyChanged("Descendants"); } }
    public ModelCollection Children { get {...} set { OnPropertyChanged("Children"); } }

    private void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = this.PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

Then you can use hierarchical data context and similar WPF's features which can utilize the INotifyPropertyChanged implementation without any work from your side.

Another option is to inherit DependencyObject and use DependencyProperties instead of simple properties, which WPF can utilize in similar ways. However I think DependencyObject may be an overkill in cases of non-UI elements, and INotifyPropertyChanged is preferred in such cases.

Update: I noticed you didn't mention that you need the ObservalbleCollection for UI reasons, so my comment about WPF may be redundant. Anyway I think that using INotifyPropertyChanged is anyway a good way to manage such things, and I think it is also the standard one.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top