dotnet-cqrs/Svrnty.CQRS.Events.Kafka/ServiceCollectionExtensions.cs
Mathias Beaulieu-Duncan 7fc680cd93 Add Svrnty.CQRS.Events.Kafka package
Kafka domain event publisher implementing IDomainEventPublisher,
sibling to Svrnty.CQRS.Events.RabbitMQ. Uses Confluent.Kafka 2.6.1,
targets net10.0 with C# 14.

Features:
- Configurable bootstrap servers, client id, idempotence, acks, retries
- Security protocol + SASL config (Plaintext/SSL/SASL_SSL etc.)
- Topic mapper (default lowercase event-type-name, custom func override)
- IAsyncDisposable producer cleanup
- Two registration overloads via AddKafkaDomainEvents

Project added to solution. Builds with 0 warnings.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-20 19:20:49 -04:00

54 lines
1.8 KiB
C#

using Microsoft.Extensions.DependencyInjection;
using Svrnty.CQRS.Events.Abstractions;
namespace Svrnty.CQRS.Events.Kafka;
/// <summary>
/// Extension methods for registering Kafka domain event publishing.
/// </summary>
public static class ServiceCollectionExtensions
{
/// <summary>
/// Adds Kafka domain event publishing to the service collection.
/// </summary>
/// <param name="services">The service collection.</param>
/// <param name="configure">Optional configuration action for Kafka options.</param>
/// <returns>The service collection for chaining.</returns>
public static IServiceCollection AddKafkaDomainEvents(
this IServiceCollection services,
Action<KafkaEventOptions>? configure = null)
{
if (configure != null)
{
services.Configure(configure);
}
services.AddSingleton<IDomainEventPublisher, KafkaDomainEventPublisher>();
return services;
}
/// <summary>
/// Adds Kafka domain event publishing with custom topic mapping.
/// </summary>
/// <param name="services">The service collection.</param>
/// <param name="topicMapper">Custom function to map event type names to topic names.</param>
/// <param name="configure">Optional configuration action for other Kafka options.</param>
/// <returns>The service collection for chaining.</returns>
public static IServiceCollection AddKafkaDomainEvents(
this IServiceCollection services,
Func<string, string> topicMapper,
Action<KafkaEventOptions>? configure = null)
{
services.Configure<KafkaEventOptions>(options =>
{
options.TopicMapper = topicMapper;
configure?.Invoke(options);
});
services.AddSingleton<IDomainEventPublisher, KafkaDomainEventPublisher>();
return services;
}
}