What is best way for editing EntityCollection in memory

Posts   
 
    
mohamed avatar
mohamed
User
Posts: 136
Joined: 10-Mar-2008
# Posted on: 06-May-2009 17:15:52   

Salam,

What is best way for editing EntityCollection in memory ?

I have this case,

An Entity that has collection child (1:n)

this collection bound to bindsource and GridView i have rule that GridView used for read only - Display Date only - , When editing row just get bound entity and bound it to controls like text box after finishing edit what is the best scenario for update Entity in entitycollection ??

Tools VS 2008 C# Window Application LLBL version 2.6 Adapter mode

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

Hi Mohamed -

Would you please be more specific with your question? I'm not sure I understand you correctly.

If I understand you correctly, you have a Entity (I'll call it RootEntity) with a related EntityCollection. And you are showing the EntityCollection in a DataGridView. And you are editing each Entity within the EntityCollection by binding to a Textbox.

What's the question?

Ryan

mohamed avatar
mohamed
User
Posts: 136
Joined: 10-Mar-2008
# Posted on: 06-May-2009 17:53:33   

Hi Ryan,

Yes.. Exactly, How can I update this edited entity in EntityCollection !!!

note:- i forget to explain thing I cloned entity (that comes form selected record in GridView for editing) before bind it to textboxes

rdhatch
User
Posts: 198
Joined: 03-Nov-2007
# Posted on: 06-May-2009 18:01:04   

I'd bet $100 that cloning is your problem.

When you clone an entity, you create a new instance. So, the EntityCollection does not contain the clone.

If you are looking for undo functionality - you can't just clone one entity - you need to clone the entire entity graph. And if the user hits cancel - you need to redatabind everything to the cloned graph.

Ryan

mohamed avatar
mohamed
User
Posts: 136
Joined: 10-Mar-2008
# Posted on: 06-May-2009 18:13:15   

I know that cloned entity case this problem

but the bottleneck is to update entitycollection with cloned entity ??

rdhatch
User
Posts: 198
Joined: 03-Nov-2007
# Posted on: 06-May-2009 18:29:33   

If you really wanted (not recommended) - yes, you could copy all fields over from the cloned entity (edited from textbox) to the EntityCollection's version of the entity.

Before copying, you can use these methods will help you find the correct entity within your EntityCollection:


    ''' <summary>
    ''' Returns an Entity instance from a Collection.  Can be used to match Cloned entities.
    ''' </summary>
    ''' <param name="myCollection"></param>
    ''' <param name="myEntity"></param>
    ''' <param name="RequireDifferentInstance"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function GetCollectionEntityByObject(ByRef myCollection As IEntityCollection, ByRef myEntity As IEntity, Optional ByVal RequireDifferentInstance As Boolean = False) As IEntity
        If myEntity Is Nothing Or myCollection Is Nothing Then Return Nothing

        Dim myCollectionEntity As IEntity = GetCollectionEntityByObject(myCollection, myEntity.ObjectID)

        'Same Instance? (not a clone)
        If RequireDifferentInstance = True Then
            If myCollectionEntity IsNot Nothing AndAlso ReferenceEquals(myEntity, myCollectionEntity) Then Return Nothing
        End If

        Return myCollectionEntity
    End Function



    Public Shared Function GetCollectionEntityByObject(ByRef myCollection As IEntityCollection, ByVal ObjectID As Guid) As IEntity
        For Each tmpEntity As IEntity In myCollection
            If tmpEntity.ObjectID = ObjectID Then
                Return tmpEntity
            End If
        Next
        Return Nothing
    End Function


    Public Shared Function GetCollectionEntityByPrimaryKey(ByRef myCollection As IEntityCollection, ByRef myEntity As IEntity) As IEntity
        If myEntity Is Nothing Or myCollection Is Nothing Then Return Nothing

        Dim myPrimaryKey As EntityField = GetPrimaryKeyField(myEntity)

        'Filter Collection by Primary Key
        Dim myFilter As New PredicateExpression(myPrimaryKey = myEntity.Fields(myPrimaryKey.Name).CurrentValue)

        Dim myIndexes As List(Of Integer) = myCollection.FindMatches(myFilter)
        If myIndexes.Count = 0 Then Return Nothing
        If myIndexes.Count > 1 Then Throw New Exception("Found more than one match in Collection (" & CType(myCollection, Object).GetType.ToString & ") for PrimaryKey: " & myEntity.Fields(myPrimaryKey.Name).CurrentValue)
        Return myCollection(myIndexes(0))
    End Function

Hope this helps!

Ryan D. Hatch

PS. Again, the best way to do Undo/Cancel is to clone the entire entity graph before editing. If the user hits Ok/Save - don't do anything because the current entity graph was updated. If the user hits Cancel/Undo - redatabind to the cloned entity graph (which will restore the EntityCollection to its previous state)

mohamed avatar
mohamed
User
Posts: 136
Joined: 10-Mar-2008
# Posted on: 06-May-2009 18:43:05   

oooh.. returned to way that i try to eccape from it anyhow thanks Ryan a lot this use-full...