Ok. Some points:
- The GetChildObjects as you have it above won't work if you pass an EntityCollection.
- The IPrefetchPathElement is just useful when you design the PrefetchPath, it's no longer useful when you already fetch the entities. You have to inspect the already fetched elements regardless of the path.
To get things easier, there is an util class in LLBLGen runtime called ObjectGraphUtils, it has a method which returns a flat list of the graph, you can use that list to bind the event handler. It would work for the root entity, its m:1 related entities and its 1:n ones. I cooked the methods for you:
private void BindEvents(IEntity2 obj)
{
ObjectGraphUtils ogu = new ObjectGraphUtils();
List<IEntity2> flatList = ogu.ProduceTopologyOrderedList(obj);
foreach (var entity in flatList)
{
entity.PropertyChanged += iEntity2_PropertyChanged;
}
}
private void BindEvents(IEntityCollection2 coll)
{
foreach (IEntity2 entity in coll)
{
BindEvents(entity);
}
}
so, your code should look like:
public T Find(IEntity2 obj, List<IPrefetchPathElement2> prefetchPathElement2)
{
var entityType = (EntityType)obj.LLBLGenProEntityTypeValue;
using (var adapter = new DataAccessAdapter())
{
adapter.FetchEntity(obj, SetPerfetchPath(entityType, prefetchPathElement2));
BindEvents(obj);
return (T)obj;
}
}
public IList FindAll(IEntity2 obj, IRelationPredicateBucket filter, List<IPrefetchPathElement2> prefetchPathElement2)
{
var entityType = (EntityType)obj.LLBLGenProEntityTypeValue;
using (var adapter = new DataAccessAdapter())
{
var entityCollection = new EntityCollection(obj.GetEntityFactory());
adapter.FetchEntityCollection(entityCollection, filter, SetPerfetchPath(entityType, prefetchPathElement2));
BindEvents(entityCollection);
return entityCollection.GetList();
}
}
void iEntity2_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
Save(sender as CommonEntityBase);
}