【问题标题】:Attempting to use a marker interface fails with IEnumerable<>尝试使用标记接口失败并出现 IEnumerable<>
【发布时间】: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


    【解决方案1】:

    开箱即用 Unity 只知道如何解析数组。看看TecX project on codeplex。它包含一个自定义扩展,该扩展教导容器处理IEnumerable&lt;T&gt;IList&lt;T&gt;ICollection&lt;T&gt;。代码可以在TecX.Unity(文件夹Collections)中找到。

    【讨论】:

      猜你喜欢
      • 2019-01-23
      • 2018-03-22
      • 2012-07-19
      • 2019-06-04
      • 1970-01-01
      • 1970-01-01
      • 2015-05-28
      • 2017-10-10
      • 1970-01-01
      相关资源
      最近更新 更多