Suppose I have
public class Parent
{
private IList<Child> _children;
public void AddChild(Child child)
{
_children.Add(child);
child.OnSmthChanged += ParentSmthChangedHandler;
}
public void RemoveChild(Child child)
{
_children.Remove(child);
child.OnSmthChanged -= ParentSmthChangedHandler;
}
}
public class Child
{
public event EventHandler OnSmthChanged;
}
How do I make NHibernate to restore events when _children are loaded from database? Notice that:
- I don't want Parent bi-directional reference in Child
- _children is a field so I cannot intercept its "set"
- I don't like to use global events like OrderEvents.OrderChanged(order)
So, I think about intercepting NHibernate doing load of _children and there recursively add events. Two questions:
- How do I do this interception for _children loading? Intercepting all collections loading would be too ineffective I guess. Small note: I use Fluent NHibernate so specific advice would be nice.
- Something like OnAfterObjectCreated(Parent) would not work because I will not have access to the private field. Well, I can use reflection to setup if this is the only way.
- Is there a better way?