Skip to content

Commit eaa6380

Browse files
committed
refactor(ServiceRegistrator): enhance handler resolution logic and improve readability
- Removed unnecessary using directive for Notification. - Refactored the handler resolution logic to use pattern matching for better clarity and performance. - Updated the handling of single handler scenarios to utilize IReadOnlyList for improved type safety and readability. - These changes streamline the service registration process and enhance maintainability without altering existing functionality.
1 parent f8969fa commit eaa6380

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

src/DispatchR/Configuration/ServiceRegistrator.cs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using DispatchR.Abstractions.Notification;
2-
using DispatchR.Abstractions.Send;
1+
using DispatchR.Abstractions.Send;
32
using Microsoft.Extensions.DependencyInjection;
43

54
namespace DispatchR.Configuration
@@ -142,20 +141,23 @@ public static void RegisterHandlers(IServiceCollection services, List<Type> allT
142141
}
143142
}
144143

145-
services.AddScoped(handlerInterface, sp =>
146-
{
147-
var keyedServices = sp.GetKeyedServices<IRequestHandler>(key);
144+
services.AddScoped(handlerInterface, sp =>
145+
{
146+
var keyedServices = sp.GetKeyedServices<IRequestHandler>(key);
148147

149-
var pipelinesWithHandler = keyedServices as IRequestHandler[] ?? keyedServices.ToArray();
148+
IReadOnlyList<IRequestHandler> pipelinesWithHandler = keyedServices switch
149+
{
150+
IRequestHandler[] asArray => asArray,
151+
IReadOnlyList<IRequestHandler> asList => asList,
152+
_ => keyedServices.ToArray()
153+
};
150154

151-
// Single handler - no pipeline chaining needed
152-
if (pipelinesWithHandler.Length == 1)
153-
{
154-
return pipelinesWithHandler[0];
155-
}
155+
// Single handler - no pipeline chaining needed
156+
if (pipelinesWithHandler.Count == 1)
157+
return pipelinesWithHandler[0];
156158

157159
IRequestHandler lastPipeline = pipelinesWithHandler[0];
158-
for (int i = 1; i < pipelinesWithHandler.Length; i++)
160+
for (var i = 1; i < pipelinesWithHandler.Count; i++)
159161
{
160162
var pipeline = pipelinesWithHandler[i];
161163
pipeline.SetNext(lastPipeline);

0 commit comments

Comments
 (0)