Skip to content

Commit

Permalink
Fix selection of custom ContextManager through CslaOptions (#4049)
Browse files Browse the repository at this point in the history
* Fix selection of custom ContextManager through CslaOptions

* #4047 Change how IContextManager is set; ensure custom manager is used
  • Loading branch information
rockfordlhotka committed Jun 20, 2024
1 parent ea08ad3 commit 018f366
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 27 deletions.
18 changes: 11 additions & 7 deletions Source/Csla.test/AppContext/AppContextTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Csla.Core;
using Csla.TestHelpers;
using Microsoft.AspNetCore.Mvc.ModelBinding.Validation;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Csla.Test.AppContext
Expand All @@ -31,7 +32,16 @@ public void Initialize()
TestResults.Reinitialise();
}

#region Simple Test
[TestMethod]
public void UseCustomApplicationContext()
{
var services = new ServiceCollection();
services.AddCsla(o => o.UseContextManager<ApplicationContextManagerTls>());
var serviceProvider = services.BuildServiceProvider();

var applicationContext = serviceProvider.GetRequiredService<ApplicationContext>();
Assert.IsInstanceOfType(applicationContext.ContextManager, typeof(ApplicationContextManagerTls));
}

[TestMethod]
public void SimpleTest()
Expand All @@ -47,10 +57,6 @@ public void SimpleTest()
Assert.AreEqual("Fetched", TestResults.GetResult("Root"), "global context missing server value");
}

#endregion

#region ClientContext

/// <summary>
/// Test the Client Context
/// </summary>
Expand Down Expand Up @@ -163,8 +169,6 @@ public void ContextDictionaryRemoveThrowsCorrectException()
Assert.ThrowsException<System.NotSupportedException>(() => contextDectionary.Remove(key));
}

#endregion

#region FailCreateContext

/// <summary>
Expand Down
36 changes: 18 additions & 18 deletions Source/Csla/Configuration/ConfigurationExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#if NET462_OR_GREATER || NETSTANDARD2_0 || NET8_0_OR_GREATER
#if NET462_OR_GREATER || NETSTANDARD2_0 || NET8_0_OR_GREATER
//-----------------------------------------------------------------------
// <copyright file="ConfigurationExtensions.cs" company="Marimer LLC">
// Copyright (c) Marimer LLC. All rights reserved.
Expand Down Expand Up @@ -45,9 +45,7 @@ public static IServiceCollection AddCsla(this IServiceCollection services, Actio

// ApplicationContext defaults
services.AddScoped<ApplicationContext>();
RegisterContextManager(services);
if (cslaOptions.ContextManagerType != null)
services.AddScoped(typeof(Core.IContextManager), cslaOptions.ContextManagerType);
RegisterContextManager(services, cslaOptions.ContextManagerType);

// Runtime Info defaults
services.TryAddScoped(typeof(IRuntimeInfo), typeof(RuntimeInfo));
Expand All @@ -65,24 +63,26 @@ public static IServiceCollection AddCsla(this IServiceCollection services, Actio
return services;
}

private static void RegisterContextManager(IServiceCollection services)
private static void RegisterContextManager(IServiceCollection services, Type contextManagerType)
{
services.AddScoped<Core.ApplicationContextAccessor>();
services.TryAddScoped(typeof(Core.IContextManagerLocal), typeof(Core.ApplicationContextManagerAsyncLocal));

var contextManagerType = typeof(Core.IContextManager);

var managerInit = services.Any(i => i.ServiceType.Equals(contextManagerType));
if (managerInit) return;

if (LoadContextManager(services, "Csla.Blazor.WebAssembly.ApplicationContextManager, Csla.Blazor.WebAssembly")) return;
if (LoadContextManager(services, "Csla.Xaml.ApplicationContextManager, Csla.Xaml")) return;
if (LoadContextManager(services, "Csla.Web.Mvc.ApplicationContextManager, Csla.Web.Mvc")) return;
if (LoadContextManager(services, "Csla.Web.ApplicationContextManager, Csla.Web")) return;
if (LoadContextManager(services, "Csla.Windows.Forms.ApplicationContextManager, Csla.Windows.Forms")) return;
if (contextManagerType == null)
{
if (LoadContextManager(services, "Csla.Blazor.WebAssembly.ApplicationContextManager, Csla.Blazor.WebAssembly")) return;
if (LoadContextManager(services, "Csla.Xaml.ApplicationContextManager, Csla.Xaml")) return;
if (LoadContextManager(services, "Csla.Web.Mvc.ApplicationContextManager, Csla.Web.Mvc")) return;
if (LoadContextManager(services, "Csla.Web.ApplicationContextManager, Csla.Web")) return;
if (LoadContextManager(services, "Csla.Windows.Forms.ApplicationContextManager, Csla.Windows.Forms")) return;

// default to AsyncLocal context manager
services.AddScoped(contextManagerType, typeof(Core.ApplicationContextManager));
// default to AsyncLocal context manager
services.AddScoped(typeof(Core.IContextManager), typeof(Core.ApplicationContextManagerAsyncLocal));
}
else
{
services.AddScoped(typeof(Core.IContextManager), contextManagerType);
}
}

private static bool LoadContextManager(IServiceCollection services, string managerTypeName)
Expand All @@ -97,4 +97,4 @@ private static bool LoadContextManager(IServiceCollection services, string manag
}
}
}
#endif
#endif
14 changes: 12 additions & 2 deletions Source/Csla/Configuration/Fluent/CslaOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,20 @@ public CslaOptions(IServiceCollection services)
public IServiceCollection Services { get; }

/// <summary>
/// Gets or sets the type for the IContextManager to
/// Sets the type for the IContextManager to
/// be used by ApplicationContext.
/// </summary>
public Type ContextManagerType { get; set; }
public CslaOptions UseContextManager<T>() where T : IContextManager
{
ContextManagerType = typeof(T);
return this;
}

/// <summary>
/// Gets the type for the IContextManager
/// used by ApplicationContext.
/// </summary>
public Type ContextManagerType { get; private set; }

/// <summary>
/// Sets a value indicating whether CSLA
Expand Down

0 comments on commit 018f366

Please sign in to comment.