3

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

Richard
  • 603
  • 5
  • 14
  • These registrations are often very dynamic in nature, which means you have to run the application to check if its working (as you do with a unit test). NDepend however uses static code analysis; it doesn't run your code. I don't think you will succeed using NDepend (or any static code analysis tool) for this, or at least you will be abusing the tool and making yourself extraordinary hard. – Steven Nov 07 '13 at 19:36
  • But do leave your question open. I'm interested to hear what mr. NDepend (patrick) has to say about this. – Steven Nov 08 '13 at 07:56
  • 1
    Richard, as Steven suggested NDepend focuses on static analysis and cannot cop yet well with DI frameworks. However we have an open suggestion on our brand new User Voice page for future implementation of this feature, feel free to vote: http://ndepend.uservoice.com/forums/226344-ndepend-user-voice/suggestions/4558222-integrate-with-ioc-framewoks – Patrick from NDepend team Nov 08 '13 at 14:42

1 Answers1

0

From comments, I can see I will write my own integration tests but hope in future to have this supported in NDepend.

Richard
  • 603
  • 5
  • 14