1

我試圖模仿從烏節CMS一些東西,在我自己的應用程序中使用統一..試圖使用一個標記接口失敗的IEnumerable <>

好了,所以我試圖做的是...

可以說我有一個名爲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 ...任何想法??

Cheers,Nick

回答

3

開箱即用Unity只知道如何解析數組。看看TecX project on codeplex。它包含一個自定義擴展,教導容器處理IEnumerable<T>,IList<T>ICollection<T>。該代碼可在TecX.Unity(文件夾收藏)中找到。

相關問題