为NinjectControllerFactory实现IDisposable

时间:2014-07-09 11:06:06

标签: asp.net-mvc ninject

如何为我的Ninject实现IDisposable?

我正在附加代码。我是新的,这就是为什么有什么不知所措,请解释我。

public class NinjectControllerFactory : DefaultControllerFactory
{
    private IKernel ninjectKernel;
    public NinjectControllerFactory()
    {
        ninjectKernel = new StandardKernel();
        AddBindings();
    }

    protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
    {
        return controllerType == null
          ? null
          : (IController)ninjectKernel.Get(controllerType);
    }

    private void AddBindings()
    {
        ninjectKernel.Bind<IProductRepository>().To<EFProductRepository>();
        }
}

1 个答案:

答案 0 :(得分:0)

如果您要问的是如何处理一次性控制器,您需要在工厂中实施此方法:

public override void ReleaseController(IController controller)
{
    var disposableController = controller as IDisposable;

    if (disposableController != null)
    {
        disposableController.Dispose();
    }
}