DEPENDENCY INJECTION IN C#
Dependency Injection (Di) in C#

Dependency Injection (Di) in C#

PRE-REQUISITES

  • C# basics
  • Methods
  • Properties
  • OOP Concepts
  • Classes and objects
  • Constructor
  • Inheritance
  • Access Specifiers – Public Private etc
  • Abstraction
  • Interface

Tight Coupling

Tight coupling is when a group of classes are highly dependent on one another.

Tight Coupling
Tight Coupling

DIS-ADVANTAGES OF TIGHT COUPLING

  • Tight coupling is when a group of classes are highly dependent on one another.
  • This scenario arises when a class assumes too many responsibilities, or when one concern is spread over many classes rather than having its own class.
  • Difficult to maintain
  • Difficult to test

Loose Coupling

Loose coupling means that the classes are independent of each other.

Loose Coupling
Loose Coupling

ADVANTAGES OF LOOSE COUPLING

  • Loose coupling means that the classes are independent of each other.
  • Loose coupling is achieved by means of a design that promotes single-responsibility and separation of concerns.
  • Easy to maintain
  • Easy to test

Important Question ?

  • The answer is by using Dependency injection.
  • Dependency injection is achieved using interfaces.
  • Interfaces are a powerful tool to use for decoupling.
  • Classes can communicate through interfaces rather than other concrete classes.

What is Dependency Injection ?

What Is Dependency Injection

What is Dependency Injection ?

  • Dependency Injection (DI) is a software pattern.
  • In C#, Dependency Injection is basically providing the objects that an object needs, instead of having it construct the objects themselves.
  • DI is a technique whereby one object supplies the dependencies of another object.
  • With the help of DI, we can write loosely coupled code.
  • DI is achieved by writing loosely couple code.
  • A loosely-coupled code is a code where all your classes can work independently without relying on each other.

Types of Dependency Injection ?

There are 3 types of DI in C#

  1. Constructor Injection
  2. Setter or property Injection
  3. Method Injection

Dependency Injection Using Constructor Injection In C#

Dependency Injection Using Constructor Injection In C#

Di Using constructor injection

Constructor injection is nothing but the process of injecting dependent class object through the constructor.

In the Given example, the account class has a dependency on SavingAccount & CurrentAccount classes.

So constructor injection means, injecting SavingAccount & CurrentAccount class objects in Account class constructor using interface.

Source Code – Constructor Injection

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

namespace DependencyInjectionCsharp
{
    public interface IAccount
    {
        void PrintDetails();
    }

    class CurrentAccount : IAccount
    {
        public void PrintDetails()
        {
            Console.WriteLine("Details Of Current Account..");
        }
    }
    class SavingAccount : IAccount
    {
        public void PrintDetails()
        {
            Console.WriteLine("Details Of Saving Account..");
        }
    }

    class Account
    {
        private IAccount account;

        public Account(IAccount account) // Parameterized Constructor
        {
            this.account = account;
        }

        public void PrintAccounts()
        {
            account.PrintDetails();
        }

    }

    class Program
    {
        static void Main(string[] args)
        {
            IAccount ca = new CurrentAccount();
            Account a = new Account(ca);
            a.PrintAccounts();

            IAccount sa = new SavingAccount();
            Account a2 = new Account(sa);
            a2.PrintAccounts();

            Console.ReadLine();
        }
    }
}

Dependency Injection Using Property injection in C#

Dependency Injection Using Property injection in C#

Di Using Property injection

  • Dependency Injection is basically providing the objects that an object needs, instead of having it construct the objects themselves.
  • Setter or property injection is injecting dependent class object through the property.
  • So setter or property injection means, injecting SavingAccount & CurrentAccount class objects in Account class using property

Source Code – Di Using Property injection

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

namespace DependencyInjectionCsharp
{
    public interface IAccount
    {
        void PrintDetails();
    }

    class CurrentAccount : IAccount
    {
        public void PrintDetails()
        {
            Console.WriteLine("Details Of Current Account..");
        }
    }
    class SavingAccount : IAccount
    {
        public void PrintDetails()
        {
            Console.WriteLine("Details Of Saving Account..");
        }
    }

    class Account
    {
        public IAccount account { get; set; }

        public void PrintAccounts()
        {
            account.PrintDetails();
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Account sa = new Account();
            sa.account = new SavingAccount();
            sa.PrintAccounts();

            Account ca = new Account();
            ca.account = new CurrentAccount();
            ca.PrintAccounts();

            Console.ReadLine();
        }
    }
}

Dependency Injection Using METHOD injection in c#

Di Using METHOD injection

  • Method Injection is injecting dependent class object through a class method.
  • What is mean by this?
  • In the given example, Account class has a dependency on SavingAccount and CurrentAccount classes.
  • So the method Injection means, injecting SavingAccount & CurrentAccount class objects directly into the Account class method using interface.

Source Code – Di Using Method injection

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

namespace DependencyInjectionCsharp
{
    public interface IAccount
    {
        void PrintDetails();
    }

    class CurrentAccount : IAccount
    {
        public void PrintDetails()
        {
            Console.WriteLine("Details Of Current Account..");
        }
    }
    class SavingAccount : IAccount
    {
        public void PrintDetails()
        {
            Console.WriteLine("Details Of Saving Account..");
        }
    }

    class Account
    {
        public void PrintAccounts(IAccount account)
        {
            account.PrintDetails();
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Account sa = new Account();
            sa.PrintAccounts(new SavingAccount());

            Account ca = new Account();
            ca.PrintAccounts(new CurrentAccount());

            Console.ReadLine();
        }
    }
}

Download Notes & Source Code From Link Given Below

https://www.mediafire.com/file/x3qgo5m3eha8sw3/DEPENDENCY+IJECTION+C#.rar/file

No responses yet

Leave a Reply

Your email address will not be published. Required fields are marked *