Back in the .NET framework days dependency injection was not the easiest task. Fortunately, Microsoft made it a first class citizen with the introduction of .NET Core. This is especially true for ASP .NET Core application where dependency injection can be used with a single line of code.
Unfortunately, .NET Core (and .NET 5) console applications do not come pre-configured for dependency injection. Therefore, this post will show you how to configure it for you .NET 5 console application.
Configure Depndency Injection for a .NET 5 Console Application
You can find the code of the demo on GitHub.
Create a new .NET 5 (.NET Core also works) application and install the Microsoft.Extensions.Hosting NuGet package. Next, create the following method which creates a DefaultBuilder and also allows you to register your instances with the dependency injection module.
The AddTransient method configures dependency injection to create a new instance of the object every time it is needed. Alternatively, you could use AddScoped or AddSingleton. Scoped objects are the same within a request, but different across different requests and Singleton objects are the same for every object and every request.
You can add as many services as your application needs.
Use Dependency Injection in a .NET 5 Console Application
The dependency injection module is already configured and now you can use it. Pass the Services of the previously created IHostBuilder to a new method where you can instantiate a new object with it.
The GreetWithDependencyInjection method uses dependency injection to create a new instance of the IGreeter class. Since I previously configured to use the ConsoleGreeter for the IGreeter interface, I get an instance of ConsoleGreeter. This is normal dependency injection functionality and nothing console application specific though.
After the object is instantiated, you can use it like any other object and, for example, call methods like Greet on it.
Use Constructor Injection
Creating objects using dependency injection is good but it is still too complicated. It would be better if the needed objects are created automatically in the constructor. This is where constructor injection comes in handy. Again, this is nothing console application specific, just good old dependency injection but you create a class that accepts one or more interfaces in the constructor. The dependency injection module automatically passes the right object into the constructor without any developers work needed.
Let’s take a look at the ConsoleGreeter class.
This class takes an IFooService object in the constructor and then uses this object to call the DoCoolStuff() method inside the Greet() method. This enables developers to change the dependency injection configuration and therefore change the behavior of the application without touching the classes that use these objects.
Testing the Dependency Injection
Start the application and you should see the following output in your console window.
When you replace the ConsoleGreeter with the ApiGreeter in the dependency injection configuration and start the program again, you should see a different greeting message.
Conclusion
Dependency injection helps developers to write more testable and overall better applications. .NET 5 and .NET Core do not come with DI pre-configured but as you have seen in this post, it is very easy to configure it. All you have to do is to install the Microsoft.Extensions.Hosting NuGet package and add a couple lines of code.
In my next post, I will add unit tests and how you how to configure the dependency injection in the test project.
You can find the code of the demo on GitHub.
Comments powered by Disqus.