How to register all existing assignable types in an assembly in .Net Core DI

  • .Net Core
  • Dependency injection
  • Mehdi Mohseni
  • February 9, 2020
  • Home
  • Blog
  • How to register all existing assignable types in an assembly in .Net Core DI

Introduction

.Net Core came with its own Dependency Injection container and .Net Core 3.0 has shown that the .Net Core DI can be reliable and efficient. It still has some shortcomings. There are many extensions to fix these shortcomings that provide excellent opportunities. Basically, .Net core has made it easy to write and extend it.

In this article, I will show you how to add a new extension to register all assignable types to a specific interface (Class: Interface). This will be helpful if you have different logics for a task and want to store them in different classes and load them by configuration.

Background

Let's say you have a specific logic to calculate employees' tax and you have several types of employees and each employee type has its own logic. So, you will have an interface and each employee type should implement the interface. Users should have this feature to add a new employee type, which means the user needs to add new logic for the new employee type too. So, base one Open-Close principle we don't want to open the main code for changes but we need to make a way for the user to add the new logic. So, we add an interface layer and a project to add new classes. When the user adds a new logic, he should restart the app the DI will load all classes in that project which implements that interfaces. Here, our nice extension will play its role.

Using the Code

As you can see the extension is really simple and easy but it is doing magic ;)

The first method, RegisterAllAssignableTypes will be used when you are configuring the Dot Net core DI. So you will have a line like this:

And it will read all the classes in the MyApp.Extended project which implements ITaxCalculator and registers them as Transient in DI container. So, in the consumer class, you just need to add IServiceProvider to the constructor.

Now, when you load an employee from the database and know its type you can easily load its tax calculation logic by this extension method

Points of Interest

I've learned more about .Net Core DI which seems that will be a new generation in .Net applications.

You can access this code NuGet: MOME.DotNetDIExtensions

I would be happy if you use it and report its issue and recommend your ideas about how we can improve it and which DI extension you need. So, I can add them to this package and develop and improve it.

Related Post