4

Suppose I have this scenario:

using System.Linq;
using NUnit.Framework;
public interface IChannel {
   void Write(double value);
}

public class Channel: IChannel {
   private readonly int channelNumber;
   public Channel(int channelNumber) {
      Requires.That(channelNumber >= 0, "channelNumber >= 0");
      this.channelNumber = channelNumber;
   }
   private int calls;
   public void Write(double value) {
      System.Console.WriteLine("{0} wrote on channel {1} [#{2}]", value.ToString(), channelNumber, ++calls);
   }
}

public interface IService {
   void Do();
}

public class ServicePiGreek: IService {
   private readonly IChannel channel;
   public ServicePiGreek(IChannel channel) {
      Requires.IsNotNull(channel, "channel");
      this.channel = channel;
   }
   public void Do() {
      channel.Write(3.14);
   }
}
public class ServiceEuler: IService {
   private readonly IChannel channel;
   public ServiceEuler(IChannel channel) {
      Requires.IsNotNull(channel, "channel");
      this.channel = channel;
   }
   public void Do() {
      channel.Write(2.71);
   }
}

So I would create two ServicePiGreek with channel 0 and 1 and a ServiceEuler with channel 0:

[TestFixture]
public class Tests {
   [Test]public void without_simpleinjector() {
      var ch0 = new Channel(0);
      var s0 = new ServicePiGreek(ch0);
      var s1 = new ServicePiGreek(new Channel(1));
      var s2 = new ServiceEuler(ch0);
      s0.Do();
      s1.Do();
      s2.Do();
   }

I thought of this:

   [Test]public void with_simpleinjector() {
      SimpleInjector.Container container = new SimpleInjector.Container();
      container.RegisterAll(new Channel(0), new Channel(1));
      container.RegisterAll(GetAllServices(container));

      foreach (var service in container.GetAllInstances()) {
         service.Do();
      } 
   }

   private System.Collections.Generic.IEnumerable GetAllServices(SimpleInjector.Container container) {
      yield return new ServicePiGreek(container.GetAllInstances().ElementAt(1));
      yield return new ServicePiGreek(container.GetAllInstances().ElementAt(0));
      yield return new ServiceEuler(container.GetAllInstances().ElementAt(0));
   }

Does anyone have any better ideas on how to accomplish this?

4

1 回答 1

4

您的用例不是通常的用例,因为您在同一个列表中多次(作为瞬态)具有相同的实现,并且需要用IChannel接口的不同实现填充它。

我无法查看您的设计,但您现在如何注册类型是有道理的。您注册了一个动态 IEnumerable,它在迭代时回调容器。很高兴看到您正在使用 Simple Injector 的新功能之一通过索引获取项目,ElementAt这是一个 O(1) 操作,因为返回的集合实现了IList<T>.

您可以执行以下操作以使代码更具可读性:

private IEnumerable<IService> GetAllServices(Container container) {
  var channels = container.GetAllInstances<IChannel>();
  yield return new ServicePiGreek(channels.ElementAt(0));
  yield return new ServicePiGreek(channels.ElementAt(1));
  yield return new ServiceEuler(channels.ElementAt(0));

}

或者当IService实现可以是单例时,您可以执行以下操作:

var container = new SimpleInjector.Container();

var blueChannel = new Channel(0);
var redChannel = new Channel(1);

container.RegisterAll<IService>(
    new ServicePiGreek(blueChannel),
    new ServicePiGreek(redChannel),
    new ServiceEuler(blueChannel),  
);

foreach (var service in container.GetAllInstances<IService>()) {
    service.Do();
}

您可以将其抽象到工厂后面,而不是按索引请求元素,例如:

interface IChannelProvider
{
    IChannel GetBlueChannel();
    IChannel GetRedChannel();
}

但是,这是否有效取决于您的情况。

于 2013-05-10T14:45:58.890 回答