Dependency Injection (DI) could be a software system style pattern. It permits us to develop loosely-coupled code. The intent of Dependency Injection is to create code rectifiable. Dependency Injection helps to cut back the tight coupling among software system elements. (DI) reduces the hard-coded dependencies among your categories by injecting those dependencies at run time rather than style time technically.

Types of Dependency Injection

As we have a tendency to mention on top of, the contrivance category injects the service (dependency) to the consumer (dependent). The contrivance category injects dependencies loosely in 3 ways: using constructor, through property, or through methodology.

  1. Constructor Injection: within the creator injection, contrivance provides service (dependency) through the consumer category creator. It supply static methods or dependencies to another objects.
  2. Property Injection: In property injection (Setter Injection), injector supplies dependency through a public property of the client class. The .NET core standard container does not support property injection.
  3. Method Injection: Its basic concept is Injection container class can override the method more times that we need to call on the class. The injecting method, such as called getter method. Client class implement an interface that declares method(s) to provide dependency and also injector uses this interface to provide dependency to the consumer class.

We are going to use the following ways to implementing the dependency injection (DI).

Constructor Injection

  1. This way is widely used to implement DI.
  2. DI is done by supplying the DEPENDENCY through the class’s constructor when it creating the instance of the class.
  3. The Injected component can be used in anywhere within the class.
  4. It’s recommended to use when the injected dependency are using across class methods.
  5. It addresses most common scenario. When a class requires one or more dependencies.
using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace propertyinjuction

{

    public interface text

    {

        void print();

    }

    class format : text

    {

        public void print()

        {

            Console.WriteLine(" here is text format");

        }

    }

    // constructor injection

    public class constructorinjection

    {

        private text _text;

        public constructorinjection(text t1)

        {

            this._text = t1;

        }

        public void print()

        {

            _text.print();

        }

    }

    class constructor

    {

        static void Main(string[] args)

        {

            constructorinjection cs = new constructorinjection(new format());

            cs.print();

            Console.ReadKey();

        }

    }

}

 

Property/Setter Injection

  1. It’s recommended to use, when a class has optional dependencies, or implementations may need this to be swapped.
  2. It could be use different logger implementations.

It does not required to creation of new object or the modifying an existing one without changing the object state.

public interface INofificationAction

{

    void ActOnNotification(string message);

}

class tutPanel

{

    INofificationAction task = null;

    public void notify(INofificationAction at, string messages)

    {

        this.task = at;

        task.ActOnNotification(messages);

    }

}

class EventLogWriter : INofificationAction

{

    public void ActOnNotification(string message)

    {

        // Write to event log here

    }

}

class Program

{

    static void Main(string[] args)

    {

        EventLogWriter elw = new EventLogWriter();

        tutPanel tp = new tutPanel();

        tp.notify(elw, "to logg");

        System.Console.ReadKey();

    }

}


Method Injection

  1. Method Inject is dependency into a single method. It is generally use of that method.
  2. It’s useful, where the whole class does not need the dependency, then only one method having that dependency.
  3. This way is rarely used.
using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;


namespace propertyinjuction

{

    public interface Iset

    {

        void print();

    }

    public class servic : Iset

    {

        public void print()

        {

            Console.WriteLine("print something…");

        }

    }

    public class client

    {

        private Iset _set;

        public void run(Iset serv)

        {

            this._set = serv;

            Console.WriteLine("start");

            this._set.print();

        }

    }

    class method

    {

        public static void Main()

        {

            client cn = new client();

            cn.run(new servic());

            Console.ReadKey();

        }

    }

}

Advantages of Dependency Injection

  1. Loosely coupling code.
  2. Increases the code reusability.
  3. Improves the code maintainability.
  4. Possible to make unit testing.

Conclusion

Dependency injection helps to achieve and improve class coupling, better code reusability, improve the code maintainability and application unit testing.

Related Articles

Exception Handling in C#

How to Use Throw Expressions in C# 7.0

What’s New in ASP.NET Core 3.0?

Last modified: February 17, 2019

Comments

Write a Reply or Comment

Your email address will not be published.