Smart.Inject
5.2.0-beta.1
dotnet add package Smart.Inject --version 5.2.0-beta.1
NuGet\Install-Package Smart.Inject -Version 5.2.0-beta.1
<PackageReference Include="Smart.Inject" Version="5.2.0-beta.1" />
<PackageVersion Include="Smart.Inject" Version="5.2.0-beta.1" />
<PackageReference Include="Smart.Inject" />
paket add Smart.Inject --version 5.2.0-beta.1
#r "nuget: Smart.Inject, 5.2.0-beta.1"
#:package Smart.Inject@5.2.0-beta.1
#addin nuget:?package=Smart.Inject&version=5.2.0-beta.1&prerelease
#tool nuget:?package=Smart.Inject&version=5.2.0-beta.1&prerelease
Smart.Inject - 智能 .NET 依赖注入框架
Smart.Inject 是一个轻量级、高效且功能强大的 .NET 依赖注入扩展库。它通过自动扫描程序集并基于特性(Attribute)注册服务,极大地简化了依赖注入的配置过程,让您的启动代码更整洁、更易于维护。
核心功能
- 🚀 自动扫描:自动扫描引用的程序集并注册服务,无需为每个服务手动编写注册代码。
- 🧩 灵活的注册策略:提供多种注册方案(
InjectScheme),可将服务注册为自身、实现的接口、基类或所有这些。 - ⏱️ 多种生命周期:通过直观的特性轻松支持
Singleton、Scoped和Transient生命周期。 - 🔧 自定义配置:通过实现
IServiceConfigure接口,允许进行高级的、自定义的注册。 - 🚫 排除支持:使用
[IgnoreInject]特性可以轻松地从自动注册中排除特定服务。 - ⚡ 高性能和线程安全:以性能为核心进行设计,使用缓存和线程安全操作,实现快速可靠的服务注册。
- 🎯 目标化扫描:智能过滤系统和第三方包程序集,以加快扫描过程。
安装
通过 NuGet 包管理器安装。
dotnet add package Smart.Inject
快速入门
定义您的服务接口和实现。
// 你的服务接口 public interface IMyService { void DoWork(); } // 你的服务实现 // 添加 [Service] 特性以标记它进行自动注册。 // 默认情况下,它以 Scoped 生命周期和 'Default' 方案注册。 [Service] public class MyService : IMyService { public void DoWork() { Console.WriteLine("工作完成!"); } }在
Program.cs中调用ScanRegisterServices。var builder = WebApplication.CreateBuilder(args); // 将服务添加到容器中 builder.Services.ScanRegisterServices(builder.Configuration); var app = builder.Build(); // ... 使用你的服务 using (var scope = app.Services.CreateScope()) { var myService = scope.ServiceProvider.GetRequiredService<IMyService>(); myService.DoWork(); } app.Run();
就这样!Smart.Inject 会扫描您的程序集,找到 MyService,并将其与对应的接口 IMyService 一起注册。
高级用法
生命周期特性
使用特定的特性来定义服务的生命周期。它们都接受一个可选的 InjectScheme 参数。
[ServiceOfTransient(InjectScheme scheme = InjectScheme.Default)]: 以 Transient 生命周期注册服务。[ServiceOfScoped(InjectScheme scheme = InjectScheme.Default)]: 以 Scoped 生命周期注册服务。[ServiceOfSingleton(InjectScheme scheme = InjectScheme.Default)]: 以 Singleton 生命周期注册服务。[Service(ServiceLifetime lifetime, InjectScheme scheme)]: 用于完全控制的基础特性。
示例:
[ServiceOfSingleton]
public class MySingletonService : IMyService { }
注册方案 (InjectScheme)
InjectScheme 决定了服务如何在 DI 容器中注册。
Default: 将服务与其实现的接口一起注册。如果没有实现接口,则注册其自身。OnlySelf: 仅将服务注册为自身(实现类)。OnlyInterfaces: 仅将服务注册为其实现的所有接口。OnlyBaseClass: 将服务注册为其直接基类。All: 将服务注册为自身、其所有实现的接口及其基类。Customized: 使用特性中ServiceType属性指定的自定义类型来注册服务。
示例:
// 仅将 MyService 注册为自身,而不是 IMyService
[Service(scheme: InjectScheme.OnlySelf)]
public class MyService : IMyService { }
// 使用自定义类型进行注册
[Service(scheme: InjectScheme.Customized, ServiceType = new[] { typeof(IMyCustomService) })]
public class MyService : IMyCustomService { }
忽略服务
要防止某个类被自动注册,请使用 [IgnoreInject] 特性。
[IgnoreInject]
public class InternalService // 这个类不会被注册
{
// ...
}
高级自定义配置
对于复杂的场景,您可以实现 IServiceConfigure 接口。Smart.Inject 会找到您的配置类,实例化它,并执行 Configure 方法。
public class MyCustomServiceConfiguration : IServiceConfigure
{
public void Configure(IServiceCollection services, IConfiguration configuration)
{
// 在这里添加任何自定义注册逻辑
// 例如,使用工厂或选项注册服务
var connectionString = configuration.GetConnectionString("Default");
services.AddSingleton(new MyDbConnection(connectionString));
}
}
这个类将被 ScanRegisterServices 自动发现并执行。
Smart.Inject - An Intelligent .NET Dependency Injection Framework
Smart.Inject is a lightweight, efficient, and powerful .NET dependency injection extension library. It simplifies the DI configuration process by automatically scanning assemblies and registering services based on attributes, making your startup code cleaner and more maintainable.
Features
- 🚀 Automatic Scanning: Automatically scans referenced assemblies and registers services, no manual registration needed for each service.
- 🧩 Flexible Registration Strategies: Provides multiple registration schemes (
InjectScheme) to register services as themselves, as implemented interfaces, as base classes, or all of them. - ⏱️ Multiple Lifetimes: Supports
Singleton,Scoped, andTransientlifetimes through intuitive attributes. - 🔧 Custom Configuration: Allows for advanced, custom registration logic by implementing the
IServiceConfigureinterface. - 🚫 Exclusion Support: Easily exclude specific services from automatic registration using the
[IgnoreInject]attribute. - ⚡ High Performance & Thread-Safe: Designed with performance in mind, using caching and thread-safe operations for fast and reliable service registration.
- 🎯 Targeted Scanning: Intelligently filters out system and package assemblies to speed up the scanning process.
Installation
Install the package from NuGet Package Manager.
dotnet add package Smart.Inject
Quick Start
Define your service and implementation.
// Your service interface public interface IMyService { void DoWork(); } // Your service implementation // Add the [Service] attribute to mark it for automatic registration. // By default, it's registered as Scoped and with the 'Default' scheme. [Service] public class MyService : IMyService { public void DoWork() { Console.WriteLine("Work done!"); } }Call
ScanRegisterServicesin yourProgram.cs.var builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.ScanRegisterServices(builder.Configuration); var app = builder.Build(); // ... use your services using (var scope = app.Services.CreateScope()) { var myService = scope.ServiceProvider.GetRequiredService<IMyService>(); myService.DoWork(); } app.Run();
That's it! Smart.Inject will scan your assemblies, find MyService, and register it with its corresponding interface IMyService.
Advanced Usage
Lifetime Attributes
Use specific attributes to define the service lifetime. All of them accept an optional InjectScheme.
[ServiceOfTransient(InjectScheme scheme = InjectScheme.Default)]: Registers the service with a transient lifetime.[ServiceOfScoped(InjectScheme scheme = InjectScheme.Default)]: Registers the service with a scoped lifetime.[ServiceOfSingleton(InjectScheme scheme = InjectScheme.Default)]: Registers the service with a singleton lifetime.[Service(ServiceLifetime lifetime, InjectScheme scheme)]: The base attribute for full control.
Example:
[ServiceOfSingleton]
public class MySingletonService : IMyService { }
Registration Schemes (InjectScheme)
The InjectScheme determines how a service is registered in the DI container.
Default: Registers the service with its implemented interfaces. If no interfaces are implemented, it registers itself.OnlySelf: Registers the service only as itself (the implementation class).OnlyInterfaces: Registers the service only with all its implemented interfaces.OnlyBaseClass: Registers the service with its direct base class.All: Registers the service as itself, all its implemented interfaces, and its base class.Customized: Registers the service with custom types specified in theServiceTypeproperty of the attribute.
Example:
// Register MyService only as itself, not as IMyService
[Service(scheme: InjectScheme.OnlySelf)]
public class MyService : IMyService { }
// Register with a custom type
[Service(scheme: InjectScheme.Customized, ServiceType = new[] { typeof(IMyCustomService) })]
public class MyService : IMyCustomService { }
Ignoring a Service
To prevent a class from being automatically registered, use the [IgnoreInject] attribute.
[IgnoreInject]
public class InternalService // This will not be registered
{
// ...
}
Advanced Custom Configuration
For complex scenarios, you can implement the IServiceConfigure interface. Smart.Inject will find your configuration class, instantiate it, and execute the Configure method.
public class MyCustomServiceConfiguration : IServiceConfigure
{
public void Configure(IServiceCollection services, IConfiguration configuration)
{
// Add any custom registration logic here
// For example, register a service with factory or options
var connectionString = configuration.GetConnectionString("Default");
services.AddSingleton(new MyDbConnection(connectionString));
}
}
This class will be automatically picked up by ScanRegisterServices.
> Developed by zenglei
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net6.0 is compatible. 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 is compatible. 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. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net6.0
- Microsoft.Extensions.Configuration.Abstractions (>= 9.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 9.0.0)
- Microsoft.Extensions.DependencyModel (>= 9.0.0)
- Microsoft.Extensions.Hosting.Abstractions (>= 9.0.0)
-
net8.0
- Microsoft.Extensions.Configuration.Abstractions (>= 9.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 9.0.0)
- Microsoft.Extensions.DependencyModel (>= 9.0.0)
- Microsoft.Extensions.Hosting.Abstractions (>= 9.0.0)
-
net9.0
- Microsoft.Extensions.Configuration.Abstractions (>= 9.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 9.0.0)
- Microsoft.Extensions.DependencyModel (>= 9.0.0)
- Microsoft.Extensions.Hosting.Abstractions (>= 9.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 |
|---|---|---|
| 5.2.0-beta.1 | 204 | 11/10/2025 |
| 5.1.1 | 175 | 10/15/2025 |
| 5.1.0 | 190 | 7/13/2025 |
| 5.0.0 | 156 | 4/5/2025 |
| 4.0.0 | 169 | 3/27/2025 |
| 3.1.0 | 215 | 3/20/2025 |
| 3.0.3 | 195 | 3/16/2025 |
| 3.0.2 | 143 | 2/26/2025 |
| 3.0.1 | 167 | 2/15/2025 |
| 3.0.0 | 155 | 2/15/2025 |
| 2.0.3 | 145 | 2/13/2025 |
| 2.0.2 | 154 | 2/9/2025 |
| 2.0.1 | 178 | 12/7/2024 |
| 2.0.0 | 164 | 11/26/2024 |
| 1.0.0.5 | 154 | 10/9/2024 |
| 1.0.0.4 | 157 | 9/25/2024 |
| 1.0.0.3 | 178 | 9/24/2024 |
| 1.0.0.2 | 158 | 9/20/2024 |
| 1.0.0.1 | 160 | 9/20/2024 |
| 1.0.0 | 180 | 9/8/2024 |