how dependency injection works code example

Example 1: dependency injection

Dependency injection is basically providing the objects that an object needs 
(its dependencies) instead of having it construct them itself. 

It's a very useful technique for testing, since it allows dependencies 
to be mocked or stubbed out.

Example 2: dependency injection

Class A   Class B   if A uses some methods of B then its a dependency injection

Example 3: dependency injection

public class CustomerService
{
    CustomerBusinessLogic _customerBL;

    public CustomerService()
    {
        _customerBL = new CustomerBusinessLogic(new CustomerDataAccess());
    }

    public string GetCustomerName(int id) {
        return _customerBL.ProcessCustomerData(id);
    }
}

Tags:

Misc Example