Skip to content

Instantly share code, notes, and snippets.

@orellabac
Created April 25, 2020 17:23
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save orellabac/21f0294948d14e0cbf614dea3d1889f6 to your computer and use it in GitHub Desktop.
Save orellabac/21f0294948d14e0cbf614dea3d1889f6 to your computer and use it in GitHub Desktop.
Some Code to Visualize your ASP.NET Core Dependencies
IServiceCollection _services;
public void ConfigureServices(IServiceCollection services)
{
_services = services;
// rest omitted
}
public void Configure(IApplicationBuilder app)//, IWebHostEnvironment env)
{
// omitted code
app.Map("/allservices", builder => builder.Run(async context =>
{
var sb = new StringBuilder();
var dependencies = new Dictionary<string,string>();
sb.AppendLine("<pre>");
sb.AppendLine("digraph Services {");
var services = _services.Select(svc => svc.ServiceType.ToString()).ToHashSet();
foreach (var svc in _services)
{
var serviceName = svc.ServiceType.ToString();
var implementationName = svc.ImplementationType?.ToString();
if (implementationName != null)
{
var implDependencies = svc.ImplementationType.GetConstructors().SelectMany(cons => cons.GetParameters()).Select(p => p.ParameterType.ToString()).Distinct().Where(x => services.Contains(x));
if (implDependencies.Count() > 0)
{
// Register Constructor dependendencies
foreach(var d in implDependencies)
{
dependencies.TryAdd(implementationName, d);
}
}
}
}
Action<string, string, string, IEnumerable<string>> printGroup = (label, cluster, color, group) =>
{
if (group.Count() > 0)
{
sb.AppendLine($" subgraph cluster_{cluster} {{");
sb.AppendLine(" style = filled;");
sb.AppendLine($" color = {color};");
sb.AppendLine(" node[style = filled, color = white];");
sb.AppendLine($" label = \"{label}\";");
foreach (var item in group)
{
sb.AppendLine($" \"{item}\"");
}
sb.AppendLine(" }");
}
};
var scopedGroup = _services.Where(s => s.Lifetime == ServiceLifetime.Scoped).Select(s => s.ServiceType.ToString());
var transientGroup = _services.Where(s => s.Lifetime == ServiceLifetime.Transient).Select(s => s.ServiceType.ToString());
printGroup("scoped", "0", "blue", scopedGroup);
printGroup("transient", "1", "lightgrey", transientGroup);
// Make interfaces different
sb.AppendLine();
sb.AppendLine();
sb.AppendLine(" node [color=green]");
var interfacesGroup = _services.Where(s => s.ServiceType.IsInterface).Select(s => s.ServiceType.ToString());
foreach (var @interface in interfacesGroup)
{
sb.AppendLine($" \"{@interface}\"");
}
sb.AppendLine();
sb.AppendLine(" node [color=black]");
var noninterfacesGroup = _services.Where(s => !s.ServiceType.IsInterface).Select(s => s.ServiceType.ToString());
foreach (var nonInterface in noninterfacesGroup)
{
sb.AppendLine($" \"{nonInterface}\"");
}
//Now print dependencies
foreach (var d in dependencies)
{
sb.AppendLine($" \"{d.Key}\" -> \"{d.Value}\"");
}
sb.AppendLine("}");
sb.AppendLine("</pre>");
// Ok ready. Now return all the graph
await context.Response.WriteAsync(sb.ToString());
}));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment