温莎城堡:自动注册在构造函数中具有依赖性的类型 [英] Castle Windsor: auto-register types that have dependency in constructor

查看:53
本文介绍了温莎城堡:自动注册在构造函数中具有依赖性的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到的大多数关于温莎城堡自动注册类型的示例均源自某些IFoo。但是,我经常有一些简单的组件(服务),它们仅在构造函数中需要IFoo:

Most of the examples I see for Castle Windsor auto-register type that derive from some IFoo. However, I often have simple components (services) that just required IFoo in the constructor:

public class Service
{
  public Service(Service2 service, IFoo foo) {}
}
public class Service2
{
  public Service2(IFoo foo) {}
}

我如何使Windsor自动注册/识别它们?我看到两种方式:

How do I make Windsor automatically register/recognize them? I see two ways:


  1. 浏览汇编中的所有类型,获取它们的构造函数,检查构造函数的任何参数是否在Windsor中注册。并注册。问题是服务可以依赖Service2-> IFoo ...我要 CONTAINER 来做到这一点,而不是我。

  2. 以某种方式扩展了Windsor(设施等) -我不是这里的专家),因此上述情况发生在解决调用期间-因此,我不需要浏览所有程序集中的所有无意义的类型。

  1. Browse all types in assembly, get their constructors, check if any of the constructor's parameter is registered in Windsor... and register it. The problem is that Service can depend on Service2 -> IFoo... I want CONTAINER to do this, not me.
  2. Somehow extend Windsor (facility, etc - I'm not expert here) so that the above happens during "Resolve" call - so that I don't need to browse all the meaningless types in all assemblies.

但是,在两种情况下,我都需要问温莎类型T是您的依赖项-是直接还是间接地通过嵌套构造函数?。

In both cases, however, I'll need a way to ask Windsor "Is type T your dependency - directly or indirectly via nested constructor?".

我真的很讨厌重复,所以我不明白为什么我需要手动注册Windsor已经知道的东西。

I really hate duplication, so I don't see why I need to manually register something that Windsor does already know about.

更新:这是代码这似乎有效。我真的不是温莎专家,所以我不确定代码是否正确。

UPDATE: here's the code that seems to work. I'm really not expert in Windsor so I'm not sure if the code is OK.

     var dependencies = container.Kernel.GetAssignableHandlers(typeof(object));
     foreach (var type in typeof(ViewModel<>).Assembly.GetTypes().Where(x => x.Name.EndsWith("ViewModel")))
     {
        var ctorArgs = type.GetConstructors().SelectMany(x => x.GetParameters());
        var canBeResolved = dependencies.Any(dep => ctorArgs.Any(a => 
                     a.ParameterType.IsAssignableFrom(dep.ComponentModel.Service)));
        if (canBeResolved)
           container.AddComponent("viewModel" + type.Name, type);
     }

但是,我最好让温莎(或我的扩展名)在解决过程中这样做时间,因为我认为浏览 ALL 类型有点过大,即使我使用名称过滤器将其缩小。

However, I would better have Windsor (or my extension) do this during resolve-time, because brosing ALL types is a bit overkill in my opinion, even though I narrow it with name filter.

推荐答案

如果您选择的是选项2,则作品中会有一个新的扩展点可以执行此操作。它尚未在主干中(您可以从讨论组下载补丁,将其与主干集成并自行重建)。
不过它将在Windsor v2.1中使用。

If you're for option 2, there is a new extension point in works that does just that. It's not in the trunk yet (you can download the patch from discussion group, integrate it with the trunk and rebuild it yourself). It will be in Windsor v2.1 though.

现在您可以做到这一点,像您展示的那样进行预先注册或开发某种形式符合惯例,例如将所有类型都放在公共命名空间中。

For now you can do either that, do the up-front registration like you showed or develop some kind of convention, like put all the types in common namespace.

这篇关于温莎城堡:自动注册在构造函数中具有依赖性的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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