public interface IDbContext
{
DbSet<TEntity> Set<TEntity>() where TEntity : class;
Task<int> SaveChangesAsync(CancellationToken cancellationToken = default);
}
public interface IInboxRepository<T>
where T : IDbContext
{
Task AddAndSaveAsync(IIntegrationEvent integrationEvent, CancellationToken cancellationToken = default);
}
public sealed class GenericIntegrationEventHandler<TEvent, TDbContext>(
IInboxRepository<TDbContext> inboxRepository)
: IIntegrationEventHandler<TEvent>
where TEvent : class, IIntegrationEvent
where TDbContext : IDbContext
{
private readonly IInboxRepository<TDbContext> _inboxRepository = inboxRepository;
public async Task Handle(TEvent notification, CancellationToken cancellationToken)
{
await _inboxRepository.AddAndSaveAsync(notification, cancellationToken);
}
}
public sealed class InboxRepository<T>(T dbContext)
: IInboxRepository<T>
where T : IDbContext
{
private readonly T _dbContext = dbContext;
public async Task AddAndSaveAsync(IIntegrationEvent integrationEvent, CancellationToken cancellationToken = default)
{
// Do stuff here
}
}
public interface IDbContext
{
DbSet<TEntity> Set<TEntity>() where TEntity : class;
Task<int> SaveChangesAsync(CancellationToken cancellationToken = default);
}
public interface IInboxRepository<T>
where T : IDbContext
{
Task AddAndSaveAsync(IIntegrationEvent integrationEvent, CancellationToken cancellationToken = default);
}
public sealed class GenericIntegrationEventHandler<TEvent, TDbContext>(
IInboxRepository<TDbContext> inboxRepository)
: IIntegrationEventHandler<TEvent>
where TEvent : class, IIntegrationEvent
where TDbContext : IDbContext
{
private readonly IInboxRepository<TDbContext> _inboxRepository = inboxRepository;
public async Task Handle(TEvent notification, CancellationToken cancellationToken)
{
await _inboxRepository.AddAndSaveAsync(notification, cancellationToken);
}
}
public sealed class InboxRepository<T>(T dbContext)
: IInboxRepository<T>
where T : IDbContext
{
private readonly T _dbContext = dbContext;
public async Task AddAndSaveAsync(IIntegrationEvent integrationEvent, CancellationToken cancellationToken = default)
{
// Do stuff here
}
}