Castle Windsor equivalent of StructureMap ObjectFactory.With<>().GetInstance<>()
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>(someDepedenc开发者_Python百科yInstance).GetInstance<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.
How do I do something like that in Castle Windsor?
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 could use a named component and service overrides:
var container = new WindsorContainer();
container.Register(Component.For<ISomeDependency>()
.ImplementedBy<SomeDependency>());
container.Register(Component.For<ISomeDependency>()
.ImplementedBy<SomeOtherDependency>()
.Named("other"));
container.Register(Component.For<IService>()
.ImplementedBy<Service>()
.ServiceOverrides(ServiceOverride.ForKey("dep").Eq("other")));
See this article for more information on service overrides.
You can register an instance as the implementation of a given component like this:
container.Register(Component
.For<ISomeDependency>()
.Instance(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.
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.
Here's an example:
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.
Notice how childContainer only overrides the registration of ISomeDependency. All other registrations are used from the parent.
加载中,请稍侯......
精彩评论