Skip to content

Commit 80f2728

Browse files
KaranChadha10KaranChadha10
authored andcommitted
refactor(service-config): Restructure service registration logic
1 parent 57c413c commit 80f2728

File tree

2 files changed

+70
-38
lines changed

2 files changed

+70
-38
lines changed

src/SimplCommerce.WebHost/Extensions/ServiceCollectionExtensions.cs

Lines changed: 67 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Diagnostics;
4+
using System.IdentityModel.Tokens.Jwt;
45
using System.IO;
56
using System.Linq;
67
using System.Net;
@@ -11,21 +12,25 @@
1112
using Microsoft.AspNetCore.Authentication.Cookies;
1213
using Microsoft.AspNetCore.Authentication.JwtBearer;
1314
using Microsoft.AspNetCore.Authentication.OAuth;
14-
using Microsoft.AspNetCore.Identity;
1515
using Microsoft.AspNetCore.Http;
16+
using Microsoft.AspNetCore.Identity;
1617
using Microsoft.AspNetCore.Mvc.ApplicationParts;
17-
using Microsoft.Extensions.Localization;
18+
using Microsoft.AspNetCore.Razor.TagHelpers;
1819
using Microsoft.EntityFrameworkCore;
1920
using Microsoft.Extensions.Configuration;
2021
using Microsoft.Extensions.DependencyInjection;
22+
using Microsoft.Extensions.Localization;
23+
using Microsoft.OpenApi.Models;
2124
using SimplCommerce.Infrastructure;
25+
using SimplCommerce.Infrastructure.Data;
2226
using SimplCommerce.Infrastructure.Modules;
27+
using SimplCommerce.Infrastructure.Web;
2328
using SimplCommerce.Infrastructure.Web.ModelBinders;
2429
using SimplCommerce.Module.Core.Data;
2530
using SimplCommerce.Module.Core.Extensions;
2631
using SimplCommerce.Module.Core.Models;
32+
using SimplCommerce.Module.Localization.TagHelpers;
2733
using SimplCommerce.WebHost.IdentityServer;
28-
using System.IdentityModel.Tokens.Jwt;
2934

