-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathSessionAccessor.cs
More file actions
50 lines (42 loc) · 1.7 KB
/
SessionAccessor.cs
File metadata and controls
50 lines (42 loc) · 1.7 KB
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
40
41
42
43
44
45
46
47
48
49
50
// Copyright (C) 2021 Xtensive LLC.
// This code is distributed under MIT license terms.
// See the License.txt file in the project root for more information.
using System;
using Xtensive.Core;
using Microsoft.AspNetCore.Http;
namespace Xtensive.Orm.Web
{
/// <summary>
/// A wrapper that provides access <see cref="Xtensive.Orm.Session"></see>
/// and <see cref="Xtensive.Orm.TransactionScope"> that are in <see cref="HttpContext"/>.</see>.
/// </summary>
public sealed class SessionAccessor
{
internal static readonly object SessionIdentifier = new object();
internal static readonly object ScopeIdentifier = new object();
private HttpContext context;
/// <summary>
/// Provides <see cref="Orm.Session"/> from bound to <see cref="HttpContext"/>.
/// If no <see cref="Orm.Session"/> instance found, return <see langword="null"/>.
/// </summary>
public Session Session =>
context != null && context.Items.TryGetValue(SessionIdentifier, out var instance)
? (Session) instance
: null;
/// <summary>
/// Provides opened <see cref="Orm.TransactionScope"/> bound to <see cref="HttpContext"/>.
/// If no <see cref="Orm.Session"/> instance found, return <see langword="null"/>.
/// </summary>
public TransactionScope TransactionScope =>
context != null && context.Items.TryGetValue(ScopeIdentifier, out var instance)
? (TransactionScope) instance
: null;
internal bool ContextIsBound => context != null;
internal IDisposable BindHttpContext(HttpContext context)
{
this.context = context;
return new Disposable(NullTheContext);
}
private void NullTheContext(bool disposing) => context = null;
}
}