Articles → .NET → Dependency Injection in C#

Dependency Injection in C#






Purpose of Dependency Injection





Constructor Injection




public class MyClass {
  public void display() {
    Console.WriteLine("Display Function for MyClass");
  }
}

public class TighlyCoupledCode {
  public void CallMethod() {
    MyClass myClass = new MyClass();
    myClass.display();
  }
}






public class MyClass {
  public void display() {
    Console.WriteLine("Display Function for MyClass");
  }
}

public class LooselyCoupledCode {
  MyClass _myClass;
  public LooselyCoupledCode(MyClass myClass) {
    this._myClass = myClass;
  }
  public void CallMethod(MyClass myClass) {
    _myClass.display();
  }
}





Posted By  -  Karan Gupta
 
Posted On  -  Monday, December 28, 2020

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250