1

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

3 回答 3

0

你需要像下面这样定义你的财产

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();
于 2019-01-21T06:52:50.150 回答
0

选项1:

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

In addition @ehasanul-hoque answer, you can modify the _salary field to be private and add a public property like (see this answer for more details):

private ISalary _salary;

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

Option 2:

If you still does not want to convert it to a property, you may use Method Injection, by adding a SetSalary method like:

public class Employee: IEmployee
{
    private ISalary _salary;

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

And the builder may looks like:

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>();
于 2019-01-23T09:36:47.150 回答
-1

在 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 }));
于 2019-01-21T08:48:48.503 回答