c# - CaSTLe Windsor自动注册多个接口(interface)及其对应的实现

标签 c# asp.net-mvc-3 castle-windsor

如果我有以下设置:

public interface IUsersQuery{}

public class UsersQuery : IUsersQuery {}

public interface ICompanyQuery{}

public class CompanyQuery : ICompanyQuery {}

是否可以一次性自动注册所有 IABCQuery 及其相应的实现 ABCQuery,而不是一一注册:

container.Regsiter(
  Component.For<ICompanyQuery>().ImplementedBy<CompanyQuery>(),
  Component.For<IUsersQuery>().ImplementedBy<UsersQuery>()
)

我在想也许如果我在界面上添加某种标记,

public interface IEnhancedQuery {}

public interface IUsersQuery : IEnhanceQuery {}

public interface ICompanyQuery : IEnhancedQuery {}

那么我也许可以做到这一点,但我很难想出通过 AllTypes 自动注册来注册它们的方法。

        container.Register(AllTypes.FromThisAssembly().BasedOn<IEnhancedQuery>());
        var iQueries = container.ResolveAll<IEnhancedQuery>();
        foreach (IEnhancedQuery p in iQueries)
        {
            var actualInterface = // how to get the actual interface type of p;

            // would the following work?
            container.Register(Component.For(actualInterface)
              .ImplementedBy(AllTypes.FromThisAssembly().BasedOn(actualInterface)
              .WithService.FirstInterface()));
        }

最佳答案

container.Register(
   Classes.FromThisAssembly()
      .Where(Component.IsInTheSameNamespaceAs<IUsersQuery>())
      .WithServiceDefaultInterfaces()
      .LifestyleTransient()
);

The documentation详细说明所有选项。

关于c# - CaSTLe Windsor自动注册多个接口(interface)及其对应的实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9321095/

相关文章:

c# - 比较两个自定义 LIST 对象

c# - 使用 Bouncy CaSTLe 为 DH key 交换生成 64 字节公钥

javascript - SWF 不在 IE9 中呈现(但在 Chrome/FF 中呈现)

asp.net-mvc - 哪里是 Simple Injector 等效于 StructureMap 的 ObjectFactory

.net - 温莎城堡 : Registering Multiple Types

.net - 将 MEF 与 NHibernate 和 CaSTLe Windsor 一起使用

c# - 是否可以在没有配置或 svc 文件的情况下使用 caSTLe windsor fluent 配置来配置 WCF 服务?

c# - 如何检索所有 RAS 连接?

c# - 在 C# 中轻松创建支持索引的属性

c# - ASP.NET-MVC3 中 "Self Validate Model"中的客户端验证