hn-dotnet/Infrastructure/Behaviors/UnitOfWorkBehavior.cs
Julien Leicher 7a9aefbefc tiny-refactors (#27)
add UnitWorkBehavior and some files moving
add Docker stuff to prepare heroku deployment
rename (Up/Down)vote
add sample for msbuild tasks
2020-12-17 12:01:11 +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;
}
}
}