温莎城堡(Castle Windsor)等同于StructureMap ObjectFactory.With<>()。GetInstance<>>() [英] Castle Windsor equivalent of StructureMap ObjectFactory.With<>().GetInstance<>()

查看:46
本文介绍了温莎城堡(Castle Windsor)等同于StructureMap ObjectFactory.With<>()。GetInstance<>>()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用StructureMap可以进行解析,并强制容器使用解析时提供的特定依赖项实例,如下所示:

With StructureMap one can do a resolution and force the container to use specific dependency instance provided at the time of resolution like so:

ObjectFactory.With<ISomeDependency>(someDepedencyInstance).GetInstance<IServiceType>()

提供的实例将可以在整个解决方案链中使用,不仅可以作为IServiceType实现的直接依赖,还可以作为IServiceType实现的任何直接和间接依赖的依赖。

Provided instance will be used in whole resolution chain, that is not only as a direct dependency of IServiceType implementation but also as a dependency of any direct and indirect dependencies of IServiceType implementation.

我该如何在温莎城堡中做类似的事情?

我知道我可以提供IWindsorContainer.Resolve<>()重载的直接依赖项。 ,但我需要将此依赖关系提供给更深层次的东西。

I know I can provide a direct dependency with an overload of IWindsorContainer.Resolve<>(), but I need to provide this dependency to something deeper.

推荐答案

您可以将实例注册为给定组件的实现,例如

You can register an instance as the implementation of a given component like this:

container.Register(Component
    .For<ISomeDependency>()
    .Instance(someDependencyInstance));

这意味着每次您解析任何内容时,ISomeDependency都是已解决的一部分对象图,将使用 someDependencyInstance 实例。

This means that everytime you resolve anything and ISomeDependency is part of the resolved object graph, the someDependencyInstance instance will be used.

这是您想要的,还是我误解了这个问题?

It that what you want, or did I misunderstand the question?

根据其他信息,这是回答问题的新尝试。

Based on additional information, here's a new attempt at answering the question.

您应该能够为此使用容器层次结构。如果Windsor容器无法解析类型,则会询问其父项。这意味着您可以创建一个仅包含覆盖的子容器,然后要求该容器为您解析类型。

You should be able to use container hierarchies for this. If a Windsor container can't resolve a type, it'll ask its parent. This means that you can create a child container that contains only the override and then ask that container to resolve the type for you.

下面是一个示例:

var container = new WindsorContainer();
container.Register(Component
    .For<ISomeDependency>()
    .ImplementedBy<SomeDependency>());
container.Register(Component
    .For<IService>()
    .ImplementedBy<Service>());

var childContainer = new WindsorContainer();
childContainer.Register(Component
    .For<ISomeDependency>()
    .ImplementedBy<SomeOtherDependency>());

childContainer.Parent = container;

var service = childContainer.Resolve<IService>();

已解析的服务将包含一个实例

The resolved service will contain an instance of SomeOtherDependency and not SomeDependency.

请注意 childContainer 如何仅覆盖ISomeDependency的注册。所有其他注册均从父级使用。

Notice how childContainer only overrides the registration of ISomeDependency. All other registrations are used from the parent.

这篇关于温莎城堡(Castle Windsor)等同于StructureMap ObjectFactory.With&lt;&gt;()。GetInstance&lt;&gt;&gt;()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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