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.
ApplicationContext.cs
Go to the documentation of this file.
1//-----------------------------------------------------------------------
2// <copyright file="ApplicationContext.cs" company="Marimer LLC">
3// Copyright (c) Marimer LLC. All rights reserved.
4// Website: https://cslanet.com
5// </copyright>
6// <summary>Provides consistent context information between the client</summary>
7//-----------------------------------------------------------------------
8using System;
9using System.Security.Principal;
10using Csla.Core;
11using System.Security.Claims;
13using System.ComponentModel;
14
15namespace Csla
16{
21 public class ApplicationContext
22 {
27 public ApplicationContext(ApplicationContextAccessor applicationContextAccessor)
28 {
29 ApplicationContextAccessor = applicationContextAccessor;
31 }
32
34
41
46 public ClaimsPrincipal Principal
47 {
48 get { return (ClaimsPrincipal)ContextManager.GetUser(); }
49 set { ContextManager.SetUser(value); }
50 }
51
62 public IPrincipal User
63 {
64 get { return ContextManager.GetUser(); }
65 set { ContextManager.SetUser(value); }
66 }
67
82 {
83 get
84 {
86 if (ctx == null)
87 {
88 ctx = new ContextDictionary();
90 }
91 return ctx;
92 }
93 }
94
95 private readonly object _syncContext = new();
96
117 {
118 get
119 {
120 lock (_syncContext)
121 {
123 if (ctx == null)
124 {
125 ctx = new ContextDictionary();
127 }
128 return ctx;
129 }
130 }
131 }
132
133 internal void SetContext(ContextDictionary clientContext)
134 {
135 lock (_syncContext)
137 }
138
142 public void Clear()
143 {
144 SetContext(null);
146 }
147
148 #region Settings
149
160 public bool IsOffline { get; set; }
161
167 public static bool UseReflectionFallback { get; set; } = false;
168
173 public static string VersionRoutingTag { get; internal set; }
174
181 public static string AuthenticationType { get; internal set; } = "Csla";
182
188 public static bool AutoCloneOnUpdate { get; internal set; } = true;
189
196 public static bool DataPortalReturnObjectOnException { get; internal set; }
197
202 {
206 Client,
210 Server
211 }
212
218 public static Type SerializationFormatter { get; internal set; } = typeof(Serialization.Mobile.MobileFormatter);
219
224 public static PropertyChangedModes PropertyChangedMode { get; set; }
225
231 {
236 Windows,
241 Xaml
242 }
243
244 private ExecutionLocations _executionLocation =
245#if (ANDROID || IOS || NETFX_CORE) && !NETSTANDARD
246 ExecutionLocations.MobileClient;
247#else
248 ExecutionLocations.Client;
249#endif
250
256 {
257 get { return _executionLocation; }
258 }
259
260 internal void SetExecutionLocation(ExecutionLocations location)
261 {
262 _executionLocation = location;
263 }
264
268 public const string DefaultRuleSet = "default";
269
274 public string RuleSet
275 {
276 get
277 {
278 var ruleSet = (string)ClientContext.GetValueOrNull("__ruleSet");
279 return string.IsNullOrEmpty(ruleSet) ? ApplicationContext.DefaultRuleSet : ruleSet;
280 }
281 set
282 {
283 ClientContext["__ruleSet"] = value;
284 }
285 }
286
294
301 public static int DefaultTransactionTimeoutInSeconds { get; internal set; } = 30;
302
307 public static System.Transactions.TransactionScopeAsyncFlowOption DefaultTransactionAsyncFlowOption
308 { get; internal set; } = System.Transactions.TransactionScopeAsyncFlowOption.Suppress;
309
310 #endregion
311
312 #region Logical Execution Location
319 {
323 Client,
328 Server
329 }
330
336 {
337 get
338 {
339 object location = LocalContext.GetValueOrNull("__logicalExecutionLocation");
340 if (location != null)
341 return (LogicalExecutionLocations)location;
342 else
343 return LogicalExecutionLocations.Client;
344 }
345 }
346
351 internal void SetLogicalExecutionLocation(LogicalExecutionLocations location)
352 {
353 LocalContext["__logicalExecutionLocation"] = location;
354 }
355 #endregion
356
363
367 internal IServiceProvider CurrentServiceProvider => ApplicationContextAccessor.ServiceProvider;
368
376 [EditorBrowsable(EditorBrowsableState.Advanced)]
377 public T CreateInstanceDI<T>(params object[] parameters)
378 {
379 return (T)CreateInstanceDI(typeof(T), parameters);
380 }
381
387 [EditorBrowsable(EditorBrowsableState.Advanced)]
389 {
390 if (CurrentServiceProvider == null)
391 throw new NullReferenceException(nameof(CurrentServiceProvider));
392
393 var result = CurrentServiceProvider.GetRequiredService<T>();
394 return result;
395 }
396
402 [EditorBrowsable(EditorBrowsableState.Advanced)]
403 public object GetRequiredService(Type serviceType)
404 {
405 if (CurrentServiceProvider == null)
406 throw new NullReferenceException(nameof(CurrentServiceProvider));
407
408 return CurrentServiceProvider.GetRequiredService(serviceType);
409 }
410
418 [EditorBrowsable(EditorBrowsableState.Advanced)]
419 public object CreateInstanceDI(Type objectType, params object[] parameters)
420 {
421 object result;
422 if (CurrentServiceProvider != null)
423 result = ActivatorUtilities.CreateInstance(CurrentServiceProvider, objectType, parameters);
424 else
425 result = Activator.CreateInstance(objectType, parameters);
426 if (result is IUseApplicationContext tmp)
427 {
428 tmp.ApplicationContext = this;
429 }
430 return result;
431 }
432
440 internal object CreateGenericInstanceDI(Type type, params Type[] paramTypes)
441 {
442 var genericType = type.GetGenericTypeDefinition();
443 var gt = genericType.MakeGenericType(paramTypes);
444 return CreateInstanceDI(gt);
445 }
446
452 [EditorBrowsable(EditorBrowsableState.Advanced)]
453 public T CreateInstance<T>(params object[] parameters)
454 {
455 return (T)CreateInstance(typeof(T), parameters);
456 }
457
463 [EditorBrowsable(EditorBrowsableState.Advanced)]
464 public object CreateInstance(Type objectType, params object[] parameters)
465 {
466 object result;
467 result = Activator.CreateInstance(objectType, parameters);
468 if (result is IUseApplicationContext tmp)
469 {
470 tmp.ApplicationContext = this;
471 }
472 return result;
473 }
474
481 internal object CreateGenericInstance(Type type, params Type[] paramTypes)
482 {
483 var genericType = type.GetGenericTypeDefinition();
484 var gt = genericType.MakeGenericType(paramTypes);
485 return CreateInstance(gt);
486 }
487
488 }
489}
Provides consistent context information between the client and server DataPortal objects.
LogicalExecutionLocations LogicalExecutionLocation
Return Logical Execution Location - Client or Server This is applicable to Local mode as well
object GetRequiredService(Type serviceType)
Attempts to get service via DI using ServiceProviderServiceExtensions.GetRequiredService.
ExecutionLocations ExecutionLocation
Returns a value indicating whether the application code is currently executing on the client or serve...
ExecutionLocations
Enum representing the locations code can execute.
static bool UseReflectionFallback
Gets or sets a value indicating whether CSLA should fallback to using reflection instead of System....
static bool AutoCloneOnUpdate
Gets a value indicating whether objects should be automatically cloned by the data portal Update() me...
LogicalExecutionLocations
Enum representing the logical execution location The setting is set to server when server is execting...
static Type SerializationFormatter
Gets the serialization formatter type used by CSLA .NET for all explicit object serialization (such a...
bool IsOffline
Gets or sets a value indicating whether the app should be considered "offline".
IContextManager ContextManager
Gets the context manager responsible for storing user and context information for the application.
object CreateInstanceDI(Type objectType, params object[] parameters)
Creates an object using 'Activator.CreateInstance' using service provider (if one is available) to po...
static PropertyChangedModes PropertyChangedMode
Gets or sets a value specifying how CSLA .NET should raise PropertyChanged events.
void Clear()
Clears all context collections.
bool IsAStatefulContextManager
Gets a value indicating whether the current context manager is used in a stateful context (e....
static int DefaultTransactionTimeoutInSeconds
Gets or sets the default transaction timeout in seconds.
PropertyChangedModes
Enum representing the way in which CSLA .NET should raise PropertyChanged events.
string? RuleSet
Gets or sets the RuleSet name to use for static HasPermission calls.
static bool DataPortalReturnObjectOnException
Gets a value indicating whether the server-side business object should be returned to the client as p...
T CreateInstanceDI< T >(params object[] parameters)
Creates an object using 'Activator.CreateInstance' using service provider (if one is available) to po...
ApplicationContext(ApplicationContextAccessor applicationContextAccessor)
Creates a new instance of the type
static string AuthenticationType
Gets the authentication type being used by the CSLA .NET framework.
IPrincipal User
Get or set the current IPrincipal object representing the user's identity.
object CreateInstance(Type objectType, params object[] parameters)
Creates an object using Activator.
T CreateInstance< T >(params object[] parameters)
Creates an object using Activator.
ClaimsPrincipal Principal
Get or set the current ClaimsPrincipal object representing the user's identity.
ContextDictionary ClientContext
Returns the application-specific context data provided by the client.
const string DefaultRuleSet
The default RuleSet name
ContextDictionary LocalContext
Returns the application-specific context data that is local to the current AppDomain.
T GetRequiredService< T >()
Attempts to get service via DI using ServiceProviderServiceExtensions.GetRequiredService.
static string VersionRoutingTag
Gets a value representing the application version for use in server-side data portal routing.
static System.Transactions.TransactionScopeAsyncFlowOption DefaultTransactionAsyncFlowOption
Gets or sets the default transaction async flow option used to create new TransactionScope objects.
static TransactionIsolationLevel DefaultTransactionIsolationLevel
Gets or sets the default transaction isolation level.
Provides access to the correct current application context manager instance depending on runtime envi...
IContextManager GetContextManager()
Gets a reference to the correct current application context manager instance depending on runtime env...
Dictionary type that is serializable with the SerializationFormatterFactory.GetFormatter().
object GetValueOrNull(string key)
Get a value from the dictionary, or return null if the key is not found in the dictionary.
Defines the interface for an application context manager type.
ApplicationContext ApplicationContext
Gets or sets a reference to the current ApplicationContext.
IPrincipal GetUser()
Gets the current principal.
bool IsStatefulContext
Gets a value indicating whether this context manager is used in a stateful context (e....
void SetClientContext(ContextDictionary clientContext, ApplicationContext.ExecutionLocations executionLocation)
Sets the client context.
void SetUser(IPrincipal principal)
Sets the current principal.
void SetLocalContext(ContextDictionary localContext)
Sets the local context.
ContextDictionary GetClientContext(ApplicationContext.ExecutionLocations executionLocation)
Gets the client context.
ContextDictionary GetLocalContext()
Gets the local context.
Implement if a class requires access to the CSLA ApplicationContext type.
@ Serialization
The object is being serialized for a clone or data portal operation.
TransactionIsolationLevel
Specifies an isolation level for transactions controlled by TransactionalAttribute