Explain the mysterious world of IoC and automatic Dispose

有些话、适合烂在心里 提交于 2019-12-04 19:18:09

As a rule of thumb, the creator of a disposable object should also be the disposer of that same object. Thus, if you create an object graph from a custom IControllerFactory you should also use its ReleaseController for decommissioning.

Here's an example using Castle Windsor:

public class WindsorControllerFactory : DefaultControllerFactory
{
    private readonly IWindsorContainer container;

    public WindsorControllerFactory(IWindsorContainer container)
    {
        if (container == null)
        {
            throw new ArgumentNullException("container");
        }

        this.container = container;
    }

    protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
    {
        return (IController)this.container.Resolve(controllerType);
    }

    public override void ReleaseController(IController controller)
    {
        this.container.Release(controller);
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!