Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Castle Windsor多接口自动注册及其相应实现_C#_Asp.net Mvc 3_Castle Windsor - Fatal编程技术网

C# Castle Windsor多接口自动注册及其相应实现

C# Castle Windsor多接口自动注册及其相应实现,c#,asp.net-mvc-3,castle-windsor,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<I

如果我有以下设置:

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>()
)
然后我也许能做到,但我很难想出一个办法,通过所有类型的自动注册来注册它们

        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(AllTypes.fromthissembly().BasedOn());
var iQueries=container.ResolveAll();
foreach(iSeries中的IENHANCEDP查询)
{
var-actualInterface=//如何获取p的实际接口类型;
//下面的方法行吗?
容器寄存器(组件)For(实现接口)
.ImplementedBy(AllTypes.FromThisAssembly().BasedOn(现实界面)
.WithService.FirstInterface());
}

我不确定Castle是否有自动注册功能,但您可以使用反射实现一个

这似乎不是一项很难完成的任务:

1.查找名称以Query结尾的所有接口

2.根据您的规则检查此接口是否有可用的类型

3.如果类型存在,请注册组件。

container.register(
container.Register(
   Classes.FromThisAssembly()
      .Where(Component.IsInTheSameNamespaceAs<IUsersQuery>())
      .WithServiceDefaultInterfaces()
      .LifestyleTransient()
);
类。FromThisAssembly() .Where(Component.IsInTheSameNamespaceAs()) .WithServiceDefaultiInterfaces() .生活方式 );

详细说明所有选项。

让它使用这个:
container.Register(AllTypes.fromthissembly().BasedOnc.LifeStyle.Is(LifestyleType.Transient))然后是容器中的IUsersQuery-->UsersQuery。是否有任何方法可以根据它们的具体类型自动命名它们?
container.Register(
   Classes.FromThisAssembly()
      .Where(Component.IsInTheSameNamespaceAs<IUsersQuery>())
      .WithServiceDefaultInterfaces()
      .LifestyleTransient()
);