CSLA.NET 6.0.0
CSLA .NET is a software development framework that helps you build a reusable, maintainable object-oriented business layer for your app.
TestContextManager.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.Collections.Specialized;
4using System.Linq;
5using System.Security.Claims;
6using System.Security.Principal;
7using System.Text;
8using System.Threading;
9using Csla.Core;
11
13{
15 {
16 [ThreadStatic]
17 private static HybridDictionary _myContext = new HybridDictionary();
18 private readonly AsyncLocal<IPrincipal> _principal = new();
19
20 private const string _localContextName = "Csla.ClientContext";
21 private const string _clientContextName = "Csla.ClientContext";
22 private const string _globalContextName = "Csla.GlobalContext";
23
24 public bool IsValid
25 {
26 get { return true; }
27 }
28
29 public bool IsStatefulContext => true;
30
32
33 public IPrincipal GetUser()
34 {
35 IPrincipal result = _principal.Value;
36 if (result == null)
37 {
38 result = new System.Security.Claims.ClaimsPrincipal();
39 SetUser(result);
40 }
41 return result;
42 }
43
44 public void SetUser(IPrincipal principal)
45 {
46 _principal.Value = principal;
47 }
48
50 {
51 if (_myContext[_localContextName] == null)
53 return (ContextDictionary)_myContext[_localContextName];
54 }
55
56 public void SetLocalContext(ContextDictionary localContext)
57 {
58 _myContext[_localContextName] = localContext;
59 }
60
62 {
63 if (_myContext[_clientContextName] == null)
64 SetClientContext(new ContextDictionary(), executionLocation);
65 return (ContextDictionary) _myContext[_clientContextName];
66 }
67
68 public void SetClientContext(ContextDictionary clientContext, ApplicationContext.ExecutionLocations executionLocation)
69 {
70 _myContext[_clientContextName] = clientContext;
71 }
72
74 {
75 if (_myContext[_globalContextName] == null)
77 return (ContextDictionary)_myContext[_globalContextName];
78 }
79
80 public void SetGlobalContext(ContextDictionary globalContext)
81 {
82 _myContext[_globalContextName] = globalContext;
83 }
84
85 private static IServiceProvider _provider;
86
90 public IServiceProvider GetDefaultServiceProvider()
91 {
92 return _provider;
93 }
94
99 public void SetDefaultServiceProvider(IServiceProvider serviceProvider)
100 {
101 _provider = serviceProvider;
102 }
103
108 public IServiceProvider GetServiceProvider()
109 {
110 return (IServiceProvider)ApplicationContext.LocalContext["__sps"];
111 }
112
117 public void SetServiceProvider(IServiceProvider scope)
118 {
119 ApplicationContext.LocalContext["__sps"] = scope;
120 }
121 }
122}
Provides consistent context information between the client and server DataPortal objects.
ExecutionLocations
Enum representing the locations code can execute.
ContextDictionary LocalContext
Returns the application-specific context data that is local to the current AppDomain.
Dictionary type that is serializable with the SerializationFormatterFactory.GetFormatter().
void SetClientContext(ContextDictionary clientContext, ApplicationContext.ExecutionLocations executionLocation)
Sets the client context.
IPrincipal GetUser()
Gets the current principal.
void SetUser(IPrincipal principal)
Sets the current principal.
void SetGlobalContext(ContextDictionary globalContext)
void SetDefaultServiceProvider(IServiceProvider serviceProvider)
Sets the default IServiceProvider
IServiceProvider GetDefaultServiceProvider()
Gets the default IServiceProvider
void SetLocalContext(ContextDictionary localContext)
Sets the local context.
void SetServiceProvider(IServiceProvider scope)
Sets the service provider for current scope
ContextDictionary GetClientContext(ApplicationContext.ExecutionLocations executionLocation)
Gets the client context.
IServiceProvider GetServiceProvider()
Gets the service provider for current scope
ContextDictionary GetLocalContext()
Gets the local context.
Defines the interface for an application context manager type.