无法使用 Autofac 进行 属性 注入
Unable to do property injection using Autofac
我是 autofac 的新手。我正在尝试使用此 IoC 容器进行 属性 注入。以下是我的代码。我收到错误:
Object reference not set to an instance of an object
在这一行:。
return _salary.employeeSalary
;在 GetSalary(int employeeId)
方法中。在程序中 class 我什至尝试过,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();
}
}
您需要像下面这样定义您的 属性
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();
在 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 }));
选项 1:
为了使 Property Injection
与属性一起工作,请注意 _salary
是一个 字段 ,您需要将其配置为一个 属性 .
此外@ehasanul-hoque , you can modify the _salary
field to be private and add a public property like (see this 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>();
我是 autofac 的新手。我正在尝试使用此 IoC 容器进行 属性 注入。以下是我的代码。我收到错误:
Object reference not set to an instance of an object
在这一行:。
return _salary.employeeSalary
;在 GetSalary(int employeeId)
方法中。在程序中 class 我什至尝试过,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();
}
}
您需要像下面这样定义您的 属性
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();
在 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 }));
选项 1:
为了使 Property Injection
与属性一起工作,请注意 _salary
是一个 字段 ,您需要将其配置为一个 属性 .
此外@ehasanul-hoque _salary
field to be private and add a public property like (see this 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>();