Castle Windsor - 瞬态实例的查找方法注入

发布于 2024-09-29 13:12:35 字数 982 浏览 4 评论 0原文

简短的问题:
Castle Windsor 是否有类似于 Spring.Net 的“查找方法注入”的功能,可以从 XML 进行配置,从而提供从容器获取瞬态实例的能力,而无需类知道 IoC 容器?

长问题:
我是 Spring/Spring.Net 的长期用户,我一直在尝试 Castle Windsor,尝试将项目移植到它上面。 Spring.Net 有一个“查找方法注入”的概念(来自 Spring 文档)...

查找方法注入是容器重写容器管理对象上的方法,以返回在容器中查找另一个命名对象的结果的能力。查找通常涉及原型对象,如上一节中描述的场景所示。 Spring 框架通过动态生成一个子类来实现此方法注入,该子类使用 System.Reflection.Emit 命名空间中的类覆盖该方法。

这意味着,如果我有以下内容...

public class SomeTransient
{
    // ... I have dependencies that need to be filled by IoC container
}

public class SomeClass
{
    public virtual void Work()
    {
        var o = CreateTransient();
    }

    public virtual SomeTransient CreateTransient() { }
}

我可以指示 Spring 重写 CreateTransient 方法,并让该方法在每次调用该方法时返回一个新的容器创建的瞬态实例(其依赖项已初始化)。 其独特之处在于,它不需要直接链接到 Spring 框架(例如,SomeClass 不必实现特定的接口)。

Castle Windsor 中是否有类似的东西可以通过 XML 来完成此任务?

(我最终将放弃 XML 配置,但目前我只是想让它运行)

The short question:
Does Castle Windsor have something similar to Spring.Net's "Lookup Method Injection" that can be configured from XML, which provides the ability to fetch transient instances from the container without the class being aware of the IoC container?

The long question:
I'm a long time Spring/Spring.Net user and I have been experimenting with Castle Windsor, by trying to port a project over to it. Spring.Net has a concept of "Lookup Method Injection" which (from the Spring docs)...

Lookup method injection is the ability of the container to override methods on container managed objects, to return the result of looking up another named object in the container. The lookup typically involves a prototype object as in the scenario described in the preceding section. The Spring framework implements this method injection by a dynamically generating a subclass overriding the method using the classes in the System.Reflection.Emit namespace.

What this means is, If I had the following...

public class SomeTransient
{
    // ... I have dependencies that need to be filled by IoC container
}

public class SomeClass
{
    public virtual void Work()
    {
        var o = CreateTransient();
    }

    public virtual SomeTransient CreateTransient() { }
}

I can instruct Spring to override the CreateTransient method, and have that method return a new container created transient instance (with it's dependencies initialized) each time the method is called.
The unique part of this is, it doesn't require direct links to the Spring Framework (eg. SomeClass doesn't have to implement a specific interface).

Is there something similar in Castle Windsor to accomplish this via XML?

(I will eventually move away from XML config, but at the moment I'm just trying to get it running)

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

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

发布评论

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

评论(1

誰ツ都不明白 2024-10-06 13:12:35

城堡有更好的东西;类型工厂。
您甚至还可以注入委托!
http://stw.castleproject.org/Windsor.Typed -Factory-Facility-delegate-based-factories.ashx

更好,因为它不依赖于动态生成代码,而且看起来更干净。

它看起来更干净,因为该类不依赖于某人重写该方法。如果没有子类化,就不可能测试这个类。

如果你真的想做这样的事情,我希望:

public abstract class SomeClass
{
  public abstract SomeTransient CreateTransient();
}

但是......再次感觉不对。

编辑2

Unity 2 支持此类委托工厂;您可以在这里阅读更多内容:
http://www.truewill.net/myblog/index.php /2010/05/06/unity_2_0_combining_injectionfactory_and

感谢@eiximenis

Castle has something better; Typed Factories.
You can also inject even a delegate!
http://stw.castleproject.org/Windsor.Typed-Factory-Facility-delegate-based-factories.ashx

It is better because it does not depend on dynamically generation code, and it looks much more cleaner.

It looks much more cleaner because the class doesn't depend on someone overriding that method. It is impossible to test this class without subclassing.

If you really want to do something like this, i would expect:

public abstract class SomeClass
{
  public abstract SomeTransient CreateTransient();
}

but... again it doesn't feel right.

Edit 2

Unity 2 support these kind of delegate factories; you can read more here:
http://www.truewill.net/myblog/index.php/2010/05/06/unity_2_0_combining_injectionfactory_and

thanks to @eiximenis

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