Perfect console application in .net Core: set up dependency injection

It may seem that when creating a console application we are doomed to use statics all over the code. Well.. we’re not! I’ll show you how to set up dependency injection and use it.

This is a part of a series of articles about writing a perfect console application in .net core 2. Feel free to read more:

Dependency Injection

There are many packages that can provide dependency injection, but I chose SimpleInjector, because I know it well. It’s also quite fast, according to Daniel Palme’s article. Here is how whole calss looks like:

Notice RegisterAllTypesWithConvention method – it is the way to register all interface implementations, that follows a simple naming convention. When interface will have an additional I in the name comparing to it’s class implementation, then it will be automatically registered. No need to remember about such silly things now!

No more statics in .net core all over the code 🙂

 All code posted here you can find on my GitHub:

3 thoughts on “Perfect console application in .net Core: set up dependency injection

    1. Luciano,
      It is possible however, I don’t think it’s the best idea. You can use ‘built it’ DI, which comes with Microsoft.Extensions.DependencyInjection package. Nevertheless, SimpleInjector offers much more and is quite fast.

Leave a Reply to Michał Białecki Cancel reply

Your email address will not be published. Required fields are marked *