c# - CaSTLe Windsor Fluent 配置 : Is it possible to make a specific lifestyle for a given service without using the concrete implementation?

标签 c# .net inversion-of-control castle-windsor fluent-interface

我有一组服务,我想使用流畅的注册技术在 CaSTLe Windsor(版本 3.0 RC1)中注册。

我希望除了一个特定的人之外的所有人都使用短暂的生活方式,而我想成为单例,所以我这样做:

container.Register(AllTypes
                       .FromThisAssembly()
                       .InSameNamespaceAs<IMyService>()
                       .WithServiceDefaultInterfaces()
                       .ConfigureIf(s => s.Implementation == typeof(MyService),
                                    s => s.LifestyleSingleton(),
                                    s => s.LifestyleTransient()));

我遇到的问题是我在 ConfigureIf 的第一个参数中使用了 typeof(MyService),但如果我可以使用 IMyService确定它是否是单例(即不管实现是什么,它应该总是单例)。这有可能吗?

最佳答案

非常感谢 oleksii,他建议查看这个问题:How to determine if a type implements a specific generic interface type在对该问题的评论中,答案是执行以下操作:

container.Register(AllTypes
                   .FromThisAssembly()
                   .InSameNamespaceAs<IMyService>()
                   .WithServiceDefaultInterfaces()
                   .ConfigureIf(s => typeof(IMyService).IsAssignableFrom(s.Implementation),
                                s => s.LifestyleSingleton(),
                                s => s.LifestyleTransient()));

关于c# - CaSTLe Windsor Fluent 配置 : Is it possible to make a specific lifestyle for a given service without using the concrete implementation?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8507546/

相关文章:

c# - 如何获取复选框的状态

c# - 使用 maxchar 居中和拆分字符串

asp.net-mvc-3 - 通过 MVC 3 中的依赖注入(inject)学习控制反转

c# - 有谁知道在 C# 中实现原子操作的任何方法?

c# - LinearGradientBrush 导致非平滑渐变位图

c# - 为什么路径填充的数据绑定(bind)有效,但其任何子元素的任何绑定(bind)都不起作用?

c# - 具有动态端点映射的最小 Api 通用 CRUD

c# - 在任务取消时处理 CancellationTokenSource 的代码是否正确?

wpf - ViewModel是否可以通过ResourceDictionary URI持有View的间接引用?

c# - MVC 4 Web Api Controller 没有默认构造函数?