核心内容摘要
老司机成人网:重拾激情,点燃生活新火花
目录什么是 Injectio安装基础用法1️⃣ 创建接口和实现类2️⃣ 在入口程序中调用 Injectio方法注册注册特性Attributes特性可选属性重复注册策略注册策略示例单例服务作用域服务瞬态服务工厂注册开放泛型Open Generic泛型特性需 .NET
0Keyed 服务需 Microsoft.Extensions.DependencyInjection
0添加到容器注册标签Tags
总结在 .NET Core/Web 项目中手动写一堆services.AddScoped...、AddSingleton...是否让你头大今天给大家介绍一个神器——Injectio帮你自动扫描并注册服务减少重复代码让你的依赖注入DI更加优雅。
什么是 InjectioInjectio是一个自动服务注册库通过约定Convention 特性Attribute自动扫描并注册依赖服务。
简单理解•不用手动注册服务•支持 Singleton / Scoped / Transient•支持工厂方法、模块注册、泛型和 Keyed 服务适用于.NET 6/7/8 Web 或控制台项目。
安装dotnet add package Injectio或者添加到.csproj文件中PackageReference IncludeInjectio PrivateAssetsall /PrivateAssetsall表示这个包不会被依赖它的项目引用。
基础用法1️⃣ 创建接口和实现类public interfaceIMessageService { string Send(string message); } [RegisterSingleton] // 自动注册为单例 publicclassEmailMessageService : IMessageService { public string Send(string message) { Console.WriteLine($Email sent: {message}); return message; } }2️⃣ 在入口程序中调用 Injectio对于 .NET 6/7/8 最小 APIusing Injectio; var builder WebApplication.CreateBuilder(args); // 自动注册项目中的服务自动生成的类可以看截图 builder.Services.AddInjectioDemo(); var app builder.Build(); app.MapGet(/, (IMessageService messageService) { messageService.Send(Hello from Injectio!); returnMessage sent!; }); app.Run();或者在控制器中使用[ApiController] [Route([controller]/[action])] publicclassWeatherForecastController : ControllerBase { privatereadonly IMessageService _myService; public WeatherForecastController(IMessageService myService) { _myService myService; } [HttpGet(Name GetWeatherForecast
] public string Get2() { return _myService.Send(Hello from WeatherForecastController!); } }这样就不用再写services.AddScopedIMessageService, EmailMessageService()了方法注册public class RegistrationModule { [RegisterServices] public static void Register(IServiceCollection services) { services .AddOptionsPollingOption() .ConfigureIConfiguration((settings, configuration) configuration.GetSection(PollingOption.SectionName).Bind(settings) ); } }image注册特性Attributes在类上添加注册特性源生成器会自动发现并注册服务。
特性说明[RegisterSingleton]标记为单例服务[RegisterScoped]标记为作用域服务[RegisterTransient]标记为瞬态服务[RegisterServices]标记方法用于注册服务特性可选属性•ServiceType指定注册接口•ImplementationType指定实现类型•Factory使用工厂方法创建实例•Duplicate重复注册策略Skip / Replace / Append•Registration注册策略Self / ImplementedInterfaces / SelfWithInterfaces重复注册策略•Skip已存在的服务跳过注册•Replace替换已有的服务注册•Append在已有服务后追加注册注册策略•Self将具体类型注册为自身•ImplementedInterfaces注册为其实现的接口•SelfWithInterfaces注册为自身及实现的接口示例单例服务[RegisterSingleton] public class SingletonService : IService { }指定服务类型[RegisterSingleton(ServiceType typeof(IService))] public class SingletonService : IService { }支持IEnumerableT多服务解析[RegisterSingleton(Duplicate DuplicateStrategy.Append)] public class SingletonService : IService { }作用域服务[RegisterScoped] public class ScopedService : IService { }瞬态服务[RegisterTransient] public class TransientService : IService { }工厂注册[RegisterTransient(Factory nameof(ServiceFactory))] publicclassFactoryService : IFactoryService { privatereadonly IService _service; public FactoryService(IService service) { _service service; } public static IFactoryService ServiceFactory(IServiceProvider serviceProvider) { returnnew FactoryService(serviceProvider.GetServiceIService()); } }开放泛型Open Generic[RegisterSingleton(ImplementationType typeof(OpenGeneric), ServiceType typeof(IOpenGeneric))] public class OpenGenericT : IOpenGenericT { }版本