diff --git a/src/Cortex.Mediator/DependencyInjection/ServiceCollectionExtensions.cs b/src/Cortex.Mediator/DependencyInjection/ServiceCollectionExtensions.cs index 70c888d..fcd40c2 100644 --- a/src/Cortex.Mediator/DependencyInjection/ServiceCollectionExtensions.cs +++ b/src/Cortex.Mediator/DependencyInjection/ServiceCollectionExtensions.cs @@ -1,5 +1,4 @@ using Cortex.Mediator.Commands; -using Cortex.Mediator.Infrastructure; using Cortex.Mediator.Notifications; using Cortex.Mediator.Processors; using Cortex.Mediator.Queries; @@ -28,8 +27,6 @@ public static IServiceCollection AddCortexMediator( // Validation has been removed for issue #118 //services.AddValidatorsFromAssemblies(handlerAssemblyMarkerTypes.Select(t => t.Assembly)); - services.AddUnitOfWork(); - RegisterHandlers(services, handlerAssemblyMarkerTypes, options); RegisterProcessors(services, handlerAssemblyMarkerTypes, options); RegisterPipelineBehaviors(services, options); @@ -161,11 +158,5 @@ private static void RegisterPipelineBehaviors(IServiceCollection services, Media services.AddTransient(typeof(IStreamQueryPipelineBehavior<,>), behaviorType); } } - - private static void AddUnitOfWork(this IServiceCollection services) - { - services.AddScoped(provider => - new UnitOfWork(provider.GetRequiredService())); - } } } diff --git a/src/Cortex.Mediator/Infrastructure/IUnitOfWork.cs b/src/Cortex.Mediator/Infrastructure/IUnitOfWork.cs deleted file mode 100644 index 60c6acd..0000000 --- a/src/Cortex.Mediator/Infrastructure/IUnitOfWork.cs +++ /dev/null @@ -1,32 +0,0 @@ -using System; -using System.Threading.Tasks; - -namespace Cortex.Mediator.Infrastructure -{ - /// - /// Represents a unit of work for transaction management. - /// - public interface IUnitOfWork - { - /// - /// Begins a new transaction. - /// - Task BeginTransactionAsync(); - } - - /// - /// Represents a transaction within a unit of work. - /// - public interface IUnitOfWorkTransaction : IAsyncDisposable - { - /// - /// Commits the transaction. - /// - Task CommitAsync(); - - /// - /// Rolls back the transaction. - /// - Task RollbackAsync(); - } -} diff --git a/src/Cortex.Mediator/Infrastructure/UnitOfWork.cs b/src/Cortex.Mediator/Infrastructure/UnitOfWork.cs deleted file mode 100644 index 301bf78..0000000 --- a/src/Cortex.Mediator/Infrastructure/UnitOfWork.cs +++ /dev/null @@ -1,61 +0,0 @@ -using System.Data; -using System.Threading.Tasks; - -namespace Cortex.Mediator.Infrastructure -{ - /// - /// Default implementation of IUnitOfWork using System.Data. - /// - public class UnitOfWork : IUnitOfWork - { - private readonly IDbConnection _connection; - - public UnitOfWork(IDbConnection connection) - { - _connection = connection; - } - - public async Task BeginTransactionAsync() - { - if (_connection.State != ConnectionState.Open) - { - _connection.Open(); - } - - var transaction = _connection.BeginTransaction(); - return new UnitOfWorkTransaction(transaction); - } - - private class UnitOfWorkTransaction : IUnitOfWorkTransaction - { - private readonly IDbTransaction _transaction; - private bool _disposed; - - public UnitOfWorkTransaction(IDbTransaction transaction) - { - _transaction = transaction; - } - - public Task CommitAsync() - { - _transaction.Commit(); - return Task.CompletedTask; - } - - public Task RollbackAsync() - { - _transaction.Rollback(); - return Task.CompletedTask; - } - - public async ValueTask DisposeAsync() - { - if (_disposed) return; - - _transaction.Dispose(); - _disposed = true; - await Task.CompletedTask; - } - } - } -}