Skip to content

Commit

Permalink
MarimerLLC#787 Enhance ApplicationContext to access IServiceProvider …
Browse files Browse the repository at this point in the history
…instance
  • Loading branch information
rockfordlhotka committed Jun 5, 2019
1 parent 0feb4f7 commit 1065c0c
Show file tree
Hide file tree
Showing 6 changed files with 211 additions and 5 deletions.
70 changes: 67 additions & 3 deletions Source/Csla.Shared/ApplicationContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -814,9 +814,33 @@ internal static void SetLogicalExecutionLocation(LogicalExecutionLocations locat
{
LocalContext["__logicalExecutionLocation"] = location;
}
#endregion
#endregion

#region ServiceProvider

/// <summary>
/// Gets or sets the default service provider
/// for this application.
/// </summary>
public static IServiceProvider DefaultServiceProvider
{
get => _contextManager.GetDefaultServiceProvider();
set => _contextManager.SetDefaultServiceProvider(value);
}

/// <summary>
/// Gets or sets the service provider
/// for this application context.
/// </summary>
public static IServiceProvider ScopedServiceProvider
{
get => _contextManager.GetScopedServiceProvider();
set => _contextManager.SetScopedServiceProvider(value);
}

#endregion

#region Default context manager
#region Default context manager

/// <summary>
/// Default context manager for the user property
Expand Down Expand Up @@ -951,8 +975,48 @@ public void SetGlobalContext(ContextDictionary globalContext)
LocalDataStoreSlot slot = Thread.GetNamedDataSlot(_globalContextName);
Thread.SetData(slot, globalContext);
}

private static IServiceProvider _provider;

/// <summary>
/// Gets the default IServiceProvider
/// </summary>
public IServiceProvider GetDefaultServiceProvider()
{
return _provider;
}

/// <summary>
/// Sets the default IServiceProvider
/// </summary>
/// <param name="serviceProvider">IServiceProvider instance</param>
public void SetDefaultServiceProvider(IServiceProvider serviceProvider)
{
_provider = serviceProvider;
}

/// <summary>
/// Gets the scoped IServiceProvider
/// </summary>
public IServiceProvider GetScopedServiceProvider()
{
IServiceProvider result;
result = (IServiceProvider)GetLocalContext().GetValueOrNull("__ssp");
if (result == null)
result = GetDefaultServiceProvider();
return result;
}

/// <summary>
/// Sets the scoped IServiceProvider
/// </summary>
/// <param name="serviceProvider">IServiceProvider instance</param>
public void SetScopedServiceProvider(IServiceProvider serviceProvider)
{
GetLocalContext()["__ssp"] = serviceProvider;
}
}

#endregion
#endregion
}
}
12 changes: 10 additions & 2 deletions Source/Csla.Shared/Configuration/CslaConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ namespace Csla.Configuration
/// </summary>
public class CslaConfiguration : ICslaConfiguration
{
#if !NETSTANDARD2_0
/// <summary>
/// Sets the web context manager.
/// </summary>
Expand All @@ -32,7 +31,6 @@ public ICslaConfiguration WebContextManager(IContextManager contextManager)
ApplicationContext.WebContextManager = contextManager;
return this;
}
#endif

/// <summary>
/// Sets the context manager.
Expand Down Expand Up @@ -115,6 +113,16 @@ public ICslaConfiguration PropertyInfoFactory(string typeName)
return this;
}

/// <summary>
/// Sets the default IServiceProvider for the application.
/// </summary>
/// <param name="serviceProvider">IServiceProvider instance</param>
public ICslaConfiguration DefaultServiceProvider(IServiceProvider serviceProvider)
{
ApplicationContext.DefaultServiceProvider = serviceProvider;
return this;
}

