从Autofac容器中解析IEnumerable通用接口

时间:2016-06-14 00:57:30

标签: c# dependency-injection autofac

我不确定这是否可能,我看到其他一些帖子提出类似的问题,但没有一个得到满意的答案。

我想要做的是从Autofac解析具有不同泛型类型的接口集合。所以类的构造函数看起来像这样:

public class SomeClass<T> where T : class
{
    private readonly IEnumerable<ITestInterface<T>> _testInterfaces;

    public SomeClass(IEnumerable<ITestInterface<T>> testInterfaces)
    {
        _testInterfaces = testInterfaces;
    }
}

理想情况下,我希望能够像这样单独注册每个实例:

builder
    .RegisterType<ImplementationA>()
    .As<ITestInterface<A>>();

builder
    .RegisterType<ImplementationB>()
    .As<ITestInterface<B>>();

我尝试了RegisterGeneric等的各种组合,但是Enumerable一直空着。

任何帮助都将不胜感激。

2 个答案:

答案 0 :(得分:1)

在玩了继承之后,我能够解决这个问题。通用约束。我最终得到的解决方案如下:

基类/接口:

public abstract class BaseClass
{
    public abstract string IAM { get; }
}

public interface ITestInterface<out T> where T : BaseClass
{
    T GetSomething();
}

已实施的课程:

public class A : BaseClass
{
    public override string IAM => "I AM TYPE A";
}

public class AInterface : ITestInterface<A>
{
    public A GetSomething()
    {
        return new A();
    }
}

public class B : BaseClass
{
    public override string IAM => "I AM TYPE B";
}

public class BInterface : ITestInterface<B>
{
    public B GetSomething()
    {
        return new B();
    }
}

我们要解决的课程:

public interface ISomeClass
{
    void DoSomething();
}

public class SomeClass<T> : ISomeClass where T : BaseClass
{
    private readonly IEnumerable<ITestInterface<T>> _testInterfaces;

    public SomeClass(IEnumerable<ITestInterface<T>> testInterfaces)
    {
        _testInterfaces = testInterfaces;
    }

    public void DoSomething()
    {
        foreach (var t in _testInterfaces)
        {
            var something = t.GetSomething();

            Console.WriteLine(something.IAM);
        }
    }
}

最后,Autofac配置:

var builder = new ContainerBuilder();

builder
    .RegisterType<SomeClass<BaseClass>>()
    .AsSelf();

builder
    .RegisterType<AInterface>()
    .As<ITestInterface<BaseClass>>();

builder
    .RegisterType<BInterface>()
    .As<ITestInterface<BaseClass>>();

builder
    .RegisterType<SomeClass<BaseClass>>()
    .As<ISomeClass>();

var container = builder.Build();

var x = container.Resolve<ISomeClass>();

x.DoSomething();

输出:

  

我是A型

     

我是B型

希望这有助于将来。

答案 1 :(得分:0)

RegisterGeneric应该可以正常工作:

builder.RegisterType<TestImplementationA>()
        .As<ITestInterface<A>>();
builder.RegisterType<TestImplementationB>()
        .As<ITestInterface<B>>();
builder.RegisterGeneric(typeof(SomeClass<>))
       .As(typeof(ISomeClass<>));

builder.RegisterType<TestImplementationA>()
        .As<ITestInterface<A>>();
builder.RegisterType<TestImplementationB>()
        .As<ITestInterface<B>>();
builder.RegisterGeneric(typeof(SomeClass<>))
       .AsSelf();

您将在下面找到一份工作样本:

public interface ISomeClass<T> where T : class
{
    Int32 Count { get; }
}
public class SomeClass<T> : ISomeClass<T> where T : class
{
    private readonly IEnumerable<ITestInterface<T>> _testInterfaces;

    public SomeClass(IEnumerable<ITestInterface<T>> testInterfaces)
    {
        _testInterfaces = testInterfaces;
    }

    public Int32 Count
    {
        get
        {
            return this._testInterfaces.Count();
        }
    }
}

public interface ITestInterface {}
public interface ITestInterface<T> : ITestInterface { }
public class A { }
public class B { }
public class TestImplementationA : ITestInterface<A> { }
public class TestImplementationB : ITestInterface<B> { }

class Program
{
    static void Main(string[] args)
    {
        ContainerBuilder builder = new ContainerBuilder();
        builder.RegisterType<TestImplementationA>()
               .As<ITestInterface<A>>()
               .As<ITestInterface>();

        builder.RegisterType<TestImplementationB>()
               .As<ITestInterface<B>>()
               .As<ITestInterface>();

        builder.RegisterGeneric(typeof(SomeClass<>))
               .As(typeof(ISomeClass<>));

        IContainer container = builder.Build();

        var x = container.Resolve<ISomeClass<A>>();

        Console.WriteLine(x.Count);

        var z = container.Resolve<IEnumerable<ITestInterface>>();
    }
}