绑定到属性始终返回null [英] bind to property always return null

查看:117
本文介绍了绑定到属性始终返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Ninject将存储库绑定到属性,但始终获取绑定对象的空引用.我将在下面的代码中说明问题.

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
}

我想在创建类时将bind the repository above转换为class below.不幸的是,Repository对象始终是null.也许我误解了Ninject的工作方式?如何解决问题?

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 ());
        }
}

注入激活码

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

更新

我试图重写这样的代码

 public class Converter
    {
        private readonly IServiceRepository _repository;

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

//skip code
}

测试...

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

给予例外

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

我只是失去了,我想了解Ninject是如何工作了一周没有任何成功.就我而言,为什么会引发此异常?

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?

还请有人发布工作示例,其中将一个存储库注入到Singleton类中.

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

推荐答案

Ninject不注入静态变量.将coynverter更改为非静态类,然后在ninject中将其配置为Singleton.还可以使用构造函数注入,并将存储库设为私有字段.

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.

这篇关于绑定到属性始终返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