I don't know if there is anything more recent about this issue (I searched and couldn't find it) - but after I implemented the suggestion I found that there was a danger of an infinite recursion - and you can guess how I found out.
So I decided to implement some protection against this and came up with the following code. It now appears to work without a problem, but I'd welcome any input about what I might have done wrong implementing the original idea, or any comments about why this might not be a good idea.
public class llbUtil
{
public static bool RecursiveIsDirty(IEntity argEntity)
{
List<IEntityCollection> visitedCollections = new List<IEntityCollection>();
List<IEntity> visitedEntities = new List<IEntity>();
return RecursiveIsDirty(argEntity, visitedCollections, visitedEntities);
}
private static bool RecursiveIsDirty(IEntity argEntity, List<IEntityCollection> visitedCollections, List<IEntity> visitedEntities)
{
if (visitedEntities.Contains(argEntity))
{
return false;
}
else
{
visitedEntities.Add(argEntity);
}
if (argEntity.IsDirty) return true;
foreach (IEntity entity in argEntity.GetDependentRelatedEntities())
{
if (entity != null)
{
if (RecursiveIsDirty(entity,visitedCollections, visitedEntities))
{
return true;
}
}
}
foreach (IEntity entity in argEntity.GetDependingRelatedEntities())
{
if (entity != null)
{
if (RecursiveIsDirty(entity, visitedCollections, visitedEntities))
{
return true;
}
}
}
foreach (IEntityCollection entityCollection in argEntity.GetMemberEntityCollections())
{
if (entityCollection != null)
{
if (RecursiveIsDirty(entityCollection, visitedCollections, visitedEntities))
{
return true;
}
}
}
return false;
}
public static bool RecursiveIsDirty(IEntityCollection argCollection)
{
List<IEntityCollection> visitedCollections = new List<IEntityCollection>();
List<IEntity> visitedEntities = new List<IEntity>();
return RecursiveIsDirty(argCollection, visitedCollections, visitedEntities);
}
private static bool RecursiveIsDirty(IEntityCollection argCollection, List<IEntityCollection> visitedCollections, List<IEntity> visitedEntities)
{
if (visitedCollections.Contains(argCollection))
{
return false;
}
else
{
visitedCollections.Add(argCollection);
}
if (argCollection.RemovedEntitiesTracker != null)
{
if (argCollection.RemovedEntitiesTracker.Count > 0)
return true;
}
foreach (IEntity myEntity in argCollection)
{
if (myEntity != null)
{
if (RecursiveIsDirty(myEntity, visitedCollections, visitedEntities))
return true;
}
}
return false;
}
}