简体   繁体   English

在Unity Dependency Injection C#中注入通用接口的IEnumerable

[英]Inject an IEnumerable of generic interface in Unity Dependency Injection c#

I am working on a web API 2 application and using Unity dependency injection. 我正在使用Web API 2应用程序并使用Unity依赖项注入。

I have multiple types of filters: Name, Brand, Types ... 我有多种类型的过滤器:名称,品牌,类型...

I want to make an interface called: IFilterService and force every other class to implement it and then I call an IEnumerable of this interface and inject it with the correct type. 我想创建一个名为:IFilterService的接口,并强制其他所有类都实现它,然后调用此接口的IEnumerable并将其注入正确的类型。

the interface is: 界面是:

public interface IFilterService<T>
{
    bool CanHandle(FilterType type);

    Task<ServiceResult<T>> FilterAsync(T entity);
}

and the classes are like: 和类是这样的:

public class NameService : IFilterService<Name>
{
    public bool CanHandle(FacetType type)
    {
        return type == FacetType.Name;
    }

    public async Task<ServiceResult<Name>> FilterAsync(Name entity)
    {
      // Code
    }
}

the controller is like: 控制器就像:

public class FilterController
{
    private readonly IEnumerable<IFilterService> filters;

    public MediaController(IEnumerable<IFilterService> filters)
    {
        this.filters = filters;
    }

     public async Task<HttpResponseMessage> FilterAsync(FilterType type, Name entity)
     {
        foreach(var filter in this.filters.Where(x => x.CanHandle(type)))
        {
            filter.FilterAsync(entity); 
        }
        ....
    }
}

everything is working correctly: the only problem is with registering the interface and the classes in the Unity Dependency Injection. 一切工作正常:唯一的问题是在Unity Dependency Injection中注册接口和类。

container.RegisterType<IEnumerable<IFilterService>, IFilterService[] >(
                new ContainerControlledLifetimeManager());
container.RegisterType<IFilterService, NameService>("Name Service",
                new ContainerControlledLifetimeManager());

I am receiving this error: 我收到此错误:

Error CS0305 Using the generic type 'IFilterService' requires 1 type arguments 错误CS0305使用通用类型“ IFilterService”需要1个类型参数

The same code I've tried it but with a non-generic interface and works fine. 我尝试过的相同代码,但是具有非通用接口,可以正常工作。

how to fix the error? 如何解决错误? and a little explanation could be very useful. 并作一些解释可能非常有用。 thank you. 谢谢。

You have two options, the first is to register the specific filter type 您有两个选择,第一个是注册特定的过滤器类型

container.RegisterType<IEnumerable<IFilterService<Name>>, IFilterService<Name>[] >(
                new ContainerControlledLifetimeManager());
container.RegisterType<IFilterService<Name>, NameService>("Name Service",
                new ContainerControlledLifetimeManager());

Used like 使用像

public class FilterController
{
    private readonly IEnumerable<IFilterService<Name>> filters;

    public MediaController(IEnumerable<IFilterService<Name>> filters)
    {
        this.filters = filters;
    }

     public async Task<HttpResponseMessage> FilterAsync(FilterType type, Name entity)
     {
        foreach(var filter in this.filters.Where(x => x.CanHandle(type)))
        {
            filter.FilterAsync(entity); 
        }
        ....
    }
}

The second option is to make your interface non generic, but you keep the function generic 第二种选择是使您的接口非通用,但您保留该函数通用

public interface IFilterService
{
    bool CanHandle(FilterType type);

    Task<ServiceResult<T>> FilterAsync<T>(T entity);
}

public class NameService : IFilterService
{
    public bool CanHandle(FacetType type)
    {
        return type == FacetType.Name;
    }

    public Task<ServiceResult<T>> FilterAsync<T>(T entity)
    {
      if(!entity is Name)
          throw new NotSupportedException("The parameter 'entity' is not assignable to the type 'Name'");

        return FilterAsyncInternal((Name)entity);
    }

    private async Task<ServiceResult<Name>> FilterAsyncInternal(Name entity)
    {
        //code
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM