Best way to "AttachToGraph" ?

Posts   
 
    
HcD avatar
HcD
User
Posts: 214
Joined: 12-May-2005
# Posted on: 18-Feb-2016 01:20:52   

Hello,

I'm struggling a bit with ho I could best achieve an "AttachToGraph" functionality.

The purpose is to apply changes from one object graph to another one.

Imagine you have to object graphs of arbitrary depth, one with nodes A-B-C-D-E , and another one with nodes C-D-E-F-G. The nodes C-D-E are logically equal entities, but not the same object instances. (The graphs are the result of deserialized entities)

Now it's quite simple to "flatten" both graphs to a list using the ObjectGraphUtils.ProduceTopologyOrderedList and compute the "Added' and "RemovedEntities" For instance in our example the "removed entities" will be A & B (present in the first graph, but not in the second) and the "added entities" will be F & G

Now suppose I want to "patch" the original graph so it becomes logically equal to the second graph. For nodes A & B it's easy, I can just "DetachFromGraph". But I cannot easily "AttachToGraph" copies of the nodes F & G. I am now playing with methods like IEntity2.GetRelations, GetRelatedData, GetDependentRelations, etc .. but i feel like i'm going to complex with this.

Basically the bottomline is : when you have 2 entities (given as IEntity2), that are related by some relation, how can you attach them to eachother ?

Am I correct in thinking that I should investigate the GetRelations, and when I find a proper relation use the method SetRelatedEntity(Property) ? But that gets tricky for "hierarchical" entities, for instance a GroupEntity, that has a "ParentGroup" (meaning it also has "ChildGroups". It has then 2 relationships with GroupEntity...

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 25-Feb-2016 06:14:20   

HcD wrote:

Am I correct in thinking that I should investigate the GetRelations, and when I find a proper relation use the method SetRelatedEntity(Property) ? But that gets tricky for "hierarchical" entities, for instance a GroupEntity, that has a "ParentGroup" (meaning it also has "ChildGroups". It has then 2 relationships with GroupEntity...

I think that you are on the correct path here. Basically you should flatten the collections and then compare them recursively, and track the visited entities in a separate list. It's not easy I think given all the possible special cases.

Have you seen the CloneHelper? It's not the same, and it's much simpler than what you are trying to achieve, but it may gives you an idea on how to traverse the hierarchical graph.

David Elizondo | LLBLGen Support Team