Subscribe / Unsubscribe Enewsletters | Login | Register

Pencil Banner

Exploring the dependency injection principle

Joydip Kanjilal | Aug. 24, 2015
Take advantage of the dependency injection principle to provide support for pluggable implementations in your application and build loosely coupled, testable components

    {

        //TODO: Properties that correspond to the Customer entity

    }

Now, refer to the following interfaces. The ICustomerRepository and IEmployeeRepository interfaces inherit the IRepository interface -- IRepository is the base interface here and declares a few repository methods. 

 public interface IRepository

    {

        IQueryable<EntityBase> GetAll();

        void Add(EntityBase entity);

        void Delete(EntityBase entity);

        void Edit(EntityBase entity);

        void Save();

    }

    public interface ICustomerRepository : IRepository

    {

        Customer GetSingle(int customerId);

    }

    public interface IEmployeeRepository : IRepository

    {

        Employee GetSingle(int employeeId);

    }

Note that the GetSingle method is specific to the ICustomerRepository and IEmployeeRepository interfaces.

Now that the interfaces are in place, let's create the concrete classes to perform CRUD operations for both the Customer and Employee entities. The following code snippet shows how the EmployeeRepository and CustomerRepository classes look like. Note that both these classes implement the IEmployeeRepository and ICustomerRepository interfaces respectively. 

public class EmployeeRepository :IEmployeeRepository

    {

         public Employee GetSingle(int employeeId)

        {

            throw new System.NotImplementedException();

        } 

        public IQueryable<EntityBase> GetAll()

        {

            throw new System.NotImplementedException();

        } 

        public void Add(EntityBase entity)

        {

            //Some code

        } 

        public void Delete(EntityBase entity)

        {

            //Some code

        } 

        public void Edit(EntityBase entity)

        {

            //Some code

        } 

            public void Save()

                {

                   //Some code

                 }

    }

    public class CustomerRepository : ICustomerRepository

    {

        public Customer GetSingle(int customerId)

        {

            throw new System.NotImplementedException();

        } 

        public IQueryable<EntityBase> GetAll()

        {

            throw new System.NotImplementedException();

        } 

        public void Add(EntityBase entity)

        {

            throw new System.NotImplementedException();

        } 

        public void Delete(EntityBase entity)

        {

            throw new System.NotImplementedException();

        } 

        public void Edit(EntityBase entity)

        {

            throw new System.NotImplementedException();

        } 

        public void Save()

        {

            throw new System.NotImplementedException();

        }

    }

The following class represents the BusinessLogic class. Note that this is generic in nature and you would need to extend this class if need be to provide some specific implementation. For the sake of this illustration, this class contains just one property named Repository of type IRepository. 

 public class BusinessLogic

    {

        public IRepository Repository

        {

            get; set;

        }

    } 

And, you are done! The following code snippet illustrates how the Add method of the EmployeeRepository class can be invoked. Note how the repository instance is injected using interface injection technique.

 

Previous Page  1  2  3  Next Page 

Sign up for CIO Asia eNewsletters.