我正在尝试使用统一在我自己的应用程序中模仿 Orchard CMS 中的一些东西。
好吧,所以我想做的是这个......
假设我有一个名为 IDependency 的标记接口。
public interface IDependency{ }
然后我有几个接口挂在这个...
public interface ICustomerService : IDependency { }
public interface ICustomerRepository : IDependency { }
然后也上一些课...
public class CustomerService : ICustomerService {
public CustomerService(ICustomerRepository customerRepo){ }
}
public class SomOtherCustomerService : ICustomerService {
public CustomerService(ICustomerRepository customerRepo){ }
}
public class NicksController : Controller {
public NicksController(IEnumerable<ICustomerService> customerServices) { }
}
public class NicksSecondController : Controller {
public NicksSecondController(IEnumerable<ICustomerService> customerServices) { }
}
到目前为止我所拥有的..
var container = new UnityContainer();
container
.ConfigureAutoRegistration()
.ExcludeSystemAssemblies()
//.Include(If.Implements<IDependency>, Then.Register()
//.As<IDependency>().UsingLifetime<PerResolveLifetimeManager>())
.Include(t =>
typeof(IDependency).IsAssignableFrom(t), (type, builder) =>
{
builder.RegisterType(type);
foreach (var interfaceType in type.GetInterfaces()
.Where(itf => typeof(IDependency).IsAssignableFrom(itf))) {
builder = builder.RegisterType(interfaceType, type);
}
})
.ApplyAutoRegistration();
我在向我的 NicksSecondController 中注入 IEnumerable 时跌倒了……有什么想法吗?
干杯,尼克