Created
September 17, 2011 14:26
-
-
Save serra/1223983 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using QuickGraph; | |
using QuickGraph.Algorithms; | |
using QuickGraph.Graphviz; | |
using Spring.Context; | |
using Spring.Context.Support; | |
using Spring.Objects; | |
using Spring.Objects.Factory; | |
using Spring.Objects.Factory.Config; | |
using Spring.Objects.Factory.Support; | |
using Spring.Objects.Factory.Xml; | |
namespace q7446068 | |
{ | |
internal class Program | |
{ | |
private static void Main(string[] args) | |
{ | |
var configFile = "circular-reference.xml"; | |
IApplicationContext ctx = new XmlApplicationContext(configFile); | |
var myRegistry = GetRegistry(configFile); | |
var graph = GetAdjacencyGraph(myRegistry); | |
var output = GetDotRepresentation(graph); | |
Console.WriteLine("Created dot file: " + output); | |
if(!graph.IsDirectedAcyclicGraph()) | |
Console.WriteLine("Warning: the configuration contains circular references."); | |
Console.ReadLine(); | |
} | |
private static string GetDotRepresentation(AdjacencyGraph<string, Edge<string>> graph) | |
{ | |
var graphviz = new GraphvizAlgorithm<string, Edge<string>>(graph); | |
graphviz.FormatVertex += graphviz_FormatVertex; | |
var output = graphviz.Generate(new FileDotEngine(), "graph"); | |
return output; | |
} | |
static void graphviz_FormatVertex(object sender, FormatVertexEventArgs<string> e) | |
{ | |
e.VertexFormatter.Label = e.Vertex; | |
} | |
private static AdjacencyGraph<string, Edge<string>> GetAdjacencyGraph(DictionaryRegistry myRegistry) | |
{ | |
var graph = new QuickGraph.AdjacencyGraph<string, Edge<string>>(); | |
foreach (var definition in myRegistry.Definitions.Keys) | |
graph.AddVertex(definition); | |
foreach (var defId in graph.Vertices) | |
{ | |
var def = myRegistry.GetObjectDefinition(defId); | |
foreach (PropertyValue pv in def.PropertyValues.PropertyValues) | |
{ | |
var objRef = pv.Value as RuntimeObjectReference; | |
if (objRef == null) | |
continue; | |
graph.AddEdge(new Edge<string>(defId, objRef.ObjectName)); | |
} | |
} | |
return graph; | |
} | |
private static DictionaryRegistry GetRegistry(string configFile) | |
{ | |
var myRegistry = new DictionaryRegistry(); | |
var rdr = new XmlObjectDefinitionReader(myRegistry); | |
rdr.LoadObjectDefinitions(configFile); | |
return myRegistry; | |
} | |
} | |
public class ClassA : IObjectNameAware | |
{ | |
public ClassA MyOtherA { get; set; } | |
public string ObjectName { get; set; } | |
} | |
public class DictionaryRegistry : IObjectDefinitionRegistry | |
{ | |
private readonly Dictionary<string, IObjectDefinition> _definitions = new Dictionary<string, IObjectDefinition>(); | |
public Dictionary<string, IObjectDefinition> Definitions | |
{ | |
get { return _definitions; } | |
} | |
#region Implementation of IObjectDefinitionRegistry | |
public bool IsObjectNameInUse(string objectName) | |
{ | |
return Definitions.ContainsKey(objectName); | |
} | |
public string[] GetObjectDefinitionNames() | |
{ | |
return Definitions.Keys.ToArray(); | |
} | |
public bool ContainsObjectDefinition(string name) | |
{ | |
return Definitions.ContainsKey(name); | |
} | |
public IObjectDefinition GetObjectDefinition(string name) | |
{ | |
return Definitions[name]; | |
} | |
public void RegisterObjectDefinition(string name, IObjectDefinition definition) | |
{ | |
Definitions.Add(name, definition); | |
} | |
public string[] GetAliases(string name) | |
{ | |
throw new NotImplementedException(); | |
} | |
public void RegisterAlias(string name, string theAlias) | |
{ | |
throw new NotImplementedException(); | |
} | |
public int ObjectDefinitionCount | |
{ | |
get { return Definitions.Count; } | |
} | |
#endregion | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment