如何基于标记界面配置生活方式
问题:
如何使用每个组件上是否存在标记界面来配置组件的生活方式?
我的规则是:
- 如果组件实现了IAutoInstalledComponent,那么它必须被注册;
- 如果组件实现了ISingletonComponent,那么组件的生命周期必须是Singleton,
- 如果一个组件实现了 IAutoInstalledComponent 但不是 ISingletonComponent 它将具有 PerWebRequest 生活方式
我在“Configure”方法的ConfigureDelegate 中给出了ComponentRegistration,但无法猜测也无法获取有关如何使用它的文档。
我当前的代码:
// a marker interface which I implement on my components by convention
// just to know which components to register
public interface IAutoInstalledComponent
{
}
// a marker interface to say that the lifestyle should be Singleton
public interface ISingletonComponent : IAutoInstalledComponent
{
}
// This is my unique installer
public class AutoInstaller : IWindsorInstaller
{
public void Install(IWindsorContainer container, IConfigurationStore store)
{
container.Register(AllTypes.FromThisAssembly()
.BasedOn<IAutoInstalledComponent>()
// this is what i want to be Singleton when the component (or service) implements ISingletonComponent
.Configure(c => c.LifeStyle.PerWebRequest)
);
}
}
上下文:
好的,如果您有时间,这里是我正在尝试做的事情的全局解释。我的目标是对当前程序集中的所有组件进行批量注册。但由于某些组件的初始化速度非常慢(并且完全是线程安全的),因此我只想创建它们的单个实例。
预先感谢您的帮助。
The question:
How can I configure the lifestyle of my components using the presence (or not) of a marker interface on each of them ?
My rules are:
- if a component implements IAutoInstalledComponent then it must be registered
- if the component implements ISingletonComponent then the lifecycle of the component must be Singleton
- if a component implements IAutoInstalledComponent but not ISingletonComponent it will be have a PerWebRequest lifestyle
I've seen an "If" method in the ComponentRegistration given in the ConfigureDelegate of the "Configure" method but could not guess nor get documentation about how to use it.
My current code:
// a marker interface which I implement on my components by convention
// just to know which components to register
public interface IAutoInstalledComponent
{
}
// a marker interface to say that the lifestyle should be Singleton
public interface ISingletonComponent : IAutoInstalledComponent
{
}
// This is my unique installer
public class AutoInstaller : IWindsorInstaller
{
public void Install(IWindsorContainer container, IConfigurationStore store)
{
container.Register(AllTypes.FromThisAssembly()
.BasedOn<IAutoInstalledComponent>()
// this is what i want to be Singleton when the component (or service) implements ISingletonComponent
.Configure(c => c.LifeStyle.PerWebRequest)
);
}
}
The context:
Ok, if you have time, here is global explanation of what I'm trying to do. My goal is to do a bulk registration of all the components in the current assembly. But since some components are really slow to initialize (and are totally threadsafe), I want to create only a single instance of them.
Thanks in advance for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以根据实现的接口使用Where()过滤组件
You may filter components using Where() according to implemented interfaces