In my ASP.net core application with Angular 2+ client, I work with a complicated object graph. In the object graph I have some objects with references to each other. I have a simplification included as an illustration below, where MainObject
represents the graph:
public class MainObject
{
public int Id { get; set; }
public ObjectProperty MyProperty { get; set; }
public List<GreenObject> GreenObjects { get; set; }
public List<YellowObject> YellowObjects { get; set; }
}
public class GreenObject
{
public int Id { get; set; }
public Guid Guid { get; set; }
public GreenObjectProperties MyProperty { get; set; }
public Guid? YellowObjectRef { get; set; }
}
public class YellowObject
{
public int Id { get; set; }
public Guid Guid { get; set; }
public YellowProperties MyProperty { get; set; }
public Guid? GreenObjectRef { get; set; }
}
Now I have the requirement to create clones of MainObject
, such that any relationship between objects in the object graph is maintained, but also such that it can be persisted. This means I need new UUID/GUID for the clone (to be able to be persisted in my DB), and continue to respect the relationship to the cloned objects and thus the newly created UUID/GUID's.
I am already using Automapper in my ASP.net application, so I was considering to use this library to create the clone. But I am willing to consider other libraries, or even to perform the cloning on the client side. AutoMapper documentation state that AutoMapper should not be used for cloning objects.
Question: Is there a common pattern or library to easily clone the object graph, creating new UUID/GUID's while keeping the references to the newly created objects/GUID's?
In the simplification I could easily iterate over the list of GreenObject
issue new GUID and then find the YellowObject
where GreenObjectRef
equals the original GUID and replace it with the new GUID (and vice versa for the list of YellowObject
). However my actual implementation is very complicated, and writing such a custom cloning method that is specific to the relationship would also be troublesome to maintain.