如何通过Windsor通过接口和名称空间注册类? [英] How do I register classes by both interface and namespace with Windsor?

查看:50
本文介绍了如何通过Windsor通过接口和名称空间注册类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Castle.Windsor(3.2.0.0)

I'm trying to use Castle.Windsor (3.2.0.0) convention based registration, but can't seem to figure out how to register classes implementing a particular interface only in a particular namespace.

例如我真的很想写这样的东西:

e.g. what I really want to be able to write is something like this :

container.Register(Classes.FromThisAssembly()
                          .InNamespace("MyApp.EventHandlers")
                          .BasedOn(typeof(IHandlesEvent<>))
                          .WithServiceAllInterfaces()

但是我得到一个警告,似乎暗示这实际上将要做的是在 EventHandlers 名称空间中注册所有内容,然后在实现 IHandlesEvent<> 的当前程序集中注册所有内容>.

But I get a warning that seems to imply what this will really do is register everything in the EventHandlers namespace and then everything in the current assembly that implements IHandlesEvent<>.

如果我运行该应用程序,确实确实会发生这种情况.我不希望所有实现该接口的东西都被注册(例如,某些实现类是Sagas,需要手动跟踪),并且我真的不希望该名称空间中的所有东西都被注册.

If I run the application this does indeed seem to be what happens. I don't want everything that implements that interface to be registered (for example, some of the implementing classes are Sagas, which need to be manually tracked) and I don't really want everything in that namespace registered.

我真的不想单独注册事件处理程序,但是我无法从Windsor文档中看到如何按照惯例进行所需的操作.有可能吗?

I really don't want to register the event handlers individually, but I can't see from the Windsor documentation how to do what I need by convention. Is it possible?

推荐答案

我也很惊讶,但是我可以观察到Castle 3.2的行为. BasedOn 发出警告,称它将重新初始化注册:这是我的示例代码:

I'm surprised too, but I could observe the behavior on Castle 3.2. BasedOn pushed a warning saying that it would reinitialize the registration: here is my sample code:

namespace WindsorTest
{
    public interface IHandlesEvent {}

    public interface IDontWantToBeRegistered {}

    namespace Select
    {
        public class SelectClass : IHandlesEvent { }
        public class DontRegisterMe : IDontWantToBeRegistered { }
    }

    namespace DontSelect
    {
        public class DontSelectClass: IHandlesEvent {}
    }


    internal class Program
    {
        private static void Main(string[] args)
        {
            var container = new WindsorContainer();
            container.Register(Classes.FromThisAssembly()
                .InNamespace("WindsorTest.Select")
                .BasedOn<IHandlesEvent>()
                .WithServiceAllInterfaces()
                );
            foreach (var handler in container.ResolveAll<IHandlesEvent>())
            {
                Console.WriteLine(handler.GetType().Name);
            }

            foreach (var handler in container.ResolveAll<IDontWantToBeRegistered>())
            {
                Console.WriteLine(handler.GetType().Name);
            }
            Console.ReadLine();
        }
    }
}

它在运行时输出 DontSelectClass .

但是,我找到了一种方法从注册的基类开始,然后再使用命名空间对其进行优化.只需使用:

However I found a way to start with the base class for your registration and refine it with the namespace afterwards. Just use:

container.Register(Classes.FromThisAssembly()
    .BasedOn<IHandlesEvent>()
    .If(t => t.Namespace == "WindsorTest.Select")
    .WithServiceAllInterfaces()
    );

这篇关于如何通过Windsor通过接口和名称空间注册类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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