【问题标题】:Unable to do property injection using Autofac无法使用 Autofac 进行属性注入
【发布时间】:2019-01-21 04:11:34
【问题描述】:

我是 autofac 的新手。我正在尝试使用这个 IoC 容器进行属性注入。以下是我的代码。我收到错误:

对象引用未设置为对象的实例

在这一行: . return _salary.employeeSalary;在GetSalary(int employeeId) 方法中。在我什至尝试过的程序类中,build.RegisterType<Employee>().WithProperty("_salary", new Salary{ employeeId = 5, employeeSalary = 500 });

public interface IEmployee
{
    double GetSalary(int employeeId);
}
public interface ISalary
{
    int employeeId { get; set; }
    double employeeSalary { get; set; }
}
public class Salary : ISalary
{
    public int employeeId {get; set;}
    public double employeeSalary { get; set; }
}

public class Employee: IEmployee
{
    public ISalary _salary;        

    public double GetSalary(int employeeId)
    {
        if (employeeId == 5)
        {
            return _salary.employeeSalary;
        }
        else
        {
            return 0;
        }
    }
}

public class Program
 {
     static void Main(string[] args)
     {
         var build = new ContainerBuilder();
         build.RegisterType<Salary>().As<ISalary>();
         build.RegisterType<Salary>().As<ISalary>().PropertiesAutowired();               
         var container = build.Build();
         Employee employee = container.Resolve<Employee>();

         Console.WriteLine(employee.GetSalary(5));
         Console.ReadLine();            
     }
}

【问题讨论】:

  • 我会给你一个小费。 _salary 不是属性。 stackoverflow.com/questions/295104/…
  • @SilentTremor 属性注入肯定在 Autofac 中工作(我已经使用过很多次了)。不,public ISalary _salary; 不是属性。它是一个领域。这不是争论的焦点。它不是属性。

标签: c# autofac-configuration


【解决方案1】:

选项 1:

为了让Property Injection 使用属性,请注意_salary 是一个字段,您需要将它配置为一个属性 .

除了@ehasanul-hoque answer,您还可以将_salary 字段修改为私有并添加一个公共属性,例如(有关详细信息,请参阅此answer):

private ISalary _salary;

public ISalary Salary
{
    get
    {
        return this._salary;
    }
    set
    {
        _salary = this.value;
    }
}

选项 2:

如果您仍然不想将其转换为属性,您可以使用Method Injection,通过添加SetSalary 方法,例如:

public class Employee: IEmployee
{
    private ISalary _salary;

    public void SetSalary(ISalary salary)
    {
        this._salary = salary;
    }
}

构建器可能看起来像:

var builder = new ContainerBuilder();
builder.RegisterType<Salary>().As<ISalary>();
builder.RegisterType<Employee>()
    .OnActivated(IActivatedEventArgs<Employee> e) =>
    {
        var salary = e.Context.Resolve<ISalary>();
        e.Instance.SetSalary(salary);
    });

var container = build.Build();
Employee employee = container.Resolve<Employee>();

【讨论】:

    【解决方案2】:

    你需要像下面这样定义你的属性

    public ISalary _salary { get; set; }
    

    那么下面的代码将为你工作

    var build = new ContainerBuilder();
    // build.RegisterType<Salary>().As<ISalary>();
    build.RegisterType<Salary>().As<ISalary>();
    build.RegisterType<Employee>().As<IEmployee>().PropertiesAutowired();
    var container = build.Build();
    IEmployee employee = container.Resolve<IEmployee>();
    
    Console.WriteLine(employee.GetSalary(5));
    Console.ReadLine();
    

    【讨论】:

      【解决方案3】:

      在Employee中创建构造函数

          public Employee(ISalary salary)
          {
              _salary = salary;
          }
      

      这样注册员工

      build.RegisterType<Employee>().As<IEmployee>()
                  .WithParameter(new TypedParameter(typeof(ISalary), "salary"));
      

      这样解决

      var employee = container.Resolve<IEmployee>(new NamedParameter("salary",
                      new Salary { employeeId = 5, employeeSalary = 500 }));
      

      【讨论】:

      • 嗨,普拉桑斯。谢谢你的建议。我已经实现了构造函数注入。我想通过属性注入来实现。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-01
      • 1970-01-01
      • 2017-01-26
      • 1970-01-01
      • 2012-09-21
      相关资源
      最近更新 更多