温莎城堡(Castle Windsor):有没有无需电话就可以确认注册的方法吗? [英] Castle Windsor: is there a way of validating registration without a resolve call?

查看:90
本文介绍了温莎城堡(Castle Windsor):有没有无需电话就可以确认注册的方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前对Castle Windsor注册的理解是,只能通过在根组件上调用Resolve来验证注册。但是由于Windsor的组件模型知道每个组件的依赖关系,因此应该有可能测试是否可以满足所有依赖关系而无需实际实例化任何东西。想要这样做的主要原因是要进行注册的单元测试,该测试不需要我存根在启动时调用外部资源的组件。

My current understanding of Castle Windsor registration is that one can only validate registration by calling Resolve on a root component. But since windsor's component model knows each component's dependencies, it should be possible to test that all dependencies can be satisfied without actually instantiating anything. The main reason for wanting to do this is to have a unit test for registration that doesn't require me to stub components that call external resources on start-up.

对于例。我有一个依赖于IChild的类Root:

For example. I have a class Root that has a dependency on IChild:

public class Root : IRoot
{
    private IChild child;

    public Root(IChild child)
    {
        this.child = child;
    }
}

如果我将Root注册为IRoot,但不要注册一个IChild。当我这样呼叫解决时:

If I register Root as IRoot, but don't register an IChild. When I call resolve like this:

var container = new WindsorContainer().Register(
    Component.For<IRoot>().ImplementedBy<Root>()
    );

container.Resolve<IRoot>();

我收到一个错误:

MyNamespace.Root is waiting for the following dependencies: 

Services: 
- MyNamespace.IChild which was not registered. 

是否存在以下内容:

container.TestResolve<IRoot>();

这将遍历依赖关系图并检查是否可以满足所有依赖关系,但实际上并没有实例化什么?

That would walk the dependency graph and check that all dependencies can be satisfied, but which doesn't actually instantiate anything?

推荐答案

好的,有可能。感谢KrzysztofKoźmic向我展示了如何。暂时性不明显,但是您可以使用Windsor的诊断子系统来提出潜在的注册问题。如果有任何配置错误的组件,我将汇总一个静态方法:

OK, It is possible. Thanks to Krzysztof Koźmic for showing me how. Not immediately obvious, but you can use Windsor's diagnostic subsystem to raise potential problems with registration. I've put together a little static method that throws if there are any misconfigured components:

private static void CheckForPotentiallyMisconfiguredComponents(IWindsorContainer container)
{
    var host = (IDiagnosticsHost)container.Kernel.GetSubSystem(SubSystemConstants.DiagnosticsKey);
    var diagnostics = host.GetDiagnostic<IPotentiallyMisconfiguredComponentsDiagnostic>();

    var handlers = diagnostics.Inspect();

    if (handlers.Any())
    {
        var message = new StringBuilder();
        var inspector = new DependencyInspector(message);

        foreach (IExposeDependencyInfo handler in handlers)
        {
            handler.ObtainDependencyDetails(inspector);
        }

        throw new MisconfiguredComponentException(message.ToString());
    }
}

您可以像这样使用它:

var container = new WindsorContainer().Register(
    Component.For<IRoot>().ImplementedBy<Root>()
    );

CheckForPotentiallyMisconfiguredComponents(container);

在这种情况下,我收到一条MisconfiguredComponentException并显示以下消息:

In this case I get a MisconfiguredComponentException with this message:

'WindsorSpikes.Root' is waiting for the following dependencies:
- Service 'WindsorSpikes.IChild' which was not registered.

WindsorSpikes.MisconfiguredComponentException:
'WindsorSpikes.Root' is waiting for the following dependencies:
- Service 'WindsorSpikes.IChild' which was not registered.

有关诊断子系统的更多详细信息,请参阅城堡文档:

See the castle documentation for more details on the diagnostic subsystem:

http://stw.castleproject.org/Default.aspx? Page = Debugger-views& NS = Windsor

这篇关于温莎城堡(Castle Windsor):有没有无需电话就可以确认注册的方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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