5

Configure Dependency Injection for .NET 5 Console Applications

 2 years ago
source link: https://www.programmingwithwolfgang.com/configure-dependency-injection-for-net-5-console-applications/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Configure Dependency Injection for .NET 5 Console Applications

2 days ago2021-09-06T00:00:00+02:00 by Wolfgang Ofner

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 preconfigured 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 DefaulBuilder and also allows you to register your instances with the dependency injection module.

private static IHostBuilder CreateHostBuilder(string[] args) { return Host.CreateDefaultBuilder(args) .ConfigureServices((_, services) => services.AddTransient<IGreeter, ConsoleGreeter>() .AddTransient<IFooService, FooService>()); }

The AddTransient method configures dependency injection to create a new instance of the object everytime 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 instanciate a new object with it.

using var host = CreateHostBuilder(args).Build();

GreetWithDependencyInjection(host.Services);

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.

public static string GreetWithDependencyInjection(IServiceProvider services) { using var serviceScope = services.CreateScope(); var provider = serviceScope.ServiceProvider;

var greeter = provider.GetRequiredService<IGreeter>();

return greeter.Greet(); }

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.

public class ConsoleGreeter : IGreeter { private readonly IFooService _fooService;

public ConsoleGreeter(IFooService fooService) { _fooService = fooService; }

public string Greet() { _fooService.DoCoolStuff(); return "Hello World from the Console Greeter"; } }

This class takes an IFooService object in the constructor and then uses this object to call the DoCool() stuff method inside the Greet() method. This enebales developers to change the dependency injection configuration and therefore change the bevhavior 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.

Testing the Dependency Injection

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.

private static IHostBuilder CreateHostBuilder(string[] args) { return Host.CreateDefaultBuilder(args) .ConfigureServices((_, services) => services.AddTransient<IGreeter, ApiGreeter>() .AddTransient<IFooService, FooService>()); }

Testing the changed DI Configuration

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.

You can find the code of the demo on Github.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK