Understand Dependency Injection: Property Injection

Introduction

Welcome to the Dependency Injection article series. In this series we are explaining various techniques to implement Dependency Injection. In our previous article we saw how to implement Dependency Injection using the constructor injection technique. You can read it here.

What is Dependency Injection?

In this article we will learn how to implement Dependency Injection using Property Injection. We explained in the previous article how Dependency Injection helps to implement a de-coupled architecture. In this article we will not repeat them, rather we will explain the implementation part directly.

The basic idea of Dependency Injection is to reduce the dependency of one component on another. For example, consider that one server class and another client class exists, if they talk directly then we can say that they are tightly coupled but if they communicate via an interface then we can say that they are loosely couple. The scenario is like the following.

property injection

In this picture we see that the consumer class is not talking to the service class directly. The Caller interface is the connection between them. So, now if we change any implementation of the Service class then it will not affect the Consumer class.

Try to understand the following example.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace Client
{
    public interface ICaller
    {
        void Serve();
    }

    public class Service : ICaller
    {
        public void Serve()
        {
            Console.WriteLine("Service Started");
        }
    }

    public class Client
    {
        private ICaller s;

        public ICaller CallService
        {
            set
            {
                this.s = value;
            }
        }

        public void Start()
        {
            Console.WriteLine("Service is called by client");
            this.s.Serve();
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            //Create object of client Class
            Client c = new Client();

            //Create object of service class using ICaller interface
            ICaller ic = new Service();

            //Set property of client class
            //This is the main trick. It will define which function needs to be called
            c.CallService = ic;

            //Call the service
            c.Start();
            Console.ReadLine();
        }
    }
}

Output

output-property injection

The example above is very simple to understand. In the client class we are setting the object of the service class to the CallService property. When we call the Start() method from the Main() function the appropriate Service() method is being called that is defined in the Service class.

Conclusion

In this example we have learned how to implement the Dependency Injection architectural style using the Property Injection technique. Hope you have understood the concept. In the next article we will learn to implement Dependency Injection using the Function Injection technique. Happy learning.


Similar Articles