本文介绍了每个解决方案的城堡自定义生活方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用温莎城堡,我有以下课程:

With Castle Windsor, let's say I have the following classes:

public class LowLevelComponent
{
}

public class HighLevelComponent
{
    readonly LowLevelComponent LowLevelComponent;

    public HighLevelComponent(LowLevelComponent lowLevelComponent)
    {
        LowLevelComponent = lowLevelComponent;
    }
}

public class ComponentBeingResolved
{
    readonly LowLevelComponent LowLevelComponent;
    readonly HighLevelComponent HighLevelComponent;

    public ComponentBeingResolved(LowLevelComponent lowLevelComponent,
                                  HighLevelComponent highLevelComponent)
    {
        LowLevelComponent = lowLevelComponent;
        HighLevelComponent = highLevelComponent;
    }
}

以最简单的方式注册:

container.Register(Component.For<LowLevelComponent>());
container.Register(Component.For<HighLevelComponent>());
container.Register(Component.For<ComponentBeingResolved>());

我想获得 LowLevelComponent 时,所有依赖项中都使用$ c>。

I'd like to get the same instance of LowLevelComponent used in all dependencies each time I call Resolve.

因此,在这些调用之后:

So, after these calls:

var instance1 = container.Resolve<ComponentBeingResolved>();
var instance2 = container.Resolve<ComponentBeingResolved>();

以下主张应为真:

instance1.LowLevelComponent == instance1.HighLevelComponent.LowLevelComponent
instance1.LowLevelComponent != instance2.LowLevelComponent
instance1.HighLevelComponent != instance2.HighLevelComponent

我也会接受您在做的一切错误,这就是您应该代替 作为答案:-)

I'll also take "you're doing everything wrong, this is what you should do instead" as an answer :-)

推荐答案

基于Mauricio的链接,我知道它是使用工厂工作的:

Based on Mauricio's link, I got it working using factories:

public interface IComponentFactory
{
    T Get<T>();
}

var container = new WindsorContainer();
container.AddFacility<TypedFactoryFacility>();
container.Register(Component.For<LowLevelComponent>()
         .LifeStyle.Custom<ContextualLifestyle>());
container.Register(Component.For<HighLevelComponent>()
         .LifeStyle.Custom<ContextualLifestyle>());
container.Register(Component.For<IComponentFactory>().AsFactory());
//Register the "context-root" component in a child container
var subContainer = new WindsorContainer();
subContainer.Register(Component.For<ComponentBeingResolved>()
            .LifeStyle.Transient);
container.AddChildContainer(subContainer);
container.Register(
    Component.For<ComponentBeingResolved>()
        .LifeStyle.Transient
        //Here's the magic
        .UsingFactoryMethod(
            () =>
                {
                    using (new ContainerContext(container))
                        return subContainer.Resolve<ComponentBeingResolved>();
                }));

用法:

var factory = container.Resolve<IComponentFactory>();
var instance1 = factory.Get<ComponentBeingResolved>();
var instance2 = factory.Get<ComponentBeingResolved>();

不知道这是个好方法还是丑陋的方法,但是效果很好。

Not sure if this is a good hack or an ugly one, but it works wonderfully.

这篇关于每个解决方案的城堡自定义生活方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 01:00