Watch2- part 5–DI
The Watch2 NuGet package project now has interfaces, making it test-friendly and ready for action.
To make life easier, we’ve sprinkled some Dependency Injection (DI) magic throughout the project.
Classes have been refactored to embrace constructor injection, ensuring all necessary interfaces are on board.
The DI container has been set up in `Program.cs`, making dependency resolution a breeze. The tests have also been given a makeover to fit the new DI style.
This is a never-ending journey, but hey, it’s a pretty good start!
The program.cs now looks like this:
var serviceCollection = new ServiceCollection(); ConfigureServices(serviceCollection); var serviceProvider = serviceCollection.BuildServiceProvider(); var console = serviceProvider.GetRequiredService<IConsoleWrapper>(); var processManager = serviceProvider.GetRequiredService<ProcessManager>(); var startInfo = serviceProvider.GetRequiredService<IProcessStartInfo>(); await processManager.StartProcessAsync(args, console, startInfo); void ConfigureServices(IServiceCollection services) { services.AddSingleton<IConsoleWrapper, ConsoleWrapper>(); services.AddSingleton<ProcessManager, ProcessManager>(); services.AddSingleton<IProcessStartInfo>(provider => new ProcessStartInfoWrapper { FileName = "dotnet", Arguments = "watch " + string.Join(' ', args), WorkingDirectory = Environment.CurrentDirectory, RedirectStandardOutput = true, RedirectStandardInput = true, RedirectStandardError = true, UseShellExecute = false, CreateNoWindow = true }); services.AddLogging(loggingBuilder => { loggingBuilder.ClearProviders(); loggingBuilder.SetMinimumLevel(LogLevel.Trace); loggingBuilder.AddNLog("nlog.config"); }); }
Leave a Reply