Check if entity contains dirtry collections

Posts   
 
    
jayrdub
User
Posts: 14
Joined: 02-Apr-2009
# Posted on: 16-Jun-2009 20:44:40   

Is there a way to determine if an entity has dirty child collections without manually walking through all of them?

If so, does this method account for child collections that contain other collections.

I'm using the 2.6 adapter .net 3.5.

Thank you.

rdhatch
User
Posts: 198
Joined: 03-Nov-2007
# Posted on: 17-Jun-2009 00:33:28   

Hi Jay -

Here's how I do a Recursive isDirty. The Simplest method is to check the entire EntityGraph (Parents, Siblings, as well as Children).

However, as you see below, I have implemented complex, true Recursive isDirty. Hope this helps!

Ryan

#Region "RecursiveIsDirty"

    Public Enum RecursiveIsDirtyMode
        RecursiveChildrenAndSiblings
        RecursiveChildren
        EntireGraph
    End Enum

    'This works great.  It also tests for Entities scheduled to be deleted in the .RemovedEntitiesTracker.
    Public Shared Function RecursiveIsDirty(ByVal argEntity As IEntity, Optional ByVal argRecursiveIsDirtyMode As RecursiveIsDirtyMode = RecursiveIsDirtyMode.RecursiveChildrenAndSiblings) As Boolean

        'Entity Dirty?
        If argEntity.IsDirty Then Return True

        'Mode = Entire Graph
        If argRecursiveIsDirtyMode = RecursiveIsDirtyMode.EntireGraph Then
            Dim utils As New ObjectGraphUtils()
            Dim myList As List(Of IEntity) = utils.ProduceTopologyOrderedList(argEntity)
            For Each myEntity As IEntity In myList
                If myEntity.IsDirty = True Then Return True
            Next
        End If

        'Mode = RecursiveChildren/RecursiveChildrenSiblings
        If argRecursiveIsDirtyMode = RecursiveIsDirtyMode.RecursiveChildren Or argRecursiveIsDirtyMode = RecursiveIsDirtyMode.RecursiveChildrenAndSiblings Then
            For Each myChildCollection As IEntityCollection In GetChildEntities(argEntity, True, False, False).Values
                If myChildCollection IsNot Nothing AndAlso RecursiveIsDirty(myChildCollection, argRecursiveIsDirtyMode) Then Return True
            Next
        End If

        'Mode = RecursiveChildrenSiblings
        If argRecursiveIsDirtyMode = RecursiveIsDirtyMode.RecursiveChildrenAndSiblings Then
            For Each mySibling As IEntity In GetSiblingEntities(argEntity, True).Values
                If mySibling IsNot Nothing AndAlso RecursiveIsDirty(mySibling, argRecursiveIsDirtyMode) Then Return True
            Next
        End If

        Return False
    End Function

    Public Shared Function RecursiveIsDirty(ByVal argCollection As IEntityCollection, Optional ByVal argRecursiveIsDirtyMode As RecursiveIsDirtyMode = RecursiveIsDirtyMode.RecursiveChildrenAndSiblings) As Boolean
        'Check for Entities in DeleteTracker
        If argCollection.RemovedEntitiesTracker IsNot Nothing AndAlso argCollection.RemovedEntitiesTracker.Count > 0 Then
            Return True
        End If

        'Loop through Collection
        For Each myEntity As IEntity In argCollection
            If myEntity IsNot Nothing AndAlso RecursiveIsDirty(myEntity, argRecursiveIsDirtyMode) Then Return True
        Next
        Return False
    End Function

#End Region
Angus
User
Posts: 44
Joined: 08-Jul-2005
# Posted on: 17-Feb-2011 17:56:05   

Hi Jay. I know this is an old post, but with .Net 3 and newer, I have really started to like the Ling queries. I also have a method that I can generically add to every entity that will do a deep 'IsDirty' check. It's was 3 lines of code but I reduced it to 1.

Here it is


    public bool IsEntityDirty()
        {
            return new ObjectGraphUtils().ProduceTopologyOrderedList(this).Any(entity => entity.IsDirty || entity.IsNew);
        }


Here it is as three lines of code


var graphUtils = new ObjectGraphUtils();
            var list = graphUtils.ProduceTopologyOrderedList(this);
            return list.Any(entity => entity.IsDirty || entity.IsNew);

I hope you or someone else may find that useful.

--Chris

MTrinder
User
Posts: 1461
Joined: 08-Oct-2008
# Posted on: 17-Feb-2011 20:16:02   

Thanks for the update Angus - neat solution. You could also declare it as an extension method to EntityBase in a seperate partial class to avoid having to modify the templates or add it to each entity.

Matt

Angus
User
Posts: 44
Joined: 08-Jul-2005
# Posted on: 17-Feb-2011 20:27:27   

MTrinder wrote:

Thanks for the update Angus - neat solution. You could also declare it as an extension method to EntityBase in a seperate partial class to avoid having to modify the templates or add it to each entity.

Matt

Great Idea Matt. I think I will do that.