如果我有以下设置:
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()));
}
答案 0 :(得分:3)
container.Register(
Classes.FromThisAssembly()
.Where(Component.IsInTheSameNamespaceAs<IUsersQuery>())
.WithServiceDefaultInterfaces()
.LifestyleTransient()
);
The documentation详细介绍了所有选项。
答案 1 :(得分:-1)
我不确定Castle是否具有自动注册功能,但您可以使用反射来实现。
这似乎不是一项非常艰巨的任务:
1.找到名称以Query
结尾的所有接口2.根据您的规则检查是否有可用于此界面的类型
3.如果存在类型,则注册组件。