Sparkdo.Core 1.0.4-preview.3

This is a prerelease version of Sparkdo.Core.
dotnet add package Sparkdo.Core --version 1.0.4-preview.3
                    
NuGet\Install-Package Sparkdo.Core -Version 1.0.4-preview.3
                    
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="Sparkdo.Core" Version="1.0.4-preview.3" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Sparkdo.Core" Version="1.0.4-preview.3" />
                    
Directory.Packages.props
<PackageReference Include="Sparkdo.Core" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Sparkdo.Core --version 1.0.4-preview.3
                    
#r "nuget: Sparkdo.Core, 1.0.4-preview.3"
                    
#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.
#:package Sparkdo.Core@1.0.4-preview.3
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Sparkdo.Core&version=1.0.4-preview.3&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=Sparkdo.Core&version=1.0.4-preview.3&prerelease
                    
Install as a Cake Tool

Sparkdo.Core

Sparkdo 核心库,提供框架基础功能和核心组件,包括依赖注入、模块化、异常处理等核心功能实现。

功能特性

  • 依赖注入扩展:提供增强的依赖注入功能,包括生命周期管理、缓存服务提供者等
  • 模块化系统:基于模块的应用程序架构,支持模块的生命周期管理
  • AOT 代理与拦截器:使用源码生成 Proxy、原生 DI 和 IInterceptor pipeline 实现可扩展 AOP
  • 异常处理:统一的异常处理机制,包括业务异常、用户友好异常等
  • 配置管理:扩展的配置系统,支持多种配置源
  • 实用工具类:各种常用的扩展方法和辅助类

核心组件

1. 应用程序基础 (ApplicationBase)

提供应用程序的基础实现,包含配置、初始化和关闭等生命周期管理。

2. 模块系统 (Modularity)

  • Module:模块抽象基类,定义模块生命周期方法
  • IModuleManager:模块管理器,负责模块的加载和生命周期管理
  • IModuleLoader:模块加载器,负责发现和加载模块
  • ModuleLifecycleActionRegistry:模块生命周期附加动作注册表,允许框架、类库和源码生成器在固有生命周期中追加配置

3. 依赖注入扩展

  • ICachedServiceProvider:缓存服务提供者,提高服务获取性能
  • SparkdoServiceProviderFactory:自定义服务提供者工厂
  • 生命周期属性:ScopedAttributeSingletonAttributeTransientAttribute

4. 异常处理

  • BusinessException:业务异常基类
  • UserFriendlyException:用户友好异常
  • SparkdoException:框架异常基类

安装

dotnet add package Sparkdo.Core

使用示例

创建自定义模块

public class MyModule : Module
{
    public override void Configure(IConfigureContext context)
    {
        // 配置服务和选项
        context.Services.Configure<MyOptions>(options =>
        {
            options.Enabled = true;
        });
    }
    
    public override void Initialize(IInitializeContext context)
    {
        // 初始化操作
    }
}

生命周期附加动作

当框架扩展、类库或源码生成器需要在模块生命周期中追加配置时,使用 ModuleLifecycleActionRegistry,避免修改 Module 基类或要求业务模块实现额外接口。

ModuleLifecycleActionRegistry.AddConfigure<MyModule>(
    "MyLibrary.Configure",
    context =>
    {
        context.Services.Configure<MyOptions>(options => options.Enabled = true);
        return ValueTask.CompletedTask;
    });

ModuleLifecycleActionRegistry.AddPostInitialize<MyModule>(
    "MyLibrary.Warmup",
    async context =>
    {
        var service = context.ServiceProvider.GetRequiredService<IMyWarmupService>();
        await service.WarmupAsync();
    });

支持的阶段包括:PreConfigureConfigurePostConfigurePreInitializeInitializePostInitializeShutdown。注册动作使用 Func<TContext, ValueTask>;同步逻辑需要返回 ValueTask.CompletedTask。同一模块、同一阶段、同一名称会替换旧动作,并按 ordername 稳定排序。

AOT 代理与拦截器

Sparkdo 的 AOP 使用源码生成 Proxy、原生 DI 和 IInterceptor pipeline:

  • 拦截器匹配在源码生成阶段完成,依赖注册阶段不再检测或追加拦截器
  • 接口代理直接注册为原生 DI 服务工厂
  • 类代理生成派生类型,并直接作为具体类服务的实现类型注册
  • 生成代理直接传入 IInvocationMethodMetadata,拦截器优先读取 metadata,避免运行时反射读取 Attribute
  • 支持 TaskTask<T>ValueTaskValueTask<T> 返回值
  • 类代理仅拦截 public virtual 异步方法;非 virtual 方法不会被源码生成类代理拦截
  • 无拦截器路径直接返回目标调用结果,降低代理开销

详细工作原理、拦截器规则和示例代码参见 动态代理文档

应用程序启动

var builder = WebApplication.CreateBuilder(args);

// 添加 Sparkdo 服务
var sparkdo = await builder.Services.AddSparkdoAsync();
var app = builder.Build();

// 初始化模块
await app.InitializeAsync(app.Services);

app.Run();
Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (41)

Showing the top 5 NuGet packages that depend on Sparkdo.Core:

Package Downloads
Sparkdo.Localization.Abstractions

Sparkdo 本地化抽象模块,提供本地化功能的抽象接口和基础实现

Sparkdo.Security

Sparkdo 安全模块,提供身份验证、授权、加密和安全日志等核心安全功能

Sparkdo.Validation.Abstractions

Sparkdo 验证抽象库,提供验证相关的基础接口和异常类型定义

Sparkdo.VirtualFileSystem.Abstractions

Sparkdo 虚拟文件系统抽象库,定义了虚拟文件系统的接口和核心抽象

Sparkdo.Uow.Abstractions

Sparkdo 框架的工作单元模式抽象实现。提供接口和基础类用于实现具有事务支持、事件发布和数据库 API 管理的工作单元模式。

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.4-preview.3 641 6/8/2026
1.0.4-preview.2 634 6/7/2026
1.0.4-preview.1 642 6/7/2026
1.0.3 1,541 6/6/2026
1.0.3-preview.1 706 5/17/2026
1.0.2 1,229 4/27/2026
1.0.2-preview.5 220 4/15/2026
1.0.2-preview.4 224 2/8/2026
1.0.2-preview.3 242 2/1/2026
1.0.2-preview.2 241 1/31/2026
1.0.2-preview.1 252 12/4/2025
1.0.1 4,914 11/27/2025
1.0.0 4,800 11/25/2025
1.0.0-preview.5 163 10/24/2025