Smart.Inject
3.0.1
dotnet add package Smart.Inject --version 3.0.1
NuGet\Install-Package Smart.Inject -Version 3.0.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Smart.Inject" Version="3.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Smart.Inject --version 3.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Smart.Inject, 3.0.1"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Smart.Inject as a Cake Addin #addin nuget:?package=Smart.Inject&version=3.0.1 // Install Smart.Inject as a Cake Tool #tool nuget:?package=Smart.Inject&version=3.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Smart.Inject
专为.NET 6/8/9设计的智能依赖注入框架,能扫描程序集并自动注册自定义服务,可对程序集的服务进行个性化配置
核心特性
- 零配置自动注册:自动扫描程序集注册服务
- 生命周期管理:通过
[ServiceOf*]
特性标记Singleton/Scoped/Transient - 智能过滤:
[IgnoreInject]
排除特定类型 - 模块化配置:
IServiceConfigure
实现按程序集配置 - 策略定制:支持6种服务注册策略(All/Default/OnlyBaseClass等)
快速安装
bash
dotnet add package Smart.Inject
基础用法
csharp
// 启动配置
services.AddSmartInject()
.AddAssembly(Assembly.GetExecutingAssembly())
.ScanCustomizedServices(configuration);
// 自动注册示例
[ServiceOfScoped(InjectScheme.OnlyInterfaces)]
public class OrderService : IOrderService { /* 自动注册为IOrderService */ }
// 排除注册示例
[IgnoreInject]
public class InternalService : IHostedService { /* 不注册 */ }
高级配置
csharp
// 自定义注册策略
[Service(Lifetime = ServiceLifetime.Singleton,Scheme = InjectScheme.Customized,ServiceType = new[] { typeof(ILogger) })]
public class CustomLogger : ILogger { /* 显式注册 */ }
// 模块化配置
public class AuthConfigure : IServiceConfigure
{
public void Configure(IServiceCollection services, IConfiguration config)
{
services.AddJwtAuth(config);
}
}
最佳实践
- 使用
InjectScheme.OnlyInterfaces
强制面向接口编程 - 在类库中实现
IServiceConfigure
实现自治配置
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. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net6.0
- Microsoft.Extensions.Configuration.Abstractions (>= 9.0.2)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 9.0.2)
- Microsoft.Extensions.DependencyModel (>= 9.0.2)
- Microsoft.Extensions.Hosting.Abstractions (>= 9.0.2)
-
net8.0
- Microsoft.Extensions.Configuration.Abstractions (>= 9.0.2)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 9.0.2)
- Microsoft.Extensions.DependencyModel (>= 9.0.2)
- Microsoft.Extensions.Hosting.Abstractions (>= 9.0.2)
-
net9.0
- Microsoft.Extensions.Configuration.Abstractions (>= 9.0.2)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 9.0.2)
- Microsoft.Extensions.DependencyModel (>= 9.0.2)
- Microsoft.Extensions.Hosting.Abstractions (>= 9.0.2)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.