Ninject.Extensions.Factory:找不到绑定

时间:2012-10-02 08:34:42

标签: c# dependency-injection ninject factory-pattern

我的Ninject.MVC RegisterServices中有以下行:

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

并在我的模块中:

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

和我的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>;
}

当我注入一个IMembershipLogicFactory并在其上调用GetUserLogic()时,我收到以下错误:

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

我似乎无法找到我做错的事。

1 个答案:

答案 0 :(得分:4)

扩展使用以Get开头的方法解析命名绑定的约定。因此,GetUserLogic将被翻译为

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

没有定义绑定。只需使用其他方法名称,例如CreateUserLogic