重构代码并抽取基础构建库
This commit is contained in:
1 parent
9e6b5be92c
commit
ccf1e7672d
55 files changed
+398
-104
No files matched your search
@@ -0,0 +1,24 @@
|
||||
// Copyright (c) HelloShop Corporation. All rights reserved.
|
||||
// See the license file in the project root for more information.
|
||||
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace HelloShop.EventBus.Abstractions
|
||||
{
|
||||
public record DistributedEvent
|
||||
{
|
||||
public DistributedEvent()
|
||||
{
|
||||
Id = Guid.NewGuid();
|
||||
CreationTime = TimeProvider.System.GetUtcNow();
|
||||
}
|
||||
|
||||
public Guid Id { get; set; }
|
||||
|
||||
public DateTimeOffset CreationTime { get; set; }
|
||||
|
||||
[JsonExtensionData]
|
||||
public Dictionary<string, JsonElement>? ExtensionData { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
// Copyright (c) HelloShop Corporation. All rights reserved.
|
||||
// See the license file in the project root for more information.
|
||||
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System.Reflection;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace HelloShop.EventBus.Abstractions
|
||||
{
|
||||
public static class EventBusBuilderExtensions
|
||||
{
|
||||
public static IEventBusBuilder ConfigureJsonOptions(this IEventBusBuilder eventBusBuilder, Action<JsonSerializerOptions> configure)
|
||||
{
|
||||
eventBusBuilder.Services.Configure<EventBusOptions>(o =>
|
||||
{
|
||||
configure(o.JsonSerializerOptions);
|
||||
});
|
||||
|
||||
return eventBusBuilder;
|
||||
}
|
||||
|
||||
public static IEventBusBuilder AddSubscription<TEvent, TEventHandler>(this IEventBusBuilder eventBusBuilder) where TEvent : DistributedEvent where TEventHandler : class, IDistributedEventHandler<TEvent>
|
||||
{
|
||||
eventBusBuilder.Services.AddKeyedTransient<IDistributedEventHandler, TEventHandler>(typeof(TEvent));
|
||||
|
||||
eventBusBuilder.Services.Configure<EventBusOptions>(o =>
|
||||
{
|
||||
o.EventTypes[typeof(TEvent).Name] = typeof(TEvent);
|
||||
});
|
||||
|
||||
return eventBusBuilder;
|
||||
}
|
||||
|
||||
public static IEventBusBuilder AddSubscription(this IEventBusBuilder eventBusBuilder, Type eventType, Type eventHandlerType)
|
||||
{
|
||||
eventBusBuilder.Services.AddKeyedTransient(typeof(IDistributedEventHandler), eventType, eventHandlerType);
|
||||
|
||||
eventBusBuilder.Services.Configure<EventBusOptions>(o =>
|
||||
{
|
||||
o.EventTypes[eventType.Name] = eventType;
|
||||
});
|
||||
|
||||
return eventBusBuilder;
|
||||
}
|
||||
|
||||
public static IEventBusBuilder AddSubscriptionFromAssembly(this IEventBusBuilder eventBusBuilder, Assembly? assembly = null)
|
||||
{
|
||||
assembly ??= Assembly.GetCallingAssembly();
|
||||
|
||||
var handlers = assembly.GetTypes().Where(t => t.IsAssignableTo(typeof(IDistributedEventHandler))).ToList();
|
||||
|
||||
handlers.ForEach(handler =>
|
||||
{
|
||||
var eventType = handler.GetInterfaces().Single(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IDistributedEventHandler<>)).GetGenericArguments().Single();
|
||||
eventBusBuilder.AddSubscription(eventType, handler);
|
||||
});
|
||||
|
||||
return eventBusBuilder;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
// Copyright (c) HelloShop Corporation. All rights reserved.
|
||||
// See the license file in the project root for more information.
|
||||
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization.Metadata;
|
||||
|
||||
namespace HelloShop.EventBus.Abstractions
|
||||
{
|
||||
public class EventBusOptions
|
||||
{
|
||||
public Dictionary<string, Type> EventTypes { get; } = [];
|
||||
|
||||
public JsonSerializerOptions JsonSerializerOptions { get; } = new(DefaultSerializerOptions);
|
||||
|
||||
internal static readonly JsonSerializerOptions DefaultSerializerOptions = new()
|
||||
{
|
||||
TypeInfoResolver = JsonSerializer.IsReflectionEnabledByDefault ? new DefaultJsonTypeInfoResolver() : JsonTypeInfoResolver.Combine()
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net9.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" />
|
||||
<PackageReference Include="Microsoft.Extensions.Options" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,17 @@
|
||||
// Copyright (c) HelloShop Corporation. All rights reserved.
|
||||
// See the license file in the project root for more information.
|
||||
|
||||
namespace HelloShop.EventBus.Abstractions
|
||||
{
|
||||
public interface IDistributedEventHandler
|
||||
{
|
||||
Task HandleAsync(DistributedEvent @event);
|
||||
}
|
||||
|
||||
public interface IDistributedEventHandler<in TDistributedEvent> : IDistributedEventHandler where TDistributedEvent : DistributedEvent
|
||||
{
|
||||
Task HandleAsync(TDistributedEvent @event);
|
||||
|
||||
Task IDistributedEventHandler.HandleAsync(DistributedEvent @event) => HandleAsync((TDistributedEvent)@event);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
// Copyright (c) HelloShop Corporation. All rights reserved.
|
||||
// See the license file in the project root for more information.
|
||||
|
||||
namespace HelloShop.EventBus.Abstractions
|
||||
{
|
||||
public interface IEventBus
|
||||
{
|
||||
Task PublishAsync(DistributedEvent @event, CancellationToken cancellationToken = default);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
// Copyright (c) HelloShop Corporation. All rights reserved.
|
||||
// See the license file in the project root for more information.
|
||||
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace HelloShop.EventBus.Abstractions
|
||||
{
|
||||
public interface IEventBusBuilder
|
||||
{
|
||||
public IServiceCollection Services { get; }
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user