Dependency Injection in C#

Dependency Injection in C#

Tudip

Tudip

29 January 2020

Dependency Injection in C#

A lot of developers get confused when they hear the term, Dependency Injection. This heavy word brings the confusion about its purpose, its usage and its procedures. To understand the term Dependency Injection, first we should know the answer of the below question.

What is Dependency? 

In consideration of the object oriented programming, suppose Class One requires Class Two for one of its job, that means Class Two is a dependency of Class One.

Before getting started with DI let me inform you about Dependency Inversion Principle (DIP) and Inversion of Control (IoC) on which Dependency Injection is based.

Dependency Inversion Principle 

DIP states about the coupling between different modules and classes, it precisely talks about the approach that high level modules should not depend on the low level modules rather they should depend on the abstraction.

Inversion of Control

IoC is programming style in which the flow of the programming is changed from the normal way. Using this style, the dependencies between the modules or classes are registered and resolved at runtime by any framework. Previous line clearly defines that IoC changed the programming from the normal way.

Now moving to Dependency Injection, it is a software design pattern which implements the Dependency Inversion Principle. It allows to make loosely coupled and maintainable code. It also implements the Ioc by reducing the hard coded dependencies between the classes and passing those dependencies at run time.

Following are the three ways of implementing dependency injection:

  1. Constructor Injection
  2. Property Injection
  3. Method Injection

Let have a walkthrough with each of these:

1. Constructor Injection:

Here we are discussing about the dependency pattern which is most commonly used. In this process, one parameterized constructor is used as we have to pass a value (dependency of the class) at the time of object creation.

Let’s understand this with the below example:

namespace propertyinjuction { 

public interface text { 

void print(); 

} class format : text { 

public void print() { 

Console.WriteLine(" here 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(); 

} } }

2. Property Injection:

This dependency pattern is not as much used as the previous one but there are some cases where only Property Injection is used. It is also called Setter Injection.

Let’s understand this with an example.

public interface INofificationAction { 

void ActOnNotification(string message); 

} 

class One { 

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(); One one = new One(); at.notify(elw, "to logg"); Console.ReadKey(); } }

3. Method Injection:

This dependency pattern is also not frequently used as Constructor Injection. There are some set of requirements very it is useful. In this pattern, dependencies to a class is provided through the methods and that method could be a Interface method or a Class method.

Let’s understand this pattern with the below example:

public class UserLogic { 

private GoogleOAuthService _authService; 

public UserLogic() { 

_authService = new GoogleOAuthService(); _emailService = new OutlookEmailService() // or Google; } public void Register(string emailAddress, string password, IEmailService emailService) 

{ 

var authResult = _authService.RegisterUser(emailAddress,password); emailService.SendMail(emailAddress, authResult.ConfirmationMessage); } }

Following are some IoC containers that are used to implement Dependency Injection:

  • AutoFac
  • Unity
  • Castle Windsor
  • StructureMap
  • Ninject

Summary:

To summarize this article here, we learned about the dependencies between two class or software components. We also walked through the Dependency Of Inversion principle and Inversion of Control. About Dependency Injection, we learned that it helps us writing maintainable, clean and loosely coupled code. It also increases code reusablity and improves testing of code.

search

Blog Categories

Request a quote