Sparkdo.Mediation.Core 1.0.1

There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package Sparkdo.Mediation.Core --version 1.0.1
                    
NuGet\Install-Package Sparkdo.Mediation.Core -Version 1.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="Sparkdo.Mediation.Core" Version="1.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Sparkdo.Mediation.Core" Version="1.0.1" />
                    
Directory.Packages.props
<PackageReference Include="Sparkdo.Mediation.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.Mediation.Core --version 1.0.1
                    
#r "nuget: Sparkdo.Mediation.Core, 1.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.
#:package Sparkdo.Mediation.Core@1.0.1
                    
#: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.Mediation.Core&version=1.0.1
                    
Install as a Cake Addin
#tool nuget:?package=Sparkdo.Mediation.Core&version=1.0.1
                    
Install as a Cake Tool

Sparkdo.Mediation.Core

Sparkdo.Mediation.Core 是 Sparkdo 框架中的中介者模式核心实现库。它提供了一种解耦组件间通信的机制,通过中介者模式减少组件之间的直接依赖,提高代码的可维护性和可测试性。

功能特性

1. 中介者模式实现

  • 实现了完整的中介者模式,包括请求/响应和通知/订阅机制
  • 通过 IMediator 接口统一管理请求处理和事件发布
  • 支持同步和异步操作

2. 请求/响应模式

  • 支持 IRequest<TResponse>IRequestHandler<TRequest, TResponse> 接口
  • 提供类型安全的请求处理机制
  • 支持流式响应处理 (IStreamRequest<TResponse>)

3. 通知/订阅模式

  • 支持 INotificationINotificationHandler<TNotification> 接口
  • 实现了发布/订阅机制,支持多个处理器处理同一通知
  • 提供可配置的通知发布策略

4. 中间件支持

  • 支持请求处理管道中的中间件
  • 可以在请求处理前后添加自定义逻辑
  • 支持条件中间件和排序

5. 可扩展架构

  • 通过 IMediatorConfigurationBuilder 提供灵活的配置机制
  • 支持自定义请求处理器和通知处理器
  • 支持插件化扩展

安装

<PackageReference Include="Sparkdo.Mediation.Core" Version="x.x.x" />

使用方法

1. 注册服务

Startup.cs 或程序初始化代码中注册中介者服务:

using Microsoft.Extensions.DependencyInjection;
using Sparkdo.Mediation;

public void ConfigureServices(IServiceCollection services)
{
    // 添加中介者服务
    services.AddMediation();
}

2. 定义请求和处理器

// 定义请求
public class GetUserQuery : IRequest<User>
{
    public int UserId { get; set; }
}

// 定义响应
public class User
{
    public int Id { get; set; }
    public string Name { get; set; }
}

// 实现请求处理器
public class GetUserQueryHandler : IRequestHandler<GetUserQuery, User>
{
    public Task<User> Handle(GetUserQuery request, CancellationToken cancellationToken)
    {
        // 处理逻辑
        return Task.FromResult(new User 
        { 
            Id = request.UserId, 
            Name = $"User {request.UserId}" 
        });
    }
}

3. 发送请求

public class UserService
{
    private readonly IMediator _mediator;

    public UserService(IMediator mediator)
    {
        _mediator = mediator;
    }

    public async Task<User> GetUserAsync(int userId)
    {
        var query = new GetUserQuery { UserId = userId };
        return await _mediator.SendAsync(query);
    }
}

4. 定义通知和处理器

// 定义通知
public class UserCreatedNotification : INotification
{
    public int UserId { get; set; }
    public string UserName { get; set; }
}

// 实现通知处理器
public class UserCreatedEmailHandler : INotificationHandler<UserCreatedNotification>
{
    public Task Handle(UserCreatedNotification notification, CancellationToken cancellationToken)
    {
        // 发送邮件逻辑
        Console.WriteLine($"Sending email to user {notification.UserName}");
        return Task.CompletedTask;
    }
}

public class UserCreatedLogHandler : INotificationHandler<UserCreatedNotification>
{
    public Task Handle(UserCreatedNotification notification, CancellationToken cancellationToken)
    {
        // 记录日志逻辑
        Console.WriteLine($"User {notification.UserName} created");
        return Task.CompletedTask;
    }
}

5. 发布通知

public class UserController
{
    private readonly IMediator _mediator;

    public UserController(IMediator mediator)
    {
        _mediator = mediator;
    }

    public async Task CreateUserAsync()
    {
        // 创建用户逻辑
        var user = new User { Id = 1, Name = "John" };
        
        // 发布通知
        var notification = new UserCreatedNotification 
        { 
            UserId = user.Id, 
            UserName = user.Name 
        };
        
        await _mediator.PublishAsync(notification);
    }
}

高级功能

1. 中间件配置

services.AddMediation(builder => {
    // 添加自定义中间件
    builder.Middlewares.Add(new CustomMiddlewareConfiguration());
    
    // 配置通知发布器
    builder.NotificationPublisherFactory = sp => new CustomNotificationPublisher();
});

2. 条件处理器

services.AddMediation(builder => {
    // 根据条件注册处理器
    builder.UseRequestHandler(
        when: requestType => requestType.Name.EndsWith("Query"),
        handlerType: typeof(QueryHandler<>));
});
Product 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.  net9.0 was computed.  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 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. 
.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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

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

Package Downloads
Sparkdo.Mediation.HttpClient

library for the Sparkdo framework

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.2-preview.1 149 12/4/2025
1.0.1 453 11/27/2025
1.0.0 435 11/25/2025
1.0.0-preview.5 111 10/24/2025
1.0.0-preview.4 107 10/3/2025
1.0.0-preview.2 142 10/2/2025
1.0.0-preview.1 146 10/2/2025