迭代接口的实现

时间:2017-05-18 14:27:35

标签: c# dependency-injection interface

有没有办法循环接口的多个实现?我没有写任何DI代码示例,因为我不想限制基于DI框架的建议。可以是Autofac,Ninject,Unity等。适合任务。

我将使用ASP.Net Core,但我相信内置DI不允许多次实现。

所以,一个单一的界面。

public interface InterfaceA
{
    void MethodA();
}

许多实现所述接口并已注册的类。

public class Class1 : InterfaceA
{
    public void MethodA()
    {
      //Do something
    }
}

public class Class2 : InterfaceA
{
    public void MethodA()
    {
      //Do something
    }
}

这样的控制器

public TestContoller: Controller
{
    private readonly List<InterfaceA> interfaces;
    void TestController(List<InterfaceA> interfaces)
    {
        this.interfaces = interfaces;
    }

    IActionResult TestMethod()
    {
        Foreach(var implementation in interfaces)
        {
            implementation.MethodA();
        }
        return View();
    }
}

2 个答案:

答案 0 :(得分:0)

也许是这样的:

using SimpleInjector;
using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main (string[] args)
        {
            var container = new Container ();

            container.RegisterCollection<IMyInterface> (new[] { typeof (MyImplementationA), typeof (MyImplementationB) });

            var testController = container.GetInstance<TestController> ();

            testController.TestMethod ();

            Console.ReadKey ();
        }


        public interface IMyInterface
        {
            void DoSomething ();
        }


        public class MyImplementationA : IMyInterface
        {
            public void DoSomething () => Console.WriteLine ("A");
        }


        public class MyImplementationB : IMyInterface
        {
            public void DoSomething () => Console.WriteLine ("B");
        }


        public class TestController
        {
            private readonly IMyInterface[] instances;

            public TestController (InstancesFactory<IMyInterface> factory)
            {
                instances = factory.GetInstances ().ToArray ();
            }

            public void TestMethod ()
            {
                foreach (var instance in instances)
                {
                    instance.DoSomething ();
                }
            }
        }


        public class InstancesFactory<T> where T : class
        {
            private readonly Container container;

            public InstancesFactory (Container container)
            {
                this.container = container;
            }

            public IEnumerable<T> GetInstances ()
            {
                return container.GetAllInstances<T> ();
            }
        }
    }
}

link

答案 1 :(得分:0)

我意识到如果我使用Autofac,就没有什么需要做我需要的。

public IServiceProvider ConfigureServices(IServiceCollection services)
    {
        // Add framework services.
        services.AddMvc();

        var builder = new ContainerBuilder();

        //  builder.RegisterModule<DataModule>();
        builder.RegisterType<Class1>().As<InterfaceA>();
        builder.RegisterType<Class2>().As<InterfaceA>();

        builder.Populate(services);

        var container = builder.Build();
        return container.Resolve<IServiceProvider>();
    }

只需要更改为返回IServiceProvider而不是void的方法。 (标准代码)

这允许我将IEnumerable传递给我能够循环的控制器构造函数。