3035
namespace SimplCommerce.WebHost.Extensions
3136
{
@@ -226,6 +231,65 @@ public static IServiceCollection AddCustomizedDataStore(this IServiceCollection
226231
return services;
227232
}
228233

234+
/// <summary>
235+
/// Registers core application services with the dependency injection container.
236+
/// </summary>
237+
/// <param name="services">The <see cref="IServiceCollection"/> to add services to.</param>
238+
/// <param name="configuration">The application configuration manager containing settings and connection strings.</param>
239+
/// <returns>The same service collection so that multiple calls can be chained.</returns>
240+
public static IServiceCollection AddApplicationServices(this IServiceCollection services, ConfigurationManager configuration)
241+
{
242+
services.AddHttpClient();
243+
services.AddTransient(typeof(IRepository<>), typeof(Repository<>));
244+
services.AddTransient(typeof(IRepositoryWithTypedId<,>), typeof(RepositoryWithTypedId<,>));
245+
services.AddScoped<SlugRouteValueTransformer>();
246+
services.AddScoped<ITagHelperComponent, LanguageDirectionTagHelperComponent>();
247+
services.AddTransient<IRazorViewRenderer, RazorViewRenderer>();
248+
services.AddAntiforgery(options => options.HeaderName = "X-XSRF-Token");
249+
services.AddCloudscribePagination();
250+
services.AddMediatR(cfg => cfg.RegisterServicesFromAssembly(typeof(Program).Assembly));
251+
252+
return services;
253+
}
254+
255+
/// <summary>
256+
/// Discovers and configures all modules in the application by finding and initializing their module initializers.
257+
/// </summary>
258+
/// <param name="services">The <see cref="IServiceCollection"/> to add module services to.</param>
259+
/// <returns>The same service collection so that multiple calls can be chained.</returns>
260+
public static IServiceCollection ConfigureModules(this IServiceCollection services)
261+
{
262+
foreach (var module in GlobalConfiguration.Modules)
263+
{
264+
var moduleInitializerType = module.Assembly.GetTypes()
265+
.FirstOrDefault(t => typeof(IModuleInitializer).IsAssignableFrom(t));
266+
267+
if (moduleInitializerType != null && moduleInitializerType != typeof(IModuleInitializer))
268+
{
269+
var moduleInitializer = (IModuleInitializer)Activator.CreateInstance(moduleInitializerType);
270+
services.AddSingleton(typeof(IModuleInitializer), moduleInitializer);
271+
moduleInitializer.ConfigureServices(services);
272+
}
273+
}
274+
275+
return services;
276+
}
277+
278+
/// <summary>
279+
/// Adds and configures Swagger/OpenAPI documentation services to the service collection.
280+
/// </summary>
281+
/// <param name="services">The <see cref="IServiceCollection"/> to add Swagger services to.</param>
282+
/// <returns>The same service collection so that multiple calls can be chained.</returns>
283+
public static IServiceCollection AddSwagger(this IServiceCollection services)
284+
{
285+
services.AddSwaggerGen(c =>
286+
{
287+
c.SwaggerDoc("v1", new OpenApiInfo { Title = "SimplCommerce API", Version = "v1" });
288+
});
289+
290+
return services;
291+
}
292+
229293
private static void TryLoadModuleAssembly(string moduleFolderPath, ModuleInfo module)
230294
{
231295
const string binariesFolderName = "bin";

src/SimplCommerce.WebHost/Program.cs

Lines changed: 3 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,19 @@
11
using System;
2-
using System.Linq;
32
using System.Text.Encodings.Web;
43
using System.Text.Unicode;
54
using Microsoft.AspNetCore.Builder;
65
using Microsoft.AspNetCore.Hosting;
76
using Microsoft.AspNetCore.Mvc.Razor;
8-
using Microsoft.AspNetCore.Razor.TagHelpers;
97
using Microsoft.EntityFrameworkCore;
108
using Microsoft.Extensions.Configuration;
119
using Microsoft.Extensions.DependencyInjection;
1210
using Microsoft.Extensions.Hosting;
1311
using Microsoft.Extensions.WebEncoders;
14-
using Microsoft.OpenApi.Models;
1512
using SimplCommerce.Infrastructure;
16-
using SimplCommerce.Infrastructure.Data;
1713
using SimplCommerce.Infrastructure.Modules;
1814
using SimplCommerce.Infrastructure.Web;
19-
using SimplCommerce.Module.Core.Data;
2015
using SimplCommerce.Module.Core.Extensions;
2116
using SimplCommerce.Module.Localization.Extensions;
22-
using SimplCommerce.Module.Localization.TagHelpers;
2317
using SimplCommerce.WebHost.Extensions;
2418

2519
var builder = WebApplication.CreateBuilder(args);
@@ -42,43 +36,17 @@ void ConfigureService()
4236
builder.Services.AddModules();
4337
builder.Services.AddCustomizedDataStore(builder.Configuration);
4438
builder.Services.AddCustomizedIdentity(builder.Configuration);
45-
builder.Services.AddHttpClient();
46-
builder.Services.AddTransient(typeof(IRepository<>), typeof(Repository<>));
47-
builder.Services.AddTransient(typeof(IRepositoryWithTypedId<,>), typeof(RepositoryWithTypedId<,>));
48-
builder.Services.AddScoped<SlugRouteValueTransformer>();
49-
39+
builder.Services.AddApplicationServices(builder.Configuration);
5040
builder.Services.AddCustomizedLocalization();
51-
5241
builder.Services.AddCustomizedMvc(GlobalConfiguration.Modules);
5342
builder.Services.Configure<RazorViewEngineOptions>(
5443
options => { options.ViewLocationExpanders.Add(new ThemeableViewLocationExpander()); });
5544
builder.Services.Configure<WebEncoderOptions>(options =>
5645
{
5746
options.TextEncoderSettings = new TextEncoderSettings(UnicodeRanges.All);
5847
});
59-
builder.Services.AddScoped<ITagHelperComponent, LanguageDirectionTagHelperComponent>();
60-
builder.Services.AddTransient<IRazorViewRenderer, RazorViewRenderer>();
61-
builder.Services.AddAntiforgery(options => options.HeaderName = "X-XSRF-Token");
62-
builder.Services.AddCloudscribePagination();
63-
64-
foreach (var module in GlobalConfiguration.Modules)
65-
{
66-
var moduleInitializerType = module.Assembly.GetTypes()
67-
.FirstOrDefault(t => typeof(IModuleInitializer).IsAssignableFrom(t));
68-
if ((moduleInitializerType != null) && (moduleInitializerType != typeof(IModuleInitializer)))
69-
{
70-
var moduleInitializer = (IModuleInitializer)Activator.CreateInstance(moduleInitializerType);
71-
builder.Services.AddSingleton(typeof(IModuleInitializer), moduleInitializer);
72-
moduleInitializer.ConfigureServices(builder.Services);
73-
}
74-
}
75-
76-
builder.Services.AddMediatR(cfg => cfg.RegisterServicesFromAssembly(typeof(Program).Assembly));
77-
78-
builder.Services.AddSwaggerGen(c =>
79-
{
80-
c.SwaggerDoc("v1", new OpenApiInfo { Title = "SimplCommerce API", Version = "v1" });
81-
});
48+
builder.Services.ConfigureModules();
49+
builder.Services.AddSwagger();
8250
}
8351

8452
void Configure()

0 commit comments

Comments
 (0)