I am using a utility class from QuickGraph called AdjacencyGraph that has a complex internal structure.
I do not care for all the internal properties of the complex class and I cant change it as its from a nuget package - at most in my database I am just concerned in storing an array of Dependency objects that can be used to reconstruct the AdacencyGraph:
public class Dependency
{
public Dependency(string source, string target)
{
this.Source = source;
this.Target = target;
}
public string Source { get; set; }
public string Target { get; set; }
}
On serialization, all i need to do is the following code:
void Serialize(Dependencies toSerialize)
{
var toBeStored = toSerialize.GetAllDependencies();
// write this to mongodb somehow
}
And deserialization build the Dependencies object:
Dependencies Deserialize(IEnumerable<Dependency> hydrateFrom)
{
var dependencies = new Dependencies();
foreach(var d in hydrateFrom)
{
dependencies.SetRequiresFor(d.Source, d.Target);
}
}
Question
How would I go about intercepting the serialization process and output?
Other Information
I have wrapped the AdjacencyGraph around in a class that lists all Dependency objects, and can accept a list of Dependency objects too.
class Dependencies
{
private AdjacencyGraph<string, Edge<string>> relationshipGraph = new AdjacencyGraph<string, Edge<string>>();
public void SetRequiresFor(SkuId on, SkuId requires)
{
var toAdd = new Edge<string>(on.Value, requires.Value);
this.relationshipGraph.AddVerticesAndEdge(toAdd);
}
public IEnumerable<Dependency> GetAllDependencies()
{
foreach(var edge in this.relationshipGraph.Edges)
{
yield return new Dependency(edge.Source, edge.Target);
}
}
}