如果我有以下设置:
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/