Part 3: Understanding with different ways of Injecting Dependencies
I will cover most of the topics about Dependency Injection in this Series. This series contains four parts as follows
- Part 1: Understanding why we need to use Dependency Injection? In detail explanation
- Part 2: Understanding how to implement Dependency Injection using Unity
- Part 3: Understanding different ways of injecting Dependencies
- Part 4: Understanding how Service Locator different from Dependency Injection
Let start with Part 3: Understanding different ways of injecting Dependencies
Please read my previous parts of this series before continuing for more better understanding further.
In part 1 of this series we discussed more on how and why we need Dependency Injection and in part 2 we discussed and done with core part implementation of Dependency Injection using Unity Framework. Now we will understand different ways of injecting this EmployeeBusiness class object from caller.
We can inject this object in three way and following are those
1) Constructor Injection: We are much familiar about this way of injecting as we already done in our implementation discussion in part 2. Here we are sending the EmployeeBusiness object via. Calling Class Constructor.
This is a common or we can say default injecting way specially with Controller classes. When we inject via. Constructor we could have the scope of this object to entire class.
If we want to inject more than one dependencies, Constructor Injection will be the best option. In following code we are sending more than one dependencies via. Constructor.
public class EmployeeController : ApiController { IEmployeeBusiness _employeeBusiness; ILogger _logger; public EmployeeController(IEmployeeBusiness employeeBusiness, ILogger logger) { _employeeBusiness = employeeBusiness; _logger = logger; } }
2) Property/Setter injection: We can also inject the dependencies using properties. This way of injecting will be used in a situation where we are not sure about sending dependency and it might depend on some logic or conditions.
One disadvantage is, we need to make sure to do a null check every time we use this property as a safe check.
No need to change the constructor if we need to extend the features of the class by introducing more functionality via other dependencies using different properties.
In the below example, we had the situation like, based on datastore we need to decide whether we need to inject MSSQLRepository object or ORACLERepository object.
public class EmployeeController : ApiController { public IEnumerable<Employee> GetAllEmployees(string datastore) { IEmployeeBusiness _employeeBusiness = new EmployeeBusiness(); if(datastore == "SQLSERVER") _employeeBusiness.RepositoryObj = new MSSQLRepository("<<Conn string>>"); else _employeeBusiness.RepositoryObj = new ORACLERepository("<<Conn string>>"); } return _employeeBusiness.GetAllEmployees(); }
3) Method injection: This is similar like Property Injection and will be useful when the scope of the service object is not whole class and limited to this method.
public class EmployeeController : ApiController { public IEnumerable<Employee> GetAllEmployees(string datastore) { IEmployeeBusiness _employeeBusiness = new EmployeeBusiness(); IRepository _repository; if(datastore == "SQLSERVER") _repository = new MSSQLRepository("<<Conn string>>"); else _repository = new ORACLERepository("<<Conn string>>"); return _employeeBusiness.GetAllEmployees(_repository); } }
That’s it, We are done with understanding different ways of injecting ddependencies and with this we almost done understanding working with Dependency Injection. In next part i.e., in Part 4 will understand Service Locator and how it is different from Dependency Injection.
Part 4: Understanding how Service Locator different from Dependency Injection