Created
July 15, 2015 15:20
-
-
Save michaeljbailey/353bc86e501e1404563e to your computer and use it in GitHub Desktop.
Provides extension method for analyzing the container for any warnings. If any come up, an exception is thrown.
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.Linq; | |
using SimpleInjector; | |
using SimpleInjector.Diagnostics; | |
public static class ContainerDiagnosticExtensions | |
{ | |
public static Registration GetRegistration<TService>(this Container container) | |
{ | |
return container.GetRegistration(typeof(TService)).Registration; | |
} | |
public static void Analyze(this Container container) | |
{ | |
var analysis = Analyzer.Analyze(container); | |
if (analysis.Any()) | |
{ | |
var groups = (from result in analysis | |
group result by result.Group into grouping | |
select new | |
{ | |
Group = grouping.Key, | |
Results = grouping.ToList() | |
}).ToList(); | |
var messages = from g in groups | |
let name = g.Group.Name | |
let formattedResults = (from result in g.Results | |
let formatted = FormatResult(result) | |
select formatted) | |
let message = string.Join(Environment.NewLine, formattedResults) | |
select string.Format("{0}{1}{0}{2}", Environment.NewLine, name, message); | |
throw new DiagnosticVerificationException(string.Join(Environment.NewLine, messages)); | |
} | |
} | |
private static string FormatResult(DiagnosticResult result) | |
{ | |
return string.Format("Type '{0}' has warning {1}:{2} - {3}", result.ServiceType, | |
result.DiagnosticType, Environment.NewLine, result.Description); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment