Skip to content

Commit 6639503

Browse files
committed
Add Service collection extension
1 parent e434d3b commit 6639503

File tree

2 files changed

+43
-6
lines changed

2 files changed

+43
-6
lines changed

src/Initium/Controllers/BaseController.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,4 @@ public abstract class BaseController : ControllerBase
3535
/// <returns>The parameter value if found; otherwise, null.</returns>
3636
protected string? GetQueryParameter(string key) =>
3737
HttpContext.Request.Query.TryGetValue(key, out var value) ? value.FirstOrDefault() : null;
38-
}
39-
40-
// public class BaseController<TService>(TService service) : ApiController where TService : BaseService
41-
// {
42-
// protected readonly TService Service = service;
43-
// }
38+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using System.Diagnostics.CodeAnalysis;
2+
using System.Text.Json.Serialization;
3+
using Initium.Infrastructure.Results;
4+
using Initium.Infrastructure.Transformers;
5+
using Microsoft.Extensions.Hosting;
6+
using Microsoft.AspNetCore.Mvc;
7+
using Microsoft.AspNetCore.Mvc.ApplicationModels;
8+
using Microsoft.AspNetCore.Routing;
9+
using Microsoft.Extensions.DependencyInjection;
10+
11+
namespace Initium.Extensions;
12+
13+
[SuppressMessage("ReSharper", "UnusedMethodReturnValue.Global")]
14+
public static class ServiceCollectionExtensions
15+
{
16+
/// <summary>
17+
/// Registers and configures Initium framework services and conventions.
18+
/// Applies API conventions, route slugification, lowercase URL enforcement,
19+
/// JSON serialization customization, exception behavior for background services,
20+
/// and a standardized invalid model state response.
21+
/// This method is designed to augment existing MVC configurations without enforcing specific pipeline usage.
22+
/// </summary>
23+
public static IServiceCollection AddInitium(this IServiceCollection services)
24+
{
25+
services.Configure<MvcOptions>(options =>
26+
options.Conventions.Add(new RouteTokenTransformerConvention(new SlugifyParameterTransformer())));
27+
28+
services.Configure<RouteOptions>(options => options.LowercaseUrls = true);
29+
services.Configure<HostOptions>(options => options.BackgroundServiceExceptionBehavior = BackgroundServiceExceptionBehavior.Ignore);
30+
31+
services.Configure<JsonOptions>(options =>
32+
{
33+
options.JsonSerializerOptions.WriteIndented = true;
34+
options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
35+
});
36+
37+
services.PostConfigure<ApiBehaviorOptions>(behaviorOptions =>
38+
behaviorOptions.InvalidModelStateResponseFactory = context => new InvalidModelStateResult(context));
39+
40+
return services;
41+
}
42+
}

0 commit comments

Comments
 (0)