Search code examples
c#dependency-injectionninjectfactory-pattern

Ninject.Extensions.Factory: cannot find binding


I have the following line in my Ninject.MVC RegisterServices:

kernel.Load(new NinjectBllModule());
kernel.Bind<IMembershipLogicFactory>().ToFactory();

and inside my Module:

 Bind<IUserLogic<LoginUser>>().To<UserLogic>();
 Bind<IRoleLogic<SimpleRole, LoginUser>>().To<RoleLogic>();

and my IMembershipLogicFactory:

public interface IMembershipLogicFactory
{
    IUserLogic<TUser> GetUserLogic<TUser>() where TUser : UserBase;
    IRoleLogic<TRole, TUser> GetRoleLogic<TRole, TUser>() where TRole : RoleBase<TUser> where TUser : UserBase<TRole>;
}

yet when I inject an IMembershipLogicFactory and call GetUserLogic() on it, I'm getting the following error:

Error activating IUserLogic{LoginUser}
No matching bindings are available, and the type is not self-bindable.
Activation path:
 1) Request for IUserLogic{LoginUser}

and I can't seem to find what I'm doing wrong.


Solution

  • The extension uses the convention that methods starting with Get resolve named bindings. GetUserLogic will therefore be translated into

    kernel.Get<IUserLogic<LoginUser>>("UserLogic");
    

    for which no binding is defined. Just use another method name such as CreateUserLogic