【发布时间】:2012-05-24 08:00:54
【问题描述】:
我正在尝试使用统一在我自己的应用程序中模仿 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();
我在将 IEnumerable 注入到我的 NicksSecondController 中失败了...有什么想法吗??
干杯,尼克
【问题讨论】:
标签: c# inversion-of-control unity-container ioc-container auto-registration