Castle Fluent 注册:无法覆盖由“AllTypes”注册的类型与“组件”

发布于 2024-09-24 07:39:46 字数 2204 浏览 0 评论 0原文

在 DLL 内部,我们定义了两个类(“Class1”和“Class2”),它们继承自接口(“IInterface”)和基类(“BaseClass”)。

我们正在使用 Castle Windsor 的 Fluent Registration API ( http://using.castleproject .org/display/IoC/Fluent+Registration+API )自动将从“BaseClass”(在该 DLL 内)继承的所有类注册到各自的接口。

对于特定的个性化,我们(从今天开始)使用了“castle.xml”文件,它覆盖(使用“组件”标签)接口和具体类(由 Fluent Registration Api 注册)之间的关联。我们在 WindsorContainer 的构造函数中加载该 xml 文件。

代码是这样的:

        //container's initialization:
        var resource = new FileResource("Castle.xml");
        var interpreter = new XmlInterpreter(resource);
        var container = new WindsorContainer(interpreter);
        container.AddFacility<TypedFactoryFacility>();

        //...

        //automatic type registration:
        container.Register(
            AllTypes
                .FromAssemblyContaining<BaseClass>()
                .BasedOn<BaseClass>()
                .WithService.Select(
                    (t1, t2) => t1.GetInterfaces()
                                    .Except(new[] {typeof (IDisposable)})
                                    .Union(new[] {t1}))
                .Configure(a => a.Named(a.ServiceType.Name)
                                    .LifeStyle.Transient)
                .AllowMultipleMatches()
            );

默认情况下,如果我们向 Castle 询问 IInterface 对象,我们会得到“Class1”;要获取“Class2”,我们必须在“Castle.xml”文件中指定它。

今天,我试图摆脱 castle.xml,在流畅的配置中指定“Component”指令(在“AllTypes”指令之前):

        container.Register(
            Component
                .For<IInterface>()
                .ImplementedBy<Class2>()
                .LifeStyle.Transient);

...但我们仍然得到一个 Class1 对象,就好像“AllTypes” “ fluid 指令覆盖了“Component”指令(这很奇怪,因为 xml 文件中的“component”指令有效)。

我做错了什么?

编辑:我通过键名称访问组件,“.Named()”解决了问题(感谢 Krzysztof):

        container.Register(
            Component
                .For<IInterface>()
                .ImplementedBy<Class2>()
                .Named(typeof(IInterface).Name)
                .LifeStyle.Transient);

Inside a DLL, we've defined two classes ("Class1" and "Class2") which inherit from an interface ("IInterface") and a base class ("BaseClass").

We're using the Castle Windsor's Fluent Registration API ( http://using.castleproject.org/display/IoC/Fluent+Registration+API ) to automatically register all the classes inheriting from "BaseClass" (inside that DLL) to their respective interfaces.

For specific personalization, we've used (since today) a "castle.xml" file, which overrides (with "component" tags) the associations between interfaces and concrete classes (registered by the Fluent Registration Api). We load that xml file inside the WindsorContainer's constructor.

The code is something like this:

        //container's initialization:
        var resource = new FileResource("Castle.xml");
        var interpreter = new XmlInterpreter(resource);
        var container = new WindsorContainer(interpreter);
        container.AddFacility<TypedFactoryFacility>();

        //...

        //automatic type registration:
        container.Register(
            AllTypes
                .FromAssemblyContaining<BaseClass>()
                .BasedOn<BaseClass>()
                .WithService.Select(
                    (t1, t2) => t1.GetInterfaces()
                                    .Except(new[] {typeof (IDisposable)})
                                    .Union(new[] {t1}))
                .Configure(a => a.Named(a.ServiceType.Name)
                                    .LifeStyle.Transient)
                .AllowMultipleMatches()
            );

By default, if we ask Castle for IInterface object, we get "Class1"; to obtain "Class2", we have to specify it inside the "Castle.xml" file.

Today, I've tried to get rid of the castle.xml, specifying the "Component" directives inside the fluent configuration (BEFORE the "AllTypes" directive):

        container.Register(
            Component
                .For<IInterface>()
                .ImplementedBy<Class2>()
                .LifeStyle.Transient);

... but we still get a Class1 object, as if the "AllTypes" fluent directive overrode the "Component" one (and it's strange, 'cos the "component" directive inside the xml file works).

What am I doing wrong?

EDIT: I was accessing to the component by key name, ".Named()" solved the problem (thanks to Krzysztof):

        container.Register(
            Component
                .For<IInterface>()
                .ImplementedBy<Class2>()
                .Named(typeof(IInterface).Name)
                .LifeStyle.Transient);

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

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

发布评论

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

评论(1

つ低調成傷 2024-10-01 07:39:46

为了避免问题看起来没有答案,答案就在上面的评论中。

Just so that the question does not appear as unanswered, the answer is in the comments above.

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