Skip to content

Commit ad5a006

Browse files
committed
Change RequestOptions to record
1 parent 66ae458 commit ad5a006

File tree

5 files changed

+59
-6
lines changed

5 files changed

+59
-6
lines changed

src/KSFramework/KSMessaging/Extensions/RegisterMediatorServices.cs

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ public static IServiceCollection AddKSMediator(this IServiceCollection services,
4646

4747
// Register notification handlers
4848
.AddClasses(c => c.AssignableTo(typeof(INotificationHandler<>)))
49-
.AsImplementedInterfaces()
50-
.WithScopedLifetime()
49+
.AsImplementedInterfaces()
50+
.WithScopedLifetime()
5151

5252
// Register stream request handlers
5353
.AddClasses(c => c.AssignableTo(typeof(IStreamRequestHandler<,>)))
@@ -72,7 +72,41 @@ public static IServiceCollection AddKSMediator(this IServiceCollection services,
7272

7373
// If you have default behaviors (e.g., logging), register them here
7474
services.AddScoped(typeof(IStreamPipelineBehavior<,>), typeof(StreamLoggingBehavior<,>));
75+
76+
services.RegisterAllImplementationsOf<IRequestDecorator>(assemblies);
7577

7678
return services;
7779
}
80+
81+
private static void RegisterAllImplementationsOf<TInterface>(this IServiceCollection services,
82+
Assembly[] assemblies,
83+
ServiceLifetime lifetime = ServiceLifetime.Scoped)
84+
{
85+
var interfaceType = typeof(TInterface);
86+
87+
foreach (var assembly in assemblies)
88+
{
89+
var implementations = assembly.DefinedTypes
90+
.Where(type => type.IsClass && !type.IsAbstract && interfaceType.IsAssignableFrom(type))
91+
.ToList();
92+
93+
foreach (var implementation in implementations)
94+
{
95+
switch (lifetime)
96+
{
97+
case ServiceLifetime.Transient:
98+
services.AddTransient(interfaceType, implementation);
99+
break;
100+
case ServiceLifetime.Scoped:
101+
services.AddScoped(interfaceType, implementation);
102+
break;
103+
case ServiceLifetime.Singleton:
104+
services.AddSingleton(interfaceType, implementation);
105+
break;
106+
default:
107+
throw new ArgumentException("Invalid service lifetime", nameof(lifetime));
108+
}
109+
}
110+
}
111+
}
78112
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace KSFramework.KSMessaging;
2+
3+
public interface IRequestDecorator
4+
{
5+
6+
}
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1+
using Newtonsoft.Json;
2+
13
namespace KSFramework.Pagination;
24

3-
public class OrderingRequestOptions
5+
public record OrderingRequestOptions
46
: PaginationRequestOptions
57
{
8+
[property:JsonProperty("orderByPropertyName")]
69
public string OrderByPropertyName { get; set; } = "Id";
10+
11+
[property:JsonProperty("desc")]
712
public bool Desc { get; set; } = false;
813
}
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1+
using Newtonsoft.Json;
2+
13
namespace KSFramework.Pagination;
24

3-
public class PaginationRequestOptions
5+
public record PaginationRequestOptions
46
{
7+
[property:JsonProperty("pageIndex")]
58
public int PageIndex { get; set; } = 1;
9+
10+
[property:JsonProperty("pageSize")]
611
public int PageSize { get; set; } = 20;
712
}
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11

2+
using Newtonsoft.Json;
3+
24
namespace KSFramework.Pagination;
35

4-
public class SearchRequestOptions
6+
public record SearchRequestOptions
57
: OrderingRequestOptions
68
{
7-
public string SearchTerm { get; set; } = string.Empty;
9+
[property:JsonProperty("searchTerm")]
10+
public string SearchTerm { get; set; }
811
}

0 commit comments

Comments
 (0)