Author : MD TAREQ HASSAN | Updated : 2021/05/29
What Is Dependency Injection?
- Dependency Injection (DI) is a design pattern used to implement IoC (inversion of control)
- In software engineering, dependency injection is a technique in which an object receives other objects that it depends on
- Dependency Injection is a software design concept that allows a service to be used/injected in a way that is completely independent of any client consumption
- Dependency injection separates the creation of a client’s dependencies from the client’s behavior, which allows program designs to be loosely coupled
Simple Console App
- In ASP.Net (core or Net5.0+) app, DI and other things are configured automatically (generic Host builder) by framework
- In console app, we will setup DI manually and once setup is done, DI will work in the same way it works in ASP.Net or other frameworks
- Followings classes will be created in simple console app
IFoo
interface andFoo
classEntryPoint.cs
(similar to worker in Worker Service, see below)Startup.cs
Program.cs
Nuget Package
Install-Package Microsoft.Extensions.DependencyInjection
Services/IFoo.cs
namespace ConsoleApp.Simple.Services
{
public interface IFoo
{
int GetRandomNumber(int start = 1, int end = 10);
}
}
Services/Foo.cs
using System;
namespace ConsoleApp.Simple.Services
{
public class Foo : IFoo
{
public int GetRandomNumber(int start, int end)
{
return new Random().Next(start, end);
}
}
}
EntryPoint.cs
using ConsoleApp.Simple.Services;
using System;
using static System.Diagnostics.Debug;
namespace ConsoleApp.Simple
{
public class EntryPoint
{
private IFoo _foo;
public EntryPoint(IFoo foo)
{
_foo = foo;
}
public void Run(String[] args)
{
WriteLine("\n\n\n\n\n");
WriteLine("=====================================================================================\n\n");
WriteLine("DI in console app is working...\n");
var randomNumber = _foo.GetRandomNumber();
WriteLine($"Foo.GetRandomNumber(): {randomNumber}");
WriteLine("\n\n=================================================================================");
WriteLine("\n\n\n\n\n");
Console.Read();
}
}
}
Startup.cs
using System;
using ConsoleApp.Simple.Services;
using Microsoft.Extensions.DependencyInjection;
namespace ConsoleApp.Simple
{
public static class Startup
{
public static IServiceCollection ConfigureServices()
{
var serviceCollection = new ServiceCollection();
serviceCollection.AddScoped<IFoo, Foo>();
serviceCollection.AddTransient<EntryPoint>();
return serviceCollection;
}
}
}
Program.cs
using Microsoft.Extensions.DependencyInjection;
namespace ConsoleApp.Simple
{
class Program
{
static void Main(string[] args)
{
//Console.WriteLine("Hello World!");
var services = Startup.ConfigureServices();
var serviceProvider = services.BuildServiceProvider();
serviceProvider.GetService<EntryPoint>().Run(args);
}
}
}
Using Generic Host Builder
- https://docs.microsoft.com/en-us/dotnet/core/extensions/generic-host
- https://docs.microsoft.com/en-us/dotnet/core/extensions/dependency-injection-usage
- https://docs.microsoft.com/en-us/dotnet/core/extensions/configuration#configure-console-apps