From 9f4d9cffb02e64be800f8d143fac7d7d1826a40b Mon Sep 17 00:00:00 2001 From: Hisham Bin Ateya Date: Fri, 9 Dec 2022 23:29:28 +0300 Subject: [PATCH] Introduce AddUsers() extension method --- .../OrchardCore.Users/Startup.cs | 21 +-------- .../UsersServiceCollectionExtensions.cs | 44 +++++++++++++++++++ 2 files changed, 45 insertions(+), 20 deletions(-) create mode 100644 src/OrchardCore/OrchardCore.Users.Core/Extensions/UsersServiceCollectionExtensions.cs diff --git a/src/OrchardCore.Modules/OrchardCore.Users/Startup.cs b/src/OrchardCore.Modules/OrchardCore.Users/Startup.cs index df89334ff88..cbacd63d126 100644 --- a/src/OrchardCore.Modules/OrchardCore.Users/Startup.cs +++ b/src/OrchardCore.Modules/OrchardCore.Users/Startup.cs @@ -36,13 +36,11 @@ using OrchardCore.Users.Controllers; using OrchardCore.Users.Drivers; using OrchardCore.Users.Handlers; -using OrchardCore.Users.Indexes; using OrchardCore.Users.Liquid; using OrchardCore.Users.Models; using OrchardCore.Users.Services; using OrchardCore.Users.ViewModels; using YesSql.Filters.Query; -using YesSql.Indexes; namespace OrchardCore.Users { @@ -166,15 +164,7 @@ public override void ConfigureServices(IServiceCollection services) // This is required for security modules like the OpenID module (that uses SignOutAsync()) to work correctly. services.AddAuthentication(options => options.DefaultSignOutScheme = IdentityConstants.ApplicationScheme); - services.TryAddScoped(); - services.TryAddScoped>(sp => sp.GetRequiredService()); - services.TryAddScoped>(sp => sp.GetRequiredService()); - services.TryAddScoped>(sp => sp.GetRequiredService()); - services.TryAddScoped>(sp => sp.GetRequiredService()); - services.TryAddScoped>(sp => sp.GetRequiredService()); - services.TryAddScoped>(sp => sp.GetRequiredService()); - services.TryAddScoped>(sp => sp.GetRequiredService()); - services.TryAddScoped>(sp => sp.GetRequiredService()); + services.AddUsers(); services.ConfigureApplicationCookie(options => { @@ -191,14 +181,8 @@ public override void ConfigureServices(IServiceCollection services) options.AccessDeniedPath = "/Error/403"; }); - services.AddSingleton(); - services.AddSingleton(); - services.AddSingleton(); - services.AddSingleton(); services.AddDataMigration(); - services.AddScoped(); - services.AddScoped, DefaultUserClaimsPrincipalProviderFactory>(); services.AddScoped(); services.AddSingleton(); @@ -243,9 +227,6 @@ public override void ConfigureServices(IServiceCollection services) }); services.AddTransient(); - - services.AddScoped(); - services.AddTransient, UserOptionsConfiguration>(); } } diff --git a/src/OrchardCore/OrchardCore.Users.Core/Extensions/UsersServiceCollectionExtensions.cs b/src/OrchardCore/OrchardCore.Users.Core/Extensions/UsersServiceCollectionExtensions.cs new file mode 100644 index 00000000000..d65d27406eb --- /dev/null +++ b/src/OrchardCore/OrchardCore.Users.Core/Extensions/UsersServiceCollectionExtensions.cs @@ -0,0 +1,44 @@ +using Microsoft.AspNetCore.Identity; +using Microsoft.Extensions.DependencyInjection.Extensions; +using OrchardCore.Users.Services; +using OrchardCore.Users; +using OrchardCore.Users.Indexes; +using YesSql.Indexes; +using OrchardCore.Users.Handlers; + +namespace Microsoft.Extensions.DependencyInjection +{ + /// + /// Provides extension methods for . + /// + public static class UsersServiceCollectionExtensions + { + /// + /// Registers the users services. + /// + /// The . + public static IServiceCollection AddUsers(this IServiceCollection services) + { + services.TryAddScoped(); + services.TryAddScoped>(sp => sp.GetRequiredService()); + services.TryAddScoped>(sp => sp.GetRequiredService()); + services.TryAddScoped>(sp => sp.GetRequiredService()); + services.TryAddScoped>(sp => sp.GetRequiredService()); + services.TryAddScoped>(sp => sp.GetRequiredService()); + services.TryAddScoped>(sp => sp.GetRequiredService()); + services.TryAddScoped>(sp => sp.GetRequiredService()); + services.TryAddScoped>(sp => sp.GetRequiredService()); + + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); + + services.AddScoped(); + services.AddScoped, DefaultUserClaimsPrincipalProviderFactory>(); + services.AddScoped(); + + return services; + } + } +}