Javatpoint Logo
Javatpoint Logo

Dependency Injection in C#

In this article, you will learn about the Decimal Injection in C# with their types and examples.

What is Dependency Injection?

The dependency injection or DI is a popular design pattern in C# and other programming languages. It is a method where a class's dependencies are obtained outside rather than developed inside the class. It encourages loose coupling between components, improving the code's flexibility, testability, and maintainability.

In other words, such requirements are supplied to the object from an external source, usually through a constructor, setter method, or Interface, rather than the object creating its dependencies or depending on a global state. It enables more modular, testable, and maintainable programming because objects may be readily replaced or tested separately.

Using a framework or container to handle the injection of dependencies is a typical way to accomplish dependency injection in object-oriented programming.

Types of Dependency Injection in C#

In C#, there are three different kinds of dependency injections.

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

Constructor Injection

In C#, constructor injection is a dependency injection (DI) in which a class receives dependencies through its constructor. Better testability and encapsulation are provided by this Method, which ensures that the class has all of its dependencies available at instantiation time.

Example:

Let us take an example to illustrate the dependency injection using constructor in C#.

Output:

Sending email to: [email protected]
Subject: Notification 
Body: This is a test notification of mail.

Explanation:

Constructor injection of dependency injection is demonstrated in this C# code. EmailService offers a practical implementation, starting with an IEmailServices interface that defines a contract for email services. NotificationService demonstrates constructor injection by passing an instance of IEmailServices as a parameter in its constructor. The email service dependency is injected into the NotificationService constructor by creating an instance of EmailService and passing it to it in the Main Method. Lastly, the injected dependency is used to execute the NotifyUser method on notificationService and send an email notification with the given user email and message.

Property Injection

In C#, property injection is another dependency injection (DI) in which a class receives dependencies via public properties. Dependencies can be specified after the object has been established via property injection, unlike constructor injection, which allows dependencies through the constructor.

Example:

Let us take an example to illustrate the dependency injection using property injection in C#.

Output:

Do Everything in Love..

Explanation:

In this example, this code demonstrates dependency injection using property injection. To begin, an ILogger interface is used to describe a logging requirement, and ConsoleLogger is used to provide a specific implementation. It is possible to inject the dependency outside because MyDemoClass encapsulates a Logger property of type ILogger. A ConsoleLogger object is assigned to the Logger property of a MyDemoClass instance generated in the Main Method. Lastly, the message "Do everything in love.." is printed to the console by the DoSomething method, which uses the injected logger.

Method Injection

Another type of dependency injection (DI) in C# is called method injection, in which dependencies are sent as parameters to a method. Instead of injecting dependencies at the class level, this Method provides for more granular control over dependency injection by allowing them to be provided to specific methods as needed.

Example:

Let us take an example to illustrate the dependency injection using method injection in C#.

Output:

Do Everything in Love...

Explanation:

Dependency injection (DI) via method injection is demonstrated in this C# code. An ILogger interface is first defined to abstract logging functionality. After that, the ConsoleLogger implements this Interface to log messages to the console. MyDemoClass encapsulates the DoSomething method, which allows external dependencies to be injected by taking an ILogger object as a parameter. A ConsoleLogger object is created in the Main Method and passed as an argument to the MyDemoClass DoSomething method to demonstrate how dependencies are injected into the class.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA