Skip to content

Commit 7edffbd

Browse files
authored
Merge pull request #28 from A-Programmer/develop
Add pagination requests
2 parents 72c6ce5 + 788dd8a commit 7edffbd

File tree

5 files changed

+78
-2
lines changed

5 files changed

+78
-2
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: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using Newtonsoft.Json;
2+
3+
namespace KSFramework.Pagination;
4+
5+
public record OrderingRequestOptions
6+
: PaginationRequestOptions
7+
{
8+
[property:JsonProperty("orderByPropertyName")]
9+
public string OrderByPropertyName { get; set; } = "Id";
10+
11+
[property:JsonProperty("desc")]
12+
public bool Desc { get; set; } = false;
13+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using Newtonsoft.Json;
2+
3+
namespace KSFramework.Pagination;
4+
5+
public record PaginationRequestOptions
6+
{
7+
[property:JsonProperty("pageIndex")]
8+
public int PageIndex { get; set; } = 1;
9+
10+
[property:JsonProperty("pageSize")]
11+
public int PageSize { get; set; } = 20;
12+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
using Newtonsoft.Json;
3+
4+
namespace KSFramework.Pagination;
5+
6+
public record SearchRequestOptions
7+
: OrderingRequestOptions
8+
{
9+
[property:JsonProperty("searchTerm")]
10+
public string SearchTerm { get; set; }
11+
}

0 commit comments

Comments
 (0)