bind to property always return null

馋奶兔 提交于 2019-12-25 09:59:33

问题


I am trying to bind a repository to property using Ninject but always get null reference of binding object. I will explain the problem using code below.

   public interface IServiceRepository
    {
        User GetUser(string email);
        IQueryable<Statistic> GetStatisticForCurrentMonth(string ip);
        void InsertStatistic(ConversionModel conversionModel);

class ServiceRepository : IServiceRepository
{
//Implementation of the Interface
}

I am would like to bind the repository above to class below while the class is created. Unfortunately Repository object is always null. Maybe I have misunderstood how Ninject is working? How to solve the problem?

    public class Converter
    {
        [Inject]
        public static IServiceRepository Repository { get; set; }
        private static Converter _converter;

        public static Converter Instance
        {
            get { return _Converter  ?? (_Converter  = new Converter ());
        }
}

Ninject activator code

private static void RegisterServices(IKernel kernel)
{
   kernel.Bind<IServiceRepository>().ToMethod(context => Converter.Repository);
}   

Update

I have tried to rewrite code like this

 public class Converter
    {
        private readonly IServiceRepository _repository;

        public Converter(IServiceRepository repository)
        {
            _repository = repository;
        }

//skip code
}

The test...

    [TestMethod]
    public void ConverterInstanceCreated()
    {           
         using (IKernel kernel = new StandardKernel())
         {                 
             kernel.Bind<IServiceRepository>().To<ServiceRepository>();
             Assert.IsNotNull(kernel.Get<Converter>());
         }
    }

gives exception

Test method PC.Tests.NinjectTest.ConverterInstanceCreated threw exception: 
Ninject.ActivationException: Error activating IServiceRepository
No matching bindings are available, and the type is not self-bindable.
Activation path:
  2) Injection of dependency IServiceRepository into parameter repository of constructor of type Converter
  1) Request for Converter

I just lost, I am trying to understand how Ninject is working for about week without any success. In my case why this exception is thrown?

Also please someone post working example with one repository injection to singleton class.


回答1:


Ninject does not inject statics. Change the coynverter to a non-static class and configure it as Singleton in ninject. Also use constructor injection and make the repo a private field.

Now you can inject the converter to the constructors where you need it.




回答2:


Even though you are using Property injection and not Constructor injection I think it would still be

private static void RegisterServices(IKernel kernel)
{
  kernel.Bind<IServiceRepository>().To<ServiceRepository>();
}

As ninject still just needs to know what concrete type to map to the Interface

I haven't tested this so apologies if it's wrong.



来源:https://stackoverflow.com/questions/10108352/bind-to-property-always-return-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!