Files
Store.Management/Source/Store.Management/Notifications/NotificationExample.cs
T
2026-10-27 14:35:03 +08:00

55 lines
1.8 KiB
C#

using MAX.Notification;
using Prism.Ioc;
namespace Store.Management.Notifications;
/// <summary>保存示例处理结果,由发布方等待完成后读取。</summary>
public sealed class NotificationExampleState
{
private readonly object gate = new();
private readonly List<string> entries = [];
public void Record(string message)
{
lock (gate)
{
entries.Add(message);
if (entries.Count > 10) entries.RemoveAt(0);
}
}
public string GetHistory()
{
lock (gate) return string.Join(Environment.NewLine, entries);
}
}
/// <summary>模拟模块一:商品保存后刷新缓存。</summary>
public sealed class ProductCacheHandler(NotificationExampleState state)
: INotificationHandler<ProductSavedNotification>
{
public Task Handle(ProductSavedNotification notification, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
state.Record($"缓存模块:已刷新商品「{notification.ProductName}」。");
return Task.CompletedTask;
}
}
/// <summary>模拟模块二:异步记录操作日志。</summary>
public sealed class ProductAuditHandler(NotificationExampleState state)
: INotificationHandler<ProductSavedNotification>
{
public async Task Handle(ProductSavedNotification notification, CancellationToken cancellationToken)
{
await Task.Delay(200, cancellationToken).ConfigureAwait(false);
state.Record($"审计模块:已记录商品「{notification.ProductName}」的保存操作。");
}
}
/// <summary>将标准服务解析转交给当前 Prism 容器。</summary>
internal sealed class PrismNotificationServiceProvider(IContainerProvider container) : IServiceProvider
{
public object? GetService(Type serviceType) => container.Resolve(serviceType);
}