说明国际奥委会和自动处置的神秘世界 [英] Explain the mysterious world of IoC and automatic Dispose

查看:142
本文介绍了说明国际奥委会和自动处置的神秘世界的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我ASP.NET MVC newbye和我学习和试验企业设计模式:的确是非常有趣和有用的东西!但我一直缺少的东西有关配置资源的概念。为了更具体要注重的ControllerFactory的机制注入控制器构造在控制器中使用IFuncyRepository或IFuncyService或电气特性的一种资源的实施(在我来说,我使用StructureMap如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).

我的问题是在哪里/ IF /如何将这些资源注入得到处理。

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

我下面为导向本书是ASP.NET设计模式,直至现在我不能得到任何的解释关于这件事情,也没有任何线索becouse没有阶级的结构实现了IDisposable。所以看起来像资源配置的任务被自动的地方(也许国际奥委会??)来完成。
因为我不明白这一点,我不能肯定我的应用程序的性能导致很烦人的疑惑!

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

推荐答案

作为一个经验法则,一次性对象的创建者也应该是同一个对象的处置。因此,如果从自定义创建IControllerFactory你也应该利用其ReleaseController退役的对象图。

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

这篇关于说明国际奥委会和自动处置的神秘世界的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