Thursday, June 4, 2020

Dependency Injection in C#

June 04, 2020

What is Dependency Injection ?


Dependency Injection is a software design pattern. It allows us to develop loosely-coupled code. The intent of Dependency Injection is to make code maintainable. Dependency Injection helps to reduce the tight coupling among software components. Dependency Injection reduces the hard-coded dependencies among your classes by injecting those dependencies at run time instead of design time technically. 





Types of Dependency Injection in C#


1) Constructor Injection

2) Property Injection

3) Method Injection



1) Constructor Injection -


In the constructor injection, the injector supplies the service (dependency) through the client class constructor.

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
using System.Threading.Tasks;  
  
namespace SampleProgram  
{  
    public interface text  
    {
        void print();
    }
    class format : text
    {
        public void print()
        {
            Console.WriteLine("this 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();          
        }
    }
}

 Output:-


this is text format


2) Property Injection-


In the property injection (aka the Setter Injection), the injector supplies the dependency through a public property of the client class.

public interface INofificationAction
{      
   void ActOnNotification(string message);
}
   class sam {  
       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)
       {
           //services srv = new services();
           //other oth = new other();
           //oth.run();
           //Console.WriteLine();
           EventLogWriter elw = new EventLogWriter();
           sam sm = new sam ();
           sm.notify(elw, "to logg");
           Console.ReadKey();
       }
   }

You cannot control when the dependency is set at all, it can be changed at any point in the object's lifetime. 

3) Method Injection-


the client class implements an interface which declares the method(s) to supply the dependency and the injector uses this interface to supply the dependency to the client class.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
  
namespace SampleProgram  
{  
    public interface Iset
    {
        void print();      
    }
    public class servic : Iset
    {
        public void print()
        {  
            Console.WriteLine("Print");          
        }      
    }
    public class client
    {
        private Iset _set;
        public void run(Iset serv)
        {  
            this._set = serv;
            Console.WriteLine("Hi");
            this._set.print();
        }      
    }
    class method
    {
        public static void Main()
        {
            client cn = new client();
            cn.run(new servic());
            Console.ReadKey();         
        }
    }
}

 Output:-


Hi
Print

Inheritance With Example

June 04, 2020

What is Inheritance ?


Acquiring (taking) the properties of one class into another class is called inheritance.

What Inheritance Provide ?


Inheritance provides reusability by allowing us to extend an existing class.

What is base & derived class ?


  • Base class - is the class from which features are to be inherited into another class.
  • Derived class - it is the class in which the base class features are inherited.

How to implement inheritance in C# ?


In below program Base class is Branch Class and Derived Class is Employee Class. 
We want to inherit Base Class (Branch class) Function into Derived Class ( Employee Class) using inheritance.

 public  class Branch         // Base  Class
    {
        int BranchId;
        string BranchName;
        string BranchAddress;

        public void GetBranchData()
        {
            Console.WriteLine("Enter Branch Id");
            BranchId =Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Enter Branch Name");
            BranchName = Console.ReadLine();
            Console.WriteLine("Enter Branch Address");
            BranchAddress = Console.ReadLine();
        }
        public void DisplayBranchData()
        {
            Console.WriteLine("Branch Details Are ");
            Console.WriteLine("Branch Id :" + BranchId);
            Console.WriteLine("Branch Name :" + BranchName);
            Console.WriteLine("Enter Branch Address :" + BranchAddress);
        }
    }

    class Employee : Branch    // Dervied Class
    {
        int EmpId;
        string EmpName;
        string EmpAddress;
        int Empage;

        public  void GetEmpData()
        {
            Console.WriteLine("Enter Employee Id");
            EmpId = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Enter Employee Name");
            EmpName = Console.ReadLine();
            Console.WriteLine("Enter Employee Address");
            EmpAddress= Console.ReadLine();
            Console.WriteLine("Enter Employee Age");
            Empage =Convert.ToInt32(Console.ReadLine());
        }
        public void DisplayEmpData()
        {
            Console.WriteLine("Employee Details Are ");
            Console.WriteLine("Employee Id :" + EmpId);
            Console.WriteLine("Employee Name :" + EmpName);
            Console.WriteLine("Employee Address :" + EmpAddress);
            Console.WriteLine("Employee Age :" + Empage);
        }
    }

    class Salary:Employee
    {
        double Basic, DA, HRA, Gross;
        public void GetSalary()
        {
            Console.WriteLine("Enter Basic Salary:");
            Basic = Convert.ToDouble(Console.ReadLine());
            DA=Basic*0.03;
            HRA=Basic*0.3;
            Gross = Basic + DA + HRA;
        }

        public void DisplaySalary()
        {
            Console.WriteLine("Salary Details Are ");
            Console.WriteLine("Basic Salary:"+Basic);
            Console.WriteLine("DA:"+DA);
            Console.WriteLine("HRA:" + HRA);
            Console.WriteLine("Gross Salary:" + Gross);
        }
    }

 class Program
    {
        static void Main(string[] args)
        {
            Salary ObjSalary = new Salary();
            ObjSalary.GetBranchData();
            ObjSalary.GetEmpData();
            ObjSalary.GetSalary();
            ObjSalary.DisplayBranchData();
            ObjSalary.DisplayEmpData();
            ObjSalary.DisplaySalary();
        }
    }

 Output :-


Enter Branch Id
1
Enter Branch Name
Axis
Enter Branch Address
Mumbai
Enter Employee Id
41
Enter Employee Name
John
Enter Employee Address
Mumbai
Enter Employee Age
25
Enter Basic Salary:
15000
Branch Details Are
Branch Id :1
Branch Name :Axis
Enter Branch Address :Mumbai
Employee Details Are
Employee Id :41
Employee Name :John
Employee Address :Mumbai
Employee Age :25
Salary Details Are
Basic Salary:15000
DA:450
HRA:4500
Gross Salary:19950
Press any key to continue . . .