/// <summary>
/// Resets any ApplicationContext settings so they
/// re-read their configuration from AppSettings
Expand Down
18 changes: 18 additions & 0 deletions Source/Csla.Shared/Core/IContextManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,23 @@ public interface IContextManager
/// </summary>
/// <param name="globalContext">Global context.</param>
void SetGlobalContext(ContextDictionary globalContext);
/// <summary>
/// Gets the default IServiceProvider
/// </summary>
IServiceProvider GetDefaultServiceProvider();
/// <summary>
/// Sets the default IServiceProvider
/// </summary>
/// <param name="serviceProvider">IServiceProvider instance</param>
void SetDefaultServiceProvider(IServiceProvider serviceProvider);
/// <summary>
/// Gets the scoped IServiceProvider
/// </summary>
IServiceProvider GetScopedServiceProvider();
/// <summary>
/// Sets the scoped IServiceProvider
/// </summary>
/// <param name="serviceProvider">IServiceProvider instance</param>
void SetScopedServiceProvider(IServiceProvider serviceProvider);
}
}
34 changes: 34 additions & 0 deletions Source/Csla.Web.Mvc.Shared/ApplicationContextManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,40 @@ public void SetGlobalContext(ContextDictionary globalContext)
{
HttpContext.Items[_globalContextName] = globalContext;
}

/// <summary>
/// Gets the default IServiceProvider
/// </summary>
public IServiceProvider GetDefaultServiceProvider()
{
return HttpContext?.RequestServices;
}

/// <summary>
/// Sets the default IServiceProvider
/// </summary>
/// <param name="serviceProvider">IServiceProvider instance</param>
public void SetDefaultServiceProvider(IServiceProvider serviceProvider)
{
throw new InvalidOperationException();
}

/// <summary>
/// Gets the scoped IServiceProvider
/// </summary>
public IServiceProvider GetScopedServiceProvider()
{
return HttpContext?.RequestServices;
}

/// <summary>
/// Sets the scoped IServiceProvider
/// </summary>
/// <param name="serviceProvider">IServiceProvider instance</param>
public void SetScopedServiceProvider(IServiceProvider serviceProvider)
{
throw new InvalidOperationException();
}
}
}
#endif
42 changes: 42 additions & 0 deletions Source/Csla.Web.Shared/ApplicationContextManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,5 +107,47 @@ public void SetGlobalContext(ContextDictionary globalContext)
{
HttpContext.Current.Items[_globalContextName] = globalContext;
}

/// <summary>
/// Gets the default IServiceProvider
/// </summary>
public IServiceProvider GetDefaultServiceProvider()
{
IServiceProvider result;
result = (IServiceProvider)GetLocalContext().GetValueOrNull("__dsp");
if (result == null)
result = GetDefaultServiceProvider();
return result;
}

/// <summary>
/// Sets the default IServiceProvider
/// </summary>
/// <param name="serviceProvider">IServiceProvider instance</param>
public void SetDefaultServiceProvider(IServiceProvider serviceProvider)
{
GetLocalContext()["__dsp"] = serviceProvider;
}

/// <summary>
/// Gets the scoped IServiceProvider
/// </summary>
public IServiceProvider GetScopedServiceProvider()
{
IServiceProvider result;
result = (IServiceProvider)GetLocalContext().GetValueOrNull("__ssp");
if (result == null)
result = GetDefaultServiceProvider();
return result;
}

/// <summary>
/// Sets the scoped IServiceProvider
/// </summary>
/// <param name="serviceProvider">IServiceProvider instance</param>
public void SetScopedServiceProvider(IServiceProvider serviceProvider)
{
GetLocalContext()["__ssp"] = serviceProvider;
}
}
}
40 changes: 40 additions & 0 deletions Source/Csla.test/AppContext/TestContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,45 @@ public void SetGlobalContext(ContextDictionary globalContext)
{
_myContext[_globalContextName] = globalContext;
}

private static IServiceProvider _provider;

/// <summary>
/// Gets the default IServiceProvider
/// </summary>
public IServiceProvider GetDefaultServiceProvider()
{
return _provider;
}

/// <summary>
/// Sets the default IServiceProvider
/// </summary>
/// <param name="serviceProvider">IServiceProvider instance</param>
public void SetDefaultServiceProvider(IServiceProvider serviceProvider)
{
_provider = serviceProvider;
}

/// <summary>
/// Gets the scoped IServiceProvider
/// </summary>
public IServiceProvider GetScopedServiceProvider()
{
IServiceProvider result;
result = (IServiceProvider)GetLocalContext().GetValueOrNull("__ssp");
if (result == null)
result = GetDefaultServiceProvider();
return result;
}

/// <summary>
/// Sets the scoped IServiceProvider
/// </summary>
/// <param name="serviceProvider">IServiceProvider instance</param>
public void SetScopedServiceProvider(IServiceProvider serviceProvider)
{
GetLocalContext()["__ssp"] = serviceProvider;
}
}
}

0 comments on commit 1065c0c

Please sign in to comment.