Ninject different behaviour between Kernel.Get and

2019-05-15 13:20发布

What do I have:

public interface IBla
{
}

public class Bla1 : IBla
{
}

public class Bla : IBla
{
}

public class Consumer
{
    private readonly IBla[] _array;

    public Consumer(IBla[] array)
    {
        _array = array;
    }
}

public static class NinjectExtensions
{
      public class BindListExpression<TElement>
      {
          private readonly IKernel _kernel;
          private readonly List<Type> _types = new List<Type>();

          public BindListExpression(IKernel kernel)
          {
              _kernel = kernel;
          }

          public BindListExpression<TElement> ImplementedBy<T>() where T : TElement
          {
              var type = typeof(T);

              _kernel.Bind<T>().To(type);
              _types.Add(type);

              return this;
          }

          public void Bind()
          {
              Func<TElement[]> createObjects = () =>
              {
                  var sourceArray = new TElement[_types.Count];
                  for (var i = 0; i < _types.Count; i++)
                  {
                      var value = _kernel.Get(_types[i]);
                      sourceArray[i] = (TElement)value;
                  }
                  return sourceArray;
              };

              _kernel.Bind<TElement[]>().ToMethod(x => createObjects().ToArray());
              _kernel.Bind<List<TElement>>().ToMethod(x => (createObjects().ToList()));
              _kernel.Bind<IEnumerable<TElement>>().ToMethod(x => createObjects().ToList());
          }
      }
      public static BindListExpression<T> ListOf<T>(this IKernel kernel)
      {
          return new BindListExpression<T>(kernel);
      }
  }

Usage:

// Binds items in the given order as a list (Ninject does not guarantee the given order so I use this mechanism).
kernel.ListOf<IBla>()
    .ImplementedBy<Bla1>()
    .ImplementedBy<Bla>()
    .Bind();

var consumer = kernel.Get<Consumer>(); // result: consumer._array is empty?! --> what is imo wrong
var array = kernel.Get<IBla[]>(); // result: Bla1, Bla --> correct

Why doesn't Ninject produce the same result between a Get<IBla[]>() and constructor with a parameter IBla[]?

2条回答
干净又极端
2楼-- · 2019-05-15 13:46

It is possible to bind array dependencies in a specific order. You just need to register them in Ninject like this.

_kernel.Bind<Bla1>().ToSelf();
_kernel.Bind<Bla>().ToSelf();
_kernel.Bind<IConsumer>().To<Consumer>()
    .WithConstructorArgument("array", 
        new IBla[] { 
            _kernel.Get<Bla1>(), 
            _kernel.Get<Bla>() 
        });
查看更多
甜甜的少女心
3楼-- · 2019-05-15 14:02

With constructor injection, ninject translates the ctor parameter IBla[] to a IResolutionRoot.GetAll<IBla>().ToArray(). That's how support for multi-injection is implemented. So it's not possible for a ctor-request to result in a IResolutionRoot.Get<IBla[]>() - but it's still something you can do manually.

This is true for all collection-types which ninject translates to multi-injection (AFAIR array, IList, IEnumerable, but not ICollection).

I'd recommend using another collection interface (like ICollection) or collection implementation as constructor parameter. This will result in consistent behavior for ctor-injection and IResolutionRoot.Get calls.

查看更多
登录 后发表回答