Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix selection of custom ContextManager through CslaOptions #4049

Merged
merged 2 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading