Translate

Thursday, January 06, 2022

Dependency Injection

One of the places I interviewed asked to present a small write up on DI, expectation was to be small and concise, here's what I came up with: 

What is dependency injection in C#, and what are the benefits of dependency injection?


Dependency Injection is a software design principle and pattern that enables developers to write loosely coupled code. 

It supports the notion of program to an interface and not an implementation. 

Dependency Injection addresses following problems: 

- The application uses an interface or base class to abstract the dependency implementation 

- Registration of the dependecy in DI container, once all the implementations of the service are registered the DI container can be built

- Injection of the service into the constructor of the class where it's used. The DI framework takes care of instatiation and disposing of the instance. 

Here are some of the advantages of the DI : 

- Late Binding : Implemented Services can be swapped with other services if needed. This is helpful in case if there's a requirement to use ORACLE instead of SQL. 

- Extensibility : Code can extended and reused in ways not specifically planned for.

- Parallel Development: Since the components are dependent on each others interfaces the development can happen parallely. This is especially helpful in large organizations. 

- Testability: Classes can be unit tested. Since the dependcies are abstracted using interfaces writing unit tests on the classes become easier by using mocking frameworks. 

No comments:

Post a Comment