如何在Windsor中动态命名组件?

本文关键字:组件 动态 Windsor | 更新日期: 2023-09-27 18:16:15

我需要在Resolve方法被调用时动态命名。

我的注册:

container.Register(Component.For<IWorker>()
                                   .ImplementedBy<Worker>()
                                   .LifeStyle.Singleton);

我的示例解析过程:

container.Resolve<IWorker>("Singleton instance 1"); //Singleton instance 1 not defined but it can definable dynamically when Resolve called
container.Resolve<IWorker>("Singleton instance 2");
container.Resolve<IWorker>("Singleton instance 1"); // same instance with first resolved instance

如何在Windsor中动态命名组件?

如何在Windsor中动态命名组件?

听起来您需要一个自定义的生活方式——比如每个"键"(无论这个键是什么)一个实例。我会研究这种方法,而不是试图强迫一个预定义的生活方式来适应您的场景,或者在创建组件后命名组件。