Saturday, August 18, 2018

Dependency Injection (DI)


  1. DI is a software design pattern that is allow to develop loosely couple code.
  2. It is great way to reduce tightly coupling between software component.
  3. DI also enables us to manage future changes of code.
  4. The purpose of DI is make code maintainable.
 We have following different way to implement DI.

1. Constructor Injection.

2. Property/Setter Injection

3. Method Injection


1. Constructor Injection:

This is the widely way used way to implement DI.
Injected component can we used anywhere in the class.

Example:

class Program
    {
        static void Main(string[] args)
        {
            SampleA obj1 = new SampleA();
            Client C1 = new Client(obj1);
            C1.MethodShow();
            Console.ReadKey();
        }
    }

    public interface Isample
    {
        void Test();
    }

    public class Client
    {
        private Isample _sampleA;
        public Client(Isample sample)
        {
            _sampleA = sample;
        }

        public void MethodShow()
        {
            _sampleA.Test();
        }

    }

    public class SampleA:Isample
    {
        public void Test() {
            Console.WriteLine("This is Example of SampleA");
        }
    }

    public class SampleB:Isample
    {
        public void Test() {
            Console.WriteLine("This is Example of SampleA");
        }

    }


Property/Setter Injection:


class Program
    {
        static void Main(string[] args)
        {
            SampleA obj1 = new SampleA();
            Client c1 = new Client();
            c1.SampleA = obj1; // Pass Dependency
         
            Console.ReadKey();

        }
    }

    public interface Isample
    {
        void Test();
    }

    public class Client
    {
        private Isample _sampleA;

        public Isample SampleA 
        {
            set { _sampleA = value; }
        }

        public void MethodShow()
        {
            _sampleA.Test();
        }
    }

    public class SampleA : Isample
    {
        public void Test()
        {
            Console.WriteLine("This is Example of SampleA");
        }
    }

    public class SampleB : Isample
    {
        public void Test()
        {
            Console.WriteLine("This is Example of SampleA");
        }

    }

Method Injection:

Example:

static void Main(string[] args)
        {
            SampleA s1 = new SampleA();
            Client c1 = new Client();
            c1.start(s1);// pass dependency
            Console.ReadKey();
        }
    }

    public interface Isample
    {
        void Test();
    }

    public class Client
    {
        private Isample _sample;
        public void start(Isample s) {
            s.Test();
        }

    }

    public class SampleA : Isample
    {
        public void Test()
        {
            Console.WriteLine("This is Example of SampleA");
        }
    }

    public class SampleB : Isample
    {
        public void Test()
        {
            Console.WriteLine("This is Example of SampleA");
        }

    }



example:






For example, Suppose your Client class needs to use two service classes, then the best you can do is to make your Client class aware of abstraction i.e. IService interface rather than implementation i.e. Service1 and Service2 classes. In this way, you can change the implementation of the IService interface at any time (and for how many times you want) without changing the client class code.



Note:

Implement this method in SchoolBusinessLayer.cs. This method gets data from DataAccessLayer.

For this, in Constructor of SchoolBusinessLayer,  inject ISchoolDataAccessLayer.
Hence, we are not directly dependent upon SchoolDataAccessLayer.cs (This technique is used to make an Application Loosely Couple).


Throughout the project development, we never created any object. We communicated with one layer and another with the help of an interface.





















No comments:

Post a Comment