解析类,而不必使用温莎城堡将它们注册城堡、而不必、温莎

2023-09-02 10:38:15 作者:分手熱线

看看下面的无用的程序:

Take the following useless program:

class Program
{
    static void Main(string[] args)
    {
        IUnityContainer unityContainer = new UnityContainer();
        IWindsorContainer windsorContainer = new WindsorContainer();

        Program unityProgram = unityContainer.Resolve<Program>();
        Program castleProgram = windsorContainer.Resolve<Program>();
    }
}

在UnityContainer将返回我的计划,其中的温莎容器将抛出ComponentNotFoundException的一个实例。

The UnityContainer will return me an instance of Program, where as the Windsor container will throw a ComponentNotFoundException.

我可以看到双方的行为参数,不介意的话,我结束了,但是棱镜V2下降8(最迟在写本新闻时)依靠团结的行为在内部,要求没有登记类。

I can see arguments for both behaviours and don't mind which I end up with, however Prism V2 Drop 8 (the latest at time of writing) relies on the Unity behaviour internally, requesting classes that haven't been registered.

而不是寻找并注册所有这些类的棱镜我宁愿只是让温莎表现得像统一。我还没有发现任何关于谷歌帮我做到这一点(虽然我的术语可能是错误的)和温莎文档相当糟糕......

Rather than find and register all these classes for Prism I'd much rather just make Windsor behave like Unity. I haven't found anything on google to help me do this (although my terminology may be wrong) and the Windsor documentation is quite bad...

任何人都可以提出一个解决这个问题?

Can anyone suggest a solution to this problem?

推荐答案

温莎目前不支持,它的设计。其理由是,你不要让错误配置的对象,你应该明确地注册,你需要这样的类型。

Windsor currently does not support that, and it's by design. The reasoning is that you should explicitly register types you need so that you dont get misconfigured object.

然而,有一种可能性,即有将被添加一个钩子在不久的将来的某个时候创建​​非注册类型,因为这需要通过将WCF集成工具。 (编辑 - 它被添加在2.1 - 看看 ILazyComponentLoader S)

There is however a possibility that there will be added a hook to create non-registered type at some point in the near future, as this is needed by the WCF integration facility. (Edit - it was added in v2.1 - take a look at ILazyComponentLoaders)

反正,无论懒组件装载机,最好你能做的就是用流利的API批量注册的所有类型从装配符合标准所需的前期。这不是更code,你会在晚上睡得更好。

Anyway, regardless of lazy component loaders, the best you can do is to use fluent API to batch register all types from an assembly matching your needed criteria upfront. It's not much more code and you'll sleep better at nights.

使用懒加载器则只有在启动时有真的没有足够的信息(在你的构成根的),以确定哪些组件,您需要。

Use lazy loaders only if you have really not enough information at startup (in your composition root) to determine what components you'll need.