সোমবার, ১৮ নভেম্বর, ২০১৩

Inversion of Control- Part-02 (Dependency Injection)



·         Dependency Injection use K‡i Avgiv GKwU class Gi g‡a¨ interface ‡K inject Ki‡Z cvi‡ev|
·         G‡ÿ‡Î Dependency Injection use K‡i Avgiv OrderService class Gi g‡a¨ IOrderSaver interface ‡K inject Ki‡Z cvi‡ev|
·         GB Injection nq‡Zv constructor injection bZzev property injection n‡Z cv‡i-

Constructor injection
Constructor Injection GKwU DI technique hvnv‡Z object Gi constructor Gi ga¨ w`‡q dependencie mg~n Pass Kiv nq-





·         Dc‡iv³  diagram constructor injection implementation show K‡i|
·         Diagram G e¨eüZ item mg~‡ni weeiY wb¤œiƒc-
o    Dependent:
§  GB classwU  dependent class ‡K represent K‡i|
§  Constructor Gi ga¨ w`‡q dependency injected nq Ges private field G stored _v‡K|
    • IDependency.
      • Dependency class G implement Kivi Rb¨ member mg~n describe K‡i|
    • Dependency.
      • GB class IDependency interface G described member mg~n implement K‡i|
      • GB class g~jZ: dependency represent K‡i hvnv dependent object G injected n‡e|
Advantages
  • Construction Injection Øviv strong dependency contract ‰Zix Kiv hvq|
  • †h‡nZz dependencie mg~n constructor Gi ga¨ w`‡q pass Kiv hvq ZvB Construction Injection testing support  K‡i|
  • Construction Injection use K‡i circular dependency prevent Kiv hvq|
Disadvantages
  • Entire dependency graph †K up front wiring Ki‡Z nq|
Example
·         wb‡¤œ constructor injection Gi diagram illustrate Kiv n‡jv-




·         Constructor injection process G Avgiv OrderService class Gi constructor Gi parameter wnmv‡e IOrderSaver interface Giinstance pass Kiv nq|
·         wb‡¤œi code G Avgiv Constructor injection processimplement K‡iwQ|
publicclassOrderService
{
privateIOrderSaver orderSaver;
 
publicOrderService(IOrderSaver orderSaver)
{
  this.orderSaver = orderSaver;
}
 
publicvoidAcceptOrder(Order order)
{
//Domain logic such as validation
 
        orderSaver.SaveOrder(order);
}
}
·         Dc‡iv³example code G Avgiv †`L‡Z cvw”Q †h
o   cÖ_‡g Avgiv GKwUorderSaverbv‡gi GKwUprivate instance defineK‡iwQ hvitype IOrderSaver
o   Then OrderService class Gi constructor Gi Parameter wnmv‡e Avgiv Gi IOrderSaverinterface Gi GKwU instance pass Kwi i.e. inject Kwi-
o   Then Private instance Gi g‡a¨ injected instance ‡K pass Kwi|
·         AcceptOrder method Gi g‡a¨ injected instance use K‡i SaveOrder()method call Kiv nq|
·         GQvov constructor Gi ga¨ w`‡q IOrderSaver Gi instance pass bv K‡iI Avgiv IOrderSaver Gi instance use K‡i Avgiv SaveOrder() method call Ki‡Z cvwi|
·         G‡ÿ‡Î constructor Gi  g‡a¨ Avgiv OrderDatabase class Gi GKwU instance create Ki‡ev wb¤œiƒ‡c-
·         wKš‘ Gfv‡e instance create use K‡i SaveOrder() method call Ki‡j OrderService class OrderDatabase class Gi Dci depend n‡q hv‡e GQvov unit test I RwUj n‡q hvq| myZivs Bnv Wise bv|
publicOrderService()
{
this.orderSaver =newOrderDatabase();
}


Setter Injection/Property Injection


  •  G‡ÿ‡Î public properties G dependencie mg~n set Kiv nq| 
  • d‡j constructor modify bv K‡i dependency injection use Kiv hvq|
·         Dc‡iv³ UML class diagram GKwU setter injection implementation describe K‡i|
·         Diagram G e¨eüZ item mg~‡ni weeiY wb¤œiƒc-
    • Dependent
      • GB class Øviv dependent class ‡K represent Kiv nq|
      • Dependency property use  K‡i dependency injected nq|
      • GKwU private field G dependency stored _v‡K|
    • IDependency
      • Dependency class G implement Kivi Rb¨ member mg~n describe K‡i|
    • Dependency
      • GB class IDependency interface G described member mg~n implement K‡i|
      • GB class g~jZ: dependency represent K‡i hvnv dependent object G injected n‡e|
Advantages
  • Entire dependency graph ‡K up front wiring Kivi cÖ‡qvRb c‡o bv|
Disadvantage
  • Required dependencie mg~n identify Kiv difficult

Ø  wb‡¤œ Setter Injection/Property Injection Gi D`vnib †`qv n‡jv :

using System;

namespace DependencyInjection
{
    public interface IDependentClass
    {
        void DoSomethingInDependentClass();
    }
     
    public class DependentClass1 : IDependentClass
    {
        public void DoSomethingInDependentClass()
        {
            Console.WriteLine("Hello from DependentClass1: I can be injected into MainClass");
        }
    }

    public class DependentClass2 : IDependentClass
    {
        public void DoSomethingInDependentClass()
        {
            Console.WriteLine("Hello from DependentClass2: I can be injected as well, just change App.Config");
        }
    }

public class MainClass
    {
        IDependentClass dependentClass;
        public IDependentClass DependentClass { 
              get { return dependentClass; } 
              set { dependentClass = value; } 
     }
         public void DoSomething()
        {
            dependentClass.DoSomethingInDependentClass();
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            // Get the correct dependency based on configuration file
            IDependentClass dependency = GetCorrectDependency();

            // Create our main class and inject the dependency
            MainClass mainClass = new MainClass();
            mainClass.DependentClass = dependency;

            // Use the main class, the method references the dependency
            // so behaviour depends on the configuration file
            mainClass.DoSomething();

            Console.ReadLine();
        }

      
  /// <summary>
        /// Instantiate and return a class conforming to the IDependentClass interface:
        /// which class gets instantiated depends on the ClassName setting in
        /// the configuration file
        /// </summary>
        /// <returns>Class conforming to the IDependentClass interface</returns>
        static IDependentClass GetCorrectDependency()
        {
            string classToCreate = System.Configuration.ConfigurationManager.AppSettings["ClassName"];
            Type type = System.Type.GetType(classToCreate);
            IDependentClass dependency = (IDependentClass)Activator.CreateInstance(type);
            return dependency;
        }
    }
}






 


কোন মন্তব্য নেই:

একটি মন্তব্য পোস্ট করুন