Skip to content

Dependency Injection

Dependency Injection is all about removing the dependency of high-level modules on the low-level modules and those that achieve Decoupling. Let’s look at the following example:

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

namespace test_application
{
    class Program
    {
        static void Main(string[] args)
        {
            userInterface UI = new userInterface();
            UI.signUp();
            Console.ReadLine();
        }
    }

    public class userInterface
    {
        Business business = new Business();
        public void signUp()
        {
            Console.WriteLine($"Please enter username");
            string username = Console.ReadLine();
            Console.WriteLine($"Please enter Password");
            string password = Console.ReadLine();

            business.signUp(username, password);

            Console.WriteLine("You have entered the following details");
            Dictionary<string, string> user = business.userList();
            Console.WriteLine($"UserName: {user.Keys.First()}  and Password: {user.Values.First()}");
        }
    }

    public class Business
    {
        DataAccess dataAccess = new DataAccess();
        public void signUp(string username, string password)
        {
            dataAccess.store(username,password);
        }

        public Dictionary<string,string> userList()
        {
            return dataAccess.userList();
        }
    }

    public class DataAccess
    {
        public Dictionary<string,string> users = new Dictionary<string, string>();
        public void store(string username, string password)
        {
            users.Add(username,password);
        }

        public Dictionary<string,string> userList()
        {
            return users;
        }
    }
}

As you could see our user interface class is dependent on the business class which intern is dependent on the DataAccess class. Let’s say we want to replace business class with some other class lets say businessV2 you end up replacing lot of code.

Instead of using concrete classes directly use interfaces, so lets say we want to change business class to businessv2 class in userinterface

namespace test_application
{
    class Program
    {
        static void Main(string[] args)
        {
            userInterface UI = new userInterface();
            UI.signUp();
            Console.ReadLine();
        }
    }

    public class userInterface
    {
        IBusiness business = new BusinessV2();
        public void signUp()
        {
            Console.WriteLine($"Please enter username");
            string username = Console.ReadLine();
            Console.WriteLine($"Please enter Password");
            string password = Console.ReadLine();

            business.signUp(username, password);

            Console.WriteLine("You have entered the following details");
            Dictionary<string, string> user = business.userList();
            Console.WriteLine($"UserName: {user.Keys.First()}  and Password: {user.Values.First()}");
        }
    }

    public interface IBusiness
    {
        void signUp(string username, string password);
        Dictionary<string, string> userList();
    }

    public class Business : IBusiness
    {
        IDataAccess dataAccess = new DataAccess();
        public void signUp(string username, string password)
        {
            dataAccess.store(username, password);
        }

        public Dictionary<string, string> userList()
        {
            return dataAccess.userList();
        }
    }

    public class BusinessV2 : IBusiness
    {
        IDataAccess dataAccess = new DataAccess();
        public void signUp(string username, string password)
        {
            dataAccess.store(username,password);
        }

        public Dictionary<string,string> userList()
        {
            return dataAccess.userList();
        }
    }

    public interface IDataAccess
    {
        void store(string username, string password);
        Dictionary<string, string> userList();
    }

    public class DataAccess : IDataAccess
    {
        public Dictionary<string, string> users = new Dictionary<string, string>();
        public void store(string username, string password)
        {
            users.Add(username, password);
        }

        public Dictionary<string, string> userList()
        {
            return users;
        }
    }
}

Lets add a constructor and try to pass businessV2 removing tightly coupling of userInterface with business

    class Program
    {
        static void Main(string[] args)
        {
            userInterface UI = new userInterface(new BusinessV2());
            UI.signUp();
            Console.ReadLine();
        }
    }

    public class userInterface
    {
        public IBusiness _business;

        public userInterface(IBusiness business)
        {
            _business = business;
        }

        public void signUp()
        {
            Console.WriteLine($"Please enter username");
            string username = Console.ReadLine();
            Console.WriteLine($"Please enter Password");
            string password = Console.ReadLine();

            _business.signUp(username, password);

            Console.WriteLine("You have entered the following details");
            Dictionary<string, string> user = _business.userList();
            Console.WriteLine($"UserName: {user.Keys.First()}  and Password: {user.Values.First()}");
        }
    }

extending it to data access class

namespace test_application
{
    class Program
    {
        static void Main(string[] args)
        {
            var access =new DataAccess();
            var business = new BusinessV2(access);
            userInterface UI = new userInterface(business);
            UI.signUp();
            Console.ReadLine();
        }
    }

    public class userInterface
    {
        public IBusiness _business;

        public userInterface(IBusiness business)
        {
            _business = business;
        }

        public void signUp()
        {
            Console.WriteLine($"Please enter username");
            string username = Console.ReadLine();
            Console.WriteLine($"Please enter Password");
            string password = Console.ReadLine();

            _business.signUp(username, password);

            Console.WriteLine("You have entered the following details");
            Dictionary<string, string> user = _business.userList();
            Console.WriteLine($"UserName: {user.Keys.First()}  and Password: {user.Values.First()}");
        }
    }

    public interface IBusiness
    {
        void signUp(string username, string password);
        Dictionary<string, string> userList();
    }

    public class Business : IBusiness
    {
        public IDataAccess _dataAccess = new DataAccess();

        public Business(IDataAccess dataAccess)
        {
            _dataAccess = dataAccess;
        }

        public void signUp(string username, string password)
        {
            _dataAccess.store(username, password);
        }

        public Dictionary<string, string> userList()
        {
            return _dataAccess.userList();
        }
    }

    public class BusinessV2 : IBusiness
    {
        public IDataAccess _dataAccess = new DataAccess();

        public BusinessV2(IDataAccess dataAccess)
        {
            _dataAccess= dataAccess;
        }

        public void signUp(string username, string password)
        {
            _dataAccess.store(username,password);
        }

        public Dictionary<string,string> userList()
        {
            return _dataAccess.userList();
        }
    }

    public interface IDataAccess
    {
        void store(string username, string password);
        Dictionary<string, string> userList();
    }

    public class DataAccess : IDataAccess
    {
        public Dictionary<string, string> users = new Dictionary<string, string>();
        public void store(string username, string password)
        {
            users.Add(username, password);
        }

        public Dictionary<string, string> userList()
        {
            return users;
        }
    }
}
Published inUncategorised

Be First to Comment

Leave a Reply

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