Copy entity with full graph

Posts   
 
    
exp2000
User
Posts: 68
Joined: 13-Apr-2006
# Posted on: 06-Sep-2006 20:27:54   

I have a entity that has some child entities and I want to make a copy of the whole graph, and then persist it with a different PK. I have some saved queries that belong to one user, and are shared amongst other users. User that is on the receiving side of the share, has option to convert those queires into their own and modify some settings. Is such copy functionality supported or do I have to serilize/deserialize whole graph and set IsNew to true for every entity in the graph?

bclubb
User
Posts: 934
Joined: 12-Feb-2004
# Posted on: 07-Sep-2006 03:11:13   

http://www.llblgen.com/tinyforum/Messages.aspx?ThreadID=1834&HighLight=1 It looks like the serializing/deserializing may be your best option. This thread goes into it in more detail.

exp2000
User
Posts: 68
Joined: 13-Apr-2006
# Posted on: 07-Sep-2006 20:02:04   

Here is what I have so far, and it seems to work pretty good. I am going to extend this to specify what properties that hold child entities to copy.

ManagerBase

Region "Clone"
        Protected Shared Function GenericCloneObject(ByVal obj As Object) As Object
            'Create a memory stream
            Dim ms As New MemoryStream
            Dim bf As New BinaryFormatter(Nothing, New StreamingContext(StreamingContextStates.Clone))
            'serialize object into the stream
            bf.Serialize(ms, obj)
            'position stream pointer back to first byte
            ms.Seek(0, SeekOrigin.Begin)
            'deserilize into another object
            GenericCloneObject = CType(bf.Deserialize(ms), IEntity2)
            'Release memory
            ms.Close()
        End Function
    
#End Region

QueryManagerBase : Inehrits ManagerBase


#Region "Clone & Copy "
        Public Shared Function CloneObject(ByVal obj As QueryEntity) As QueryEntity 
          Return CType(GenericCloneObject(obj),QueryEntity)
        End Function

        Public Shared Function CopyObject(ByVal entityToCopy As QueryEntity) As QueryEntity 
        
            Dim copiedEntity As QueryEntity = CloneObject(entityToCopy)

            ResetFieldsInCopy(copiedEntity)

            'now reset fields in all needed childentities
            Dim memberEntities As ArrayList = copiedEntity.GetMemberEntityCollections()

            For Each coll As EntityCollectionBase2 In memberEntities
            
                For Each subEntity As IEntity2 In coll
                    ResetFieldsInCopy(subEntity)
                Next
            Next

            Return copiedEntity

        End Function
        
        Private Shared Sub ResetFieldsInCopy(ByVal copiedEntity As IEntity2)
            copiedEntity.IsNew = True
            Dim fields As IEntityFields2 = copiedEntity.Fields
            For i As Integer = 0 To fields.Count - 1
                fields(i).IsChanged = True
            Next
            fields.IsDirty = True
        End Sub
        
#End Region


MatthewM
User
Posts: 78
Joined: 26-Jul-2006
# Posted on: 07-Sep-2006 22:34:19   

I had to do this too. The serialize/deserialize is such a great escape out of the problem. Then I "cheated" even more and used the ObjectGraphUtils.ProduceTopologyOrderedList (since Frans made it public just for moi wink ) to get all entities and just loop that flattened list to set isnew etc.

ObjectGraphUtils ogu = new ObjectGraphUtils();
List<IEntity> flatList = ogu.ProduceTopologyOrderedList(clonedEntity);

Answer
User
Posts: 363
Joined: 28-Jun-2004
# Posted on: 08-Sep-2006 04:19:45   

Frans made it public!??!? sweet!

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39927
Joined: 17-Aug-2003
# Posted on: 08-Sep-2006 09:34:26   

Answer wrote:

Frans made it public!??!? sweet!

Yes, a couple of days ago simple_smile Was an oversight simple_smile

Frans Bouma | Lead developer LLBLGen Pro