讲解IoC和自动Dispose的神秘世界

发布于 2024-11-17 12:28:17 字数 426 浏览 5 评论 0原文

我是 ASP.NET MVC 新手,我正在学习和试验企业设计模式:确实非常有趣且有用的东西!但我一直忽略了一些关于处置资源的概念。更具体地说,希望重点关注controllerFactory的机制,它在控制器构造函数中注入IFuncyRepository或IFuncyService的实现或要在控制器中使用的任何其他类型的“资源”(在我的例子中,我使用StructureMap作为IoC)。

我的问题是这些注入的资源在哪里/如果/如何被处置。

我遵循的指导书是《ASP.NET 设计模式》,到目前为止我无法得到任何关于这件事的解释,也没有任何线索,因为没有类是结构化实现 IDisposable 的。所以看起来资源处置任务是在某个地方自动完成的(也许在 IoC 中??)。 由于我无法理解这一点,因此我无法确定应用程序的性能,这会导致非常烦人的怀疑!

预先感谢任何会回复或分享我的担忧的人;)

I'm ASP.NET MVC newbye and I'm learning and experimenting Enterprise design patterns: very interesting and helpful things indeed! But I keep missing something about the concept of disposing resources. To be more specific want to focus on the mechanism of controllerFactory which injects in the controller constructor an implementation of IFuncyRepository or IFuncyService or anyother kind of "resource" to be used in the controller (In my case I'm using StructureMap as IoC).

My question is WHERE / IF / HOW these injected resources get Disposed.

The book I'm following as guideline is ASP.NET Design Patterns and until now I can't get any explaination about this thing and also no clue becouse no class is structured implementing IDisposable. So looks like the resource disposing task gets done automagically somewhere (maybe in the IoC??).
Since I can't understand this, I cannot be sure about the performance of my application which leads to very annoying doubts!

Thanks in advance to anyone will reply or share my concern ;)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黑色毁心梦 2024-11-24 12:28:17

根据经验,一次性对象的创建者也应该是同一对象的处置者。因此,如果您从自定义 IControllerFactory 创建对象图,您还应该使用其 ReleaseController 进行停用。

这是使用温莎城堡的示例:

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);
    }
}

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);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文