hn-dotnet/Infrastructure/Behaviors/UnitOfWorkBehavior.cs
2020-12-17 12:00:26 +01:00

25 lines
604 B
C#

using System.Threading;
using System.Threading.Tasks;
using MediatR;
namespace HN.Infrastructure.Behaviors
{
public sealed class UnitOfWorkBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
{
private readonly HNDbContext _context;
public UnitOfWorkBehavior(HNDbContext context)
{
_context = context;
}
public async Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate<TResponse> next)
{
var response = await next();
await _context.SaveChangesAsync();
return response;
}
}
}