-
Notifications
You must be signed in to change notification settings - Fork 29
/
RavenSaveChangesAsyncFilter.cs
39 lines (34 loc) · 1.17 KB
/
RavenSaveChangesAsyncFilter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Filters;
using Raven.Client.Documents.Session;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Sample
{
/// <summary>
/// Razor Pages filter that saves any changes after the action completes.
/// </summary>
public class RavenSaveChangesAsyncFilter : IAsyncPageFilter
{
private readonly IAsyncDocumentSession dbSession;
public RavenSaveChangesAsyncFilter(IAsyncDocumentSession dbSession)
{
this.dbSession = dbSession;
}
public async Task OnPageHandlerSelectionAsync(PageHandlerSelectedContext context)
{
await Task.CompletedTask;
}
public async Task OnPageHandlerExecutionAsync(PageHandlerExecutingContext context, PageHandlerExecutionDelegate next)
{
var result = await next.Invoke();
// If there was no exception, and the action wasn't cancelled, save changes.
if (result.Exception == null && !result.Canceled)
{
await this.dbSession.SaveChangesAsync();
}
}
}
}