cbp
cbp

Reputation: 25638

Can't get NInject Conventions working

I am struggling to get NInject Conventions to work. I have the following code:

public class DataAccessInjectionModule : NInjectModule
{
    var scanner = new AssemblyScanner();
    scanner.From(new[] {
            "Domain.dll",
            "DataAccess.dll"
        });
    scanner.WhereTypeInheritsFrom(typeof(IRepository<>));
    scanner.BindWith<DefaultBindingGenerator>(); // I have also tried new GenericBindingGenerator(typeof(IRepository<>))
    scanner.InRequestScope();

    Kernel.Scan(scanner);
}

So basically I am trying to bind interfaces (such as IFooRepository) which are in Domain.dll, to the concrete class (such as FooRepository) that are in DataAccess.dll.

However, when I later try to fetch the class from the Kernel I get the error: No matching bindings are available, and the type is not self-bindable.

Is there something I am missing?

Upvotes: 0

Views: 381

Answers (1)

Remo Gloor
Remo Gloor

Reputation: 32725

The problem is that IFooRepository is not derived from IRepository<> but from IRepository<IFoo>. If you want to use the interface as condition you have to implement your own condition using Where(t => t.DoSomeReflectionMagicHere()) and do a bit of reflection magic. Another approach is to use the name as the condition e.g. bind all classes having Repository in its name.

Upvotes: 1

Related Questions