Skip to content

Commit

Permalink
remove DomainEventUnitOfWorkBehavior,不支持在domainEvent中处理事务
Browse files Browse the repository at this point in the history
  • Loading branch information
witskeeper committed Dec 28, 2023
1 parent d1ff8c1 commit a706a8d
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 47 deletions.
2 changes: 1 addition & 1 deletion eng/versions.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<VersionPrefix>1.0.2</VersionPrefix>
<VersionPrefix>1.0.3</VersionPrefix>
<VersionSuffix></VersionSuffix>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -1,43 +1,44 @@
using MediatR;
using NetCorePal.Extensions.Domain;
using NetCorePal.Extensions.Primitives;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NetCorePal.Extensions.Repository.EntityFrameworkCore
{

internal class DomainEventUnitOfWorkBehavior<TDomanEvent, TResponse> : IPipelineBehavior<TDomanEvent, TResponse> where TDomanEvent : IDomainEvent
{
private readonly ITransactionUnitOfWork _unitOfWork;
public DomainEventUnitOfWorkBehavior(ITransactionUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
}


public async Task<TResponse> Handle(TDomanEvent request, RequestHandlerDelegate<TResponse> next, CancellationToken cancellationToken)
{
if (_unitOfWork.CurrentTransaction != null)
{
var response = await next();
await _unitOfWork.SaveEntitiesAsync(cancellationToken);
return response;
}


using (var transaction = _unitOfWork.BeginTransaction())
{
var response = await next();
await _unitOfWork.SaveEntitiesAsync(cancellationToken);
await _unitOfWork.CommitAsync(cancellationToken);
return response;
}
}
}


}
// using MediatR;

Check warning on line 1 in src/Repository.EntityFrameworkCore/Behaviors/DomainEventUnitOfWorkBehavior.cs

View workflow job for this annotation

GitHub Actions / build

Remove this commented out code. (https://rules.sonarsource.com/csharp/RSPEC-125)

Check warning on line 1 in src/Repository.EntityFrameworkCore/Behaviors/DomainEventUnitOfWorkBehavior.cs

View workflow job for this annotation

GitHub Actions / build

Remove this commented out code. (https://rules.sonarsource.com/csharp/RSPEC-125)

Check warning on line 1 in src/Repository.EntityFrameworkCore/Behaviors/DomainEventUnitOfWorkBehavior.cs

View workflow job for this annotation

GitHub Actions / build

Remove this commented out code. (https://rules.sonarsource.com/csharp/RSPEC-125)

Check warning on line 1 in src/Repository.EntityFrameworkCore/Behaviors/DomainEventUnitOfWorkBehavior.cs

View workflow job for this annotation

GitHub Actions / build

Remove this commented out code. (https://rules.sonarsource.com/csharp/RSPEC-125)
// using NetCorePal.Extensions.Domain;
// using NetCorePal.Extensions.Primitives;
// using System;
// using System.Collections.Generic;
// using System.Linq;
// using System.Text;
// using System.Threading.Tasks;
//
// namespace NetCorePal.Extensions.Repository.EntityFrameworkCore
// {
//
//
// internal class DomainEventUnitOfWorkBehavior<TDomanEvent, TResponse> : IPipelineBehavior<TDomanEvent, TResponse> where TDomanEvent : IDomainEvent
// {
// private readonly ITransactionUnitOfWork _unitOfWork;
// public DomainEventUnitOfWorkBehavior(ITransactionUnitOfWork unitOfWork)
// {
// _unitOfWork = unitOfWork;
// }
//
//
// public async Task<TResponse> Handle(TDomanEvent request, RequestHandlerDelegate<TResponse> next, CancellationToken cancellationToken)
// {
// if (_unitOfWork.CurrentTransaction != null)
// {
// var response = await next();
// await _unitOfWork.SaveEntitiesAsync(cancellationToken);
// return response;
// }
//
//
// using (var transaction = _unitOfWork.BeginTransaction())
// {
// var response = await next();
// await _unitOfWork.SaveEntitiesAsync(cancellationToken);
// await _unitOfWork.CommitAsync(cancellationToken);
// return response;
// }
// }
// }
//
//
// }
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ namespace NetCorePal.Extensions.DependencyInjection
{
public static class ServiceCollectionExtensions
{

/// <summary>
/// Scan assemblies add all IRepository to ServiceCollection
/// </summary>
Expand All @@ -27,7 +26,8 @@ public static IServiceCollection AddRepositories(this IServiceCollection service
{
services.TryAddScoped(repositoryType);
var repositoryInterfaceType = repositoryType.GetInterfaces()
.FirstOrDefault(x => !x.IsGenericType && Array.Exists(x.GetInterfaces(), i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IRepository<>)));
.FirstOrDefault(x => !x.IsGenericType && Array.Exists(x.GetInterfaces(),

Check warning on line 29 in src/Repository.EntityFrameworkCore/Extensions/ServiceCollectionExtensions.cs

View workflow job for this annotation

GitHub Actions / build

"Find" method should be used instead of the "FirstOrDefault" extension method. (https://rules.sonarsource.com/csharp/RSPEC-6602)

Check warning on line 29 in src/Repository.EntityFrameworkCore/Extensions/ServiceCollectionExtensions.cs

View workflow job for this annotation

GitHub Actions / build

"Find" method should be used instead of the "FirstOrDefault" extension method. (https://rules.sonarsource.com/csharp/RSPEC-6602)

Check warning on line 29 in src/Repository.EntityFrameworkCore/Extensions/ServiceCollectionExtensions.cs

View workflow job for this annotation

GitHub Actions / build

"Find" method should be used instead of the "FirstOrDefault" extension method. (https://rules.sonarsource.com/csharp/RSPEC-6602)

Check warning on line 29 in src/Repository.EntityFrameworkCore/Extensions/ServiceCollectionExtensions.cs

View workflow job for this annotation

GitHub Actions / build

"Find" method should be used instead of the "FirstOrDefault" extension method. (https://rules.sonarsource.com/csharp/RSPEC-6602)
i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IRepository<>)));

if (repositoryInterfaceType == null)
continue;
Expand All @@ -49,7 +49,6 @@ public static IServiceCollection AddUnitOfWork<TDbContext>(this IServiceCollecti
public static MediatRServiceConfiguration AddUnitOfWorkBehaviors(this MediatRServiceConfiguration cfg)
{
cfg.AddOpenBehavior(typeof(CommandUnitOfWorkBehavior<,>));
cfg.AddOpenBehavior(typeof(DomainEventUnitOfWorkBehavior<,>));
return cfg;
}
}
Expand Down

0 comments on commit a706a8d

Please sign in to comment.