Dependency Injection in C#
Everyone loves it when their applications are running smoothly and require minimal maintenance. The onus of this lies on the programmers who are expected to write a maintainable code so that the existing codebases can be extended and used to release newer versions of the application.
And always remember, the more maintainable the code, the better it is to work with codebases effectively. An excellent approach to writing maintainable code is through a loose coupling. Loosely coupled code is easy to understand and with better testability. It has swapping components and is more scalable. Programmers can easily add new features to an application if the written code is maintainable.
An excellent way to write loosely coupled code is through Dependency Injection. We have covered dependency injection in this blog, if you want to know it in detail, we request you to have a look at our other blog on dependency injection by clicking here. In this post, we are going to understand how is Dependency Injection implemented in C#?
Table of Content:
- What is code dependency?
- What is dependency injection?
- How is Dependency Injection implemented in C#?
- Code implementation of dependency injection in C#
- Wrapping Up
What is code dependency?
An important concept that we need to understand is code dependencies. Code dependency can be termed as the dependency of various code components on each other. Code components can be in the form of different classes or services.
For example, if class X uses another class or service Y then X is dependent on Y. This is because class X cannot carry out its function without Y. X and Y are said to be coupled together and the coupling between different components can be either loosely coupled or tightly coupled. Code dependencies decrease code reuse which restricts writing well maintainable code. Loosely coupled code has weak dependencies and helps to increase code reuse. To achieve loose coupling among code components we use Dependency Injection. With that introduction, let us quickly recap what is dependency injection from our previous blog.
What is dependency injection?
First, let us understand what is dependency inversion principle and inversion of control.
The dependency Inversion Principle states that high-level modules should not depend on low-level modules and should rather depend on abstractions.
Abstraction should not be dependent upon details rather details should depend upon abstractions.
Inversion of Control enables to change the flow of programming from the normal flow. It helps to reduce dependencies in the codebase. IoC transfers the control of objects to a container or a framework and the dependencies among classes and modules are resolved at runtime by the framework.
Dependency Injection is a form of a software design pattern that enables us to write loosely coupled code and reduces tight coupling among components. The tight coupling has strong dependencies that restrict code reuse. Dependency Injection enables us to reduce the coupling and helps us to write well maintainable code. It strives to make code more flexible which allows easy testing of code.
It implements the Dependency Inversion Principle and implements Inversion of Control enabling us to reduce hard-coded dependencies and passing them during runtime.
Some of the advantages of Dependency Injection includes enabling developers to write well maintainable code by reducing tight coupling among software components. It also helps to improve code reusability and allows easy testing of code. Lastly, it enables swapping components and code flexibility and helps to change the workflow of an application or software.
How is Dependency Injection implemented in C#?
There are majorly three ways in which Dependency Injection can be implemented in C#:
- Constructor Injection – This method of implementation is one of the most widely used ways to implement DI. It is the process of injecting the dependency (the dependent class object) through the constructor. The injected component can be used anywhere within the class and it is highly recommended to be used when the injected dependency is being used across class methods.
- Setter or Property Injection – This is the method of injecting the dependent class object through a property. It could work without changing the object state and is mostly recommended to be used when a class has optional dependencies.
- Method Injection – It is the process of injecting the dependent class object through a class method. The dependency can be injected into a single method and can be useful when the whole class does not require dependency and is required only by a specific class method.
Code implementation of dependency injection in C#
As we have seen, dependency injection is a design pattern used to develop code that is loosely coupled. The main aim of dependency injection is to make your code easily maintainable. Let us assume that your client needs two classes for an application. Dependency Injection allows you to insert dependencies from outside the class. This feature of DI decreases class coupling and increases code reusability. Unit testing can also easily be carried out. Let us see how this is possible using Constructor Injection:
Suppose the two classes are called Application1 and Application2. The best way is to use an interface for data abstraction. Data abstraction enables making changes to the interface implemented without changing the code for Client class.
Code snippet 1: Dependency injected through the constructor of the class
Code Snippet 2: Passing injected dependency to the Client class
Through this post, you would have not only understood what dependency injection is, but also seen how dependency injection in C# can be implemented. Now you can try the methods for yourself and implement dependency injection in your software workflow.