We have a WPF application, let’s say I have a Department object, in it I have an ObservableCollection of Courses, inside each Course I have an ObservableCollection of Teachers, and inside each Teacher I have an ObservableCollection of Student:
Department.cs
—- ObservableCollection Courses
Course.cs
—- ObservableCollection Teachers
Teacher.cs
—- ObservableCollection Students
Student.cs
—- Name, Age, etc
These 4 classes all implemented INotifyPropertyChanged, they are working fine by themselves, what I want to achieve is whenever anything inside the Department object graph is changed, whether it was a student’s age get changed, or a new course added or a teacher removed, anything at all, I want to know about it, ideally via an event like DepartmentChanged (and ideally this only raise once), so I can call some other long running action.
I have a hard time coming up with a clean way to do it. I understand that INotifyPropertyChanged only track property change of a class and ObservationCollection has no idea if one of its items has changed something.
So far I tried nested loop like (pseudo code):
foreach (var course in d.Courses){
foreach (var teacher in course.Teachers){
foreach (var student in teacher.Students){
((INotifyPropertyChanged)student).PropertyChanged += ...
}
teacher.Students.CollectionChanged += ...
}
}
But as can imagine, this created lots of PropertyChanged events since every property subscribe to that, and the action I wanted to do in Department ended up being executed many many times, which is bad.
I then tried using a timer (each of our class has an IsDirty() to determine if the object has changed) with interval set to 500:
if(department != null && department.IsDirty()){
//call some long running Action here
}
While this seems to work, somehow I feel this approach is wrong, I’d really like to avoid timer if possible, I would love to hear other suggestions and ideas!
Thanks,
This is just a quick idea, without a timer, can you implement IsDirty event on the Course, & Teacher classes?
For example, Teacher, managers on PropertyChanged internally (subscribes/unsubscribes properly when Student collection changes).
When any of its students have a valid change, all it has to do is raise IsDirty event, which all have student obj as a param.
Course will listen and capture teacher.IsDirty event, and will raise its own course.IsDirty where CourseEventArgs = new CourseEventArgs will have a Course, Teacher with Student that has dirty fields.
In the Department class you listen to Course IsDirty event and parse the args…
This idea, seems to do the same thing that you have, but not really, because it handles any PropertyChanged only on the lowest level then takes use of your own events to bubble necessary info to the top. Which separates top classes from Student’s PropertyChanged behavioral implementations as well as extensive chatter..
Also if on the student level, you make changes (through UI, and post them, click save or whatever) then you can control when IsDirty is being raised, maybe only once.