Skip to content

Commit

Permalink
Merge pull request #293 from Lombiq/issue/OFFI-132
Browse files Browse the repository at this point in the history
OFFI-132: Add ShellHostExtensions
  • Loading branch information
wAsnk authored Nov 7, 2024
2 parents 95704a3 + 1a4dd57 commit 2b4122e
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,18 @@ public static class ServiceProviderExtensions
/// <summary>
/// Executes <paramref name="asyncAction"/> in the specified shell's scope.
/// </summary>
public static async Task WithShellScopeAsync(
public static Task WithShellScopeAsync(
this IServiceProvider serviceProvider,
Func<ShellScope, Task> asyncAction,
string scopeName = "Default")
{
var shellHost = serviceProvider.GetRequiredService<IShellHost>();
var shellScope = await shellHost.GetScopeAsync(scopeName);
await shellScope.UsingAsync(asyncAction);
}
string scopeName = ShellSettings.DefaultShellName) =>
serviceProvider.GetRequiredService<IShellHost>().WithShellScopeAsync(asyncAction, scopeName);

/// <summary>
/// Executes <paramref name="asyncFunc"/> in the specified shell's scope and returns the resulting object.
/// </summary>
public static async Task<T> GetWithShellScopeAsync<T>(
public static Task<T> GetWithShellScopeAsync<T>(
this IServiceProvider serviceProvider,
Func<ShellScope, Task<T>> asyncFunc,
string scopeName = "Default")
{
T result = default;

await serviceProvider.WithShellScopeAsync(async scope => result = await asyncFunc(scope), scopeName);

return result;
}
string scopeName = ShellSettings.DefaultShellName) =>
serviceProvider.GetRequiredService<IShellHost>().GetWithShellScopeAsync(asyncFunc, scopeName);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using OrchardCore.Environment.Shell.Scope;
using System;
using System.Threading.Tasks;

namespace OrchardCore.Environment.Shell;

public static class ShellHostExtensions
{
/// <summary>
/// Executes <paramref name="asyncAction"/> in the specified shell's scope.
/// </summary>
public static async Task WithShellScopeAsync(
this IShellHost shellHost,
Func<ShellScope, Task> asyncAction,
string scopeName = ShellSettings.DefaultShellName)
{
await using var shellScope = await shellHost.GetScopeAsync(scopeName);
await shellScope.UsingAsync(asyncAction);
}

/// <summary>
/// Executes <paramref name="asyncFunc"/> in the specified shell's scope and returns the resulting object.
/// </summary>
public static async Task<T> GetWithShellScopeAsync<T>(
this IShellHost shellHost,
Func<ShellScope, Task<T>> asyncFunc,
string scopeName = ShellSettings.DefaultShellName)
{
T result = default;

await shellHost.WithShellScopeAsync(async scope => result = await asyncFunc(scope), scopeName);

return result;
}
}
3 changes: 2 additions & 1 deletion Lombiq.HelpfulLibraries.OrchardCore/Mvc/TypedRoute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Microsoft.Extensions.Options;
using OrchardCore.Admin;
using OrchardCore.Environment.Extensions;
using OrchardCore.Environment.Shell;
using OrchardCore.Modules.Manifest;
using OrchardCore.Mvc.Core.Utilities;
using System;
Expand Down Expand Up @@ -111,7 +112,7 @@ public override string ToString()
/// route, and other arguments.
/// </summary>
public string ToString(string tenantName) =>
string.IsNullOrWhiteSpace(tenantName) || tenantName.EqualsOrdinalIgnoreCase("Default")
string.IsNullOrWhiteSpace(tenantName) || tenantName.EqualsOrdinalIgnoreCase(ShellSettings.DefaultShellName)
? ToString()
: $"/{tenantName}{this}";

Expand Down

0 comments on commit 2b4122e

Please sign in to comment.