kind_robot
kind_robot

Reputation: 2523

ninject inherited interface

I have two basic interfaces as below, one inheriting the other. I am binding them to implementations as below..

public interface IUnitOfWork { }
public interface IEventStoreUnitOfWork : IUnitOfWork { }

public class TransactionalHandler<TCommand, TCommandHandler> {
    public TransactionalHandler(IUnitOfWork unitOfWork) {
            // removed
    }
}

public class EventStoreUnitOfWork : IEventStoreUnitOfWork { }

Bindings...

Bind<IEventStoreUnitOfWork>().To<EventStoreUnitOfWork>();

Bind<TransactionalHandler<IDomainCommand, IDomainCommandHandler<IDomainCommand>>>()
    .ToSelf()
    .WithConstructorArgument("unitOfWork", Kernel.GetService(typeof(IEventStoreUnitOfWork)));

This is not working. The error is:

Error activating IUnitOfWork
No matching bindings are available, and the type is not self-bindable.
Activation path:
 2) Injection of dependency IUnitOfWork into parameter unitOfWork of constructor of type TransactionalHandler{CreateUserCmd, CreateUserCmdHandler}
 1) Request for TransactionalHandler{CreateUserCmd, CreateUserCmdHandler}

What is going on here? Clearly IUnitOfWork is inherited by IEventStoreUnitOfWork and it has a binding. I event tried this but I get the same error...

Bind<TransactionalHandler<IDomainCommand, IDomainCommandHandler<IDomainCommand>>>()
 .ToSelf()
 .WithConstructorArgument("unitOfWork", Kernel.GetService(typeof(IEventStoreUnitOfWork)) as IUnitOfWork);

Any ideas?

Thanks

Upvotes: 2

Views: 983

Answers (2)

Daniel Marbach
Daniel Marbach

Reputation: 21

Aaron jensen has a ninject fork which considers base types at github if you absolutly need that behavior

Upvotes: 2

Remo Gloor
Remo Gloor

Reputation: 32725

The type must match exactly. This means you need a binding for IUnitOfWork. Base types are not considered when searching for a dependency.

Upvotes: 1

Related Questions