如何基于标记界面配置生活方式

发布于 2024-11-05 20:14:47 字数 1435 浏览 1 评论 0原文

问题:

如何使用每个组件上是否存在标记界面来配置组件的生活方式?

我的规则是:

  • 如果组件实现了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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

生生漫 2024-11-12 20:14:47

您可以根据实现的接口使用Where()过滤组件

            container.Register
            (
                AllTypes.FromThisAssembly()
                .Where(c=> c.GetInterface(typeof(LifestyleUponInterface.InterfaceForSingleton).Name)!=null )
                .Configure( c=> c.LifeStyle.Singleton )
            );
            container.Register
            (
                AllTypes.FromThisAssembly()
                .Where(c => c.GetInterface(typeof(LifestyleUponInterface.InterfaceForTransient).Name) != null)
                //.If(Component.IsInNamespace("<yourNamespace>"))
                .Configure(c => c.LifeStyle.Transient)
            );

You may filter components using Where() according to implemented interfaces

            container.Register
            (
                AllTypes.FromThisAssembly()
                .Where(c=> c.GetInterface(typeof(LifestyleUponInterface.InterfaceForSingleton).Name)!=null )
                .Configure( c=> c.LifeStyle.Singleton )
            );
            container.Register
            (
                AllTypes.FromThisAssembly()
                .Where(c => c.GetInterface(typeof(LifestyleUponInterface.InterfaceForTransient).Name) != null)
                //.If(Component.IsInNamespace("<yourNamespace>"))
                .Configure(c => c.LifeStyle.Transient)
            );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文