From comments, I can see I will write my own integration tests but hope in future to have this supported in NDepend.
How to Create a Custom NDepend Rule to Check DI Registrations
Question
we are using Autofac as our container for dependency injection. We would like to check various things using NDepend to ensure our DI is set up properly and not being misused (we have a very large solution).
In unit testing, I might take an approach:
private static IEnumerable<TestCaseData> TestCases
{
get
{
return from registration in Container.Value
.ComponentRegistry.Registrations
from service in registration.Services
where service is TypedService
orderby service.Description
select new TestCaseData(registration, service)
.SetName(service.Description);
}
}
Then:
[Test]
[TestCaseSource("TestCases")]
public void CanBeResolved(
IComponentRegistration componentRegistration,
TypedService typedService)
{
using (var scope = Container.Value.BeginLifetimeScope())
scope.Resolve(typedService.ServiceType);
}
How can I create a custom rule within NDepend to ensure that all appropriate types are registered with the Autofac container?
Thanks, Richard
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow