1

I am new to autofac. I am trying to do property injection using this IoC container. The following is my code. I am getting error:

Object reference not set to an instance of an object

At this line: . return _salary.employeeSalary; in the GetSalary(int employeeId) method. In the Program class I even tried, 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();            
     }
}
AsthaUndefined
  • 1,111
  • 1
  • 11
  • 24
Massey
  • 1,099
  • 3
  • 24
  • 50
  • 1
    I'll give you a tip. `_salary` is **not** a property. https://stackoverflow.com/questions/295104/what-is-the-difference-between-a-field-and-a-property?rq=1 – mjwills Jan 21 '19 at 04:54
  • @SilentTremor Property injection definitely **will** work in Autofac (I've used it many times). And no, `public ISalary _salary;` isn't a property. It is a field. This isn't a point of debate. It **is not** a property. – mjwills Jan 21 '19 at 10:09

3 Answers3

1

Option 1:

In order to get Property Injection to work with properties, pay attention that the _salary is a field, and what you need to configure it as a Property .

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>();
Shahar Shokrani
  • 7,598
  • 9
  • 48
  • 91
0

you need to define your property like below

public ISalary _salary { get; set; }

Then the below code will work for you

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();
Shahar Shokrani
  • 7,598
  • 9
  • 48
  • 91
Ehasanul Hoque
  • 578
  • 8
  • 14
-1

Create a constructor in Employee

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

Register Employee like this

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

Resolve it like this

var employee = container.Resolve<IEmployee>(new NamedParameter("salary",
                new Salary { employeeId = 5, employeeSalary = 500 }));
Prasanth V J
  • 1,126
  • 14
  • 32
  • Hi Prashanth. Thanks for your suggestion. I have already implemented the constructor injection. I want to do it through property injection. – Massey Jan 21 '19 at 19:42