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

OFFI-132: Add ShellHostExtensions #293

Merged
merged 4 commits into from
Nov 7, 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
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
Loading