EasyDI 1.0.0
dotnet add package EasyDI --version 1.0.0
NuGet\Install-Package EasyDI -Version 1.0.0
<PackageReference Include="EasyDI" Version="1.0.0" />
paket add EasyDI --version 1.0.0
#r "nuget: EasyDI, 1.0.0"
// Install EasyDI as a Cake Addin #addin nuget:?package=EasyDI&version=1.0.0 // Install EasyDI as a Cake Tool #tool nuget:?package=EasyDI&version=1.0.0
EasyDI in .Net core
What if you could register your DI services inside your service interface just like this
[InjectThisServiceAs(ServiceLifetime.Transient)]
public interface IServiceA
{
void Do();
}
well you can 😃
Install EasyDI package
dotnet add package EasyDI
And then just add this line to your ConfigureService method of your Startup.cs like this
services.AddAnnotatedServices(typeof(Startup));
and you are good to go. In every service interface by using the InjectThisServiceAs annotation and passing your desired service lifetime, your service will be automatically registered in the dotnet core DI engine, isn't it cool 😃
checkout the source code here
install the Nuget package from here
Why EasyDI ?
1. To solve the forgotten service in DI registry
you only need to add the
services.AddAnnotatedServices(typeof(Startup));
onetime and afterwards your job with the Startup.cs for typical service registering is done. How many times have you run your .Net core application and then boom! you got an error message like this
System.InvalidOperationException: Unable to resolve service for type ...
at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetService(IServiceProvider sp, ...
And after checking out your code you figured out that the reason was that you have forgot to register your service(s) to the DI engine. This can be so annoying sometimes considering that you have already coded your service interface and its implementation and even used it by constructor injection in your controller or another service
public interface IServiceA
{
void Do();
}
public class ServiceA :IServiceA
{
void Do(){
// implementation
}
}
public class TestController : ControllerBase
{
private readonly IServiceA _serviceA;
public TestController(IServiceA serviceA)
{
_serviceA = serviceA;
}
}
And the only missing part of the puzzle was
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
...
services.AddScoped<IServiceA, ServiceA>();
...
}
}
2. To solve the service registry hell issue
After your application begin to grow, you will need to add more and more services to your application to take care of the new functionalities. However, as you add more services to the application you need to register them in DI as well somewhere in the Startup.cs or an extension class. No matter how clean you try to be in terms of the folder structure and naming, after a while you end up having a class with hundreds of line like this
services.AddTransient<IServiceA, ServiceA>();
services.AddScoped<IServiceB, ServiceB>();
services.AddSingleton<IServiceC, ServiceC>();
services.AddScoped<IServiceD, ServiceD>();
services.AddScoped<IServiceE, ServiceE>();
services.AddScoped<IServiceF, ServiceF>();
services.AddScoped<IServiceH, ServiceH>();
services.AddScoped<IServiceI, ServiceI>();
services.AddScoped<IServiceJ, ServiceJ>();
.
.
.
.
by using the EasyDI, you distribute this registration to each interface of your service, therefore you do not need to spend any effort to organize these registrations plus when you open your service interface you have a goof idea that what is the service life time of that service, isn't it cool 😃
checkout the source code here
install the Nuget package from here
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp3.1 is compatible. |
-
.NETCoreApp 3.1
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.0.0 | 1,566 | 11/26/2020 |