Mikpro.AutoInject
1.0.0
dotnet add package Mikpro.AutoInject --version 1.0.0
NuGet\Install-Package Mikpro.AutoInject -Version 1.0.0
<PackageReference Include="Mikpro.AutoInject" Version="1.0.0" />
paket add Mikpro.AutoInject --version 1.0.0
#r "nuget: Mikpro.AutoInject, 1.0.0"
// Install Mikpro.AutoInject as a Cake Addin
#addin nuget:?package=Mikpro.AutoInject&version=1.0.0
// Install Mikpro.AutoInject as a Cake Tool
#tool nuget:?package=Mikpro.AutoInject&version=1.0.0
AutoInject
The extension of Microsoft.Extensions.DependencyInjection to inject all services automatically for implementations.
Where to get?
The package deploy on Nuget and the code can view on Github. You can install from Package Management Console
with command as below:
Install-Package Micpro.AutoInject
Release Note for v1.0.0
- [New]
AddAutoInject
extensions method ofIServiceCollection
object. - [New]Resolve the service automatically while the instance implement from
ITransient
,IScoped
orISingleton
.
Why to use?
In DI of .NET Core
, user always inject the services in ConfigureServices
manually, but sometimes the would get the message Unable to resolve xxxxx from service
while running because forgot to inject to the container.
And if should have a lot of codes to add the service to the container like this:
services.AddTransient<xxx>();
services.AddTransient<xxx>();
services.AddScoped<xxx>();
services.AddSingletone<xxx>();
services.AddScoped<xxx>();
services.AddTransient<xxx>();
services.AddScoped<xxx>();
services.AddSingletone<xxx>();
services.AddScoped<xxx>();
services.AddTransient<xxx>();
services.AddSingletone<xxx>();
//.....
once you forget some service to inject, you will get the exception thrown <u>Unable to resolve xxxxxx from service</u>.
So I consider about the injection could be automatically.
How to use?
Just implement ITransient
,IScoped
or ISingleton
interface for your injection service, such as your class or interface that the service type you wanna be.
class MyService : ITransient { } //the service of class
or
interface IMyService : IScoped { } // the service of interface
class MyService : IMyService { }
The lifetime should be the similar name of ITransient
for transient, IScoped
for scoped and ISingleton
for singleton.
and they have the same called in ConfigureServices
like:
class MyService : ITransient
//same as
services.AddTransient<MyService>();
or
interface IMyService : IScoped { }
class MyService : IMyService { }
//same as
services.AddScoped<IMyService, MyService>();
Just call AddAutoInject
method in ConfigureServices
in Startup
class.
//scan all assembly
services.AddAutoInject();
//scan specify assemblies
services.AddAutoInject(new []{ Assembly.Load("XXXX.AAA"), Assembly.Load("XXXX.BBB") })
//scan specify search pattern of assemblies
services.AddAutoInject("XXX.YY.*");
//scan specify types
services.AddAutoInject(new []{ Type.LoadType("AAA.BB"), Type.LoadType("CC.DD") });
and you can get your service from constructor or injection container.
public MyController(IMyService service)//get the injected service
{
}
Support Generic Type
You will inject generic type manully like:
service.AddScoped(typeof(IRepository<>), typeof(Repository<>));
AutoInject also support this kind of expression without write any codes :
public interface IRepository<T> : IScoped
public interface IRepository<TSource, TResult> : ITransient
it will resolve automatically and inject the correct generic arguments for generic type
public MyController(IRepository<Entity> repository, IRepository<int, List<int>> repository2) //still get the generic type service
{
}
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 | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- Microsoft.Extensions.DependencyInjection (>= 2.0.0)
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 |
---|
- [Add]`AddAutoInject` extensions method of `IServiceCollection` object.
- [Add]Resolve the service automatically while the instance implement from `ITransient`,`IScoped` or `ISingleton`.