简体   繁体   English

温莎注册单例组件并通过传递构造函数参数进行解析

[英]Windsor register singleton component and Resolve by passing constructor parameters

I have a class with bellow constructor: 我有一个波纹管构造函数的类:

Foo(FooType type)

the FooType is an Enum. FooType是一个枚举。 I register this class like this: 我像这样注册此类:

container.Register(
  Component.For<IFoo>()
           .ImplementedBy<Foo>()
           .LifestyleSingleton())

I need to have two instances of this class with difference FooType . 我需要具有差异FooType该类的两个实例。 I Resolve this type like bellow: 我像下面这样解决这种问题:

IFoo foo1 = container.Resolve<IFoo>(new { type = FooType.Type1 });
IFoo foo2 = container.Resolve<IFoo>(new { type = FooType.Type2 });

Are foo1 and foo2 same objects? foo1foo2相同的对象吗?

Answer is Yes , So, how I can register Foo as singleton and resolve two instance of it with difference FooType ? 答案是肯定的 ,因此,我如何将Foo注册为单例并使用不同的FooType解析它的两个实例?

I finally resolved my problem by converting IFoo to IFoo<T> and using multiple interface instead of Enum values. 我终于通过将IFoo转换为IFoo<T>并使用多个接口而不是Enum值解决了我的问题。 Now my code like this: 现在我的代码是这样的:

public interface IFooType
{ }

public interface IFooType1 : IFooType
{ }

public interface IFooType2 : IFooType
{ }

public interface IFoo<T>
    where T : IFooType
{
    string FooType { get; }
}

public class Foo<T> : IFoo<T>
    where T : IFooType
{
    public string FooType
    {
        get
        {
            return typeof(T).ToString();
        }
    }
}

And register and resolve Foo: 并注册并解决Foo:

container.Register(Component.For(typeof(IFoo<>))
                            .ImplementedBy(typeof(Foo<>))
                            .LifestyleSingleton());

var foo1 = container.Resolve<IFoo<IFooType1>>();
var foo2 = container.Resolve<IFoo<IFooType2>>();

Console.WriteLine("foo1 : " + foo1.FooType); // return foo1: IFooType1
Console.WriteLine("foo2 : " + foo2.FooType); // return foo1: IFooType2

This way resolved my problem, but I am not sure if it is the only way to do this? 这种方式解决了我的问题,但是我不确定这是否是唯一的方法吗?

You can't do this directly. 您不能直接这样做。 The singleton lifestyle means there is only one instance of your service, this is what singleton means. 单身人士的生活方式意味着您只有一个服务实例,这就是单身人士的意思。

To get multiple instances you can either change the lifestyle to transient (or possibly scoped) or register this service multiple times. 要获取多个实例,您可以将生活方式更改为临时(或可能是范围内的)或多次注册此服务。 To do this, you need a way to distinctly address each service instance. 为此,您需要一种方法来分别寻址每个服务实例。

In Windsor it is possible to register names services. Windsor ,可以注册名称服务。

container.Register(
    Component.For<IFoo>().ImplementedBy<Foo>().LifestyleSingleton().Named("Instance1),
    Component.For<IFoo>().ImplementedBy<Foo>().LifestyleSingleton().Named("Instance2)
);

you can then resolve this type by calling: 您可以通过以下方式解决此类型:

var intsance1 = container.Resolve("Instance1");
var intsance2 = container.Resolve("Instance2");

But because your services are singletons, it is not possible to pass the constructor parameter in each resolve, they will only be used once. 但是由于您的服务是单例的,因此无法在每个解析中传递构造函数参数,它们将仅使用一次。 In this case, it would probably be better set the constructor parameters while registering the services. 在这种情况下,最好在注册服务时设置构造函数参数。 You can do this by using DepensOn(...) 您可以使用DepensOn(...)

container.Register(
    Component.For<IFoo>()
             .ImplementedBy<Foo>()
             .LifestyleSingleton()
             .Named("Instance1)
             .DependsOn(new { type = FooType.Type1 }),
    Component.For<IFoo>()
             .ImplementedBy<Foo>()
             .LifestyleSingleton()
             .Named("Instance2)
             .DependsOn(new { type = FooType.Type2 })
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM