Skip to content

Commit 5a2321e

Browse files
committed
Fix BadRequestException message, refactor Mediator registration
- Correct BadRequestException message from "Unathorized" to "Bad request" - Refactor Mediator registration: use HashSet for interfaces, replace Fill with AddRangeDistinct, and improve naming - Use Count > 0 instead of Any() for processor registration checks - Clarify estimatedPerWindow calculation in AesGcmStreamCipher - Remove stray blank line in ShellLink.cs
1 parent 9a908a5 commit 5a2321e

4 files changed

Lines changed: 30 additions & 30 deletions

File tree

Sources/EasyExtensions.AspNetCore/Exceptions/BadRequestException.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ namespace EasyExtensions.AspNetCore.Exceptions
1010
/// </summary>
1111
/// <param name="objectName">The name of the object associated with the unauthorized or invalid request.</param>
1212
public class BadRequestException(string objectName)
13-
: WebApiException(HttpStatusCode.BadRequest, objectName, "Unathorized")
13+
: WebApiException(HttpStatusCode.BadRequest, objectName, "Bad request")
1414
{ }
1515
}

Sources/EasyExtensions.Crypto/AesGcmStreamCipher.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ private int ComputeEffectiveWindowCap(int chunkSize)
153153
return Math.Clamp(_windowCap, 4, int.MaxValue);
154154
}
155155
// Estimate per-window memory footprint: ciphertext buffer ~ chunkSize + headers/overhead ~ 64KB
156-
long estimatedPerWindow = cs + 64L * 1024L;
156+
long estimatedPerWindow = cs + (64L * 1024L);
157157
long maxWindowsByMem = Math.Max(4, _memoryLimitBytes.Value / Math.Max(estimatedPerWindow, 1));
158158
int effective = (int)Math.Clamp(maxWindowsByMem, 4, int.MaxValue);
159159
return Math.Min(_windowCap, effective);

Sources/EasyExtensions.Mediator/Registration/ServiceRegistrar.cs

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,9 @@ private static void ConnectImplementationsToTypesClosing(Type openRequestInterfa
136136
CancellationToken cancellationToken = default)
137137
{
138138
var concretions = new List<Type>();
139-
var interfaces = new List<Type>();
139+
var interfaces = new HashSet<Type>();
140140
var genericConcretions = new List<Type>();
141-
var genericInterfaces = new List<Type>();
141+
var genericInterfaces = new HashSet<Type>();
142142

143143
var types = assembliesToScan
144144
.SelectMany(a => a.DefinedTypes)
@@ -154,55 +154,48 @@ private static void ConnectImplementationsToTypesClosing(Type openRequestInterfa
154154
if (!type.IsOpenGeneric())
155155
{
156156
concretions.Add(type);
157-
158-
foreach (var interfaceType in interfaceTypes)
159-
{
160-
interfaces.Fill(interfaceType);
161-
}
157+
AddRangeDistinct(interfaces, interfaceTypes);
162158
}
163159
else
164160
{
165161
genericConcretions.Add(type);
166-
foreach (var interfaceType in interfaceTypes)
167-
{
168-
genericInterfaces.Fill(interfaceType);
169-
}
162+
AddRangeDistinct(genericInterfaces, interfaceTypes);
170163
}
171164
}
172165

173-
foreach (var @interface in interfaces)
166+
foreach (var iface in interfaces)
174167
{
175-
var exactMatches = concretions.Where(x => x.CanBeCastTo(@interface)).ToList();
168+
var exactMatches = concretions.Where(x => x.CanBeCastTo(iface)).ToList();
176169
if (addIfAlreadyExists)
177170
{
178171
foreach (var type in exactMatches)
179172
{
180-
services.AddTransient(@interface, type);
173+
services.AddTransient(iface, type);
181174
}
182175
}
183176
else
184177
{
185178
if (exactMatches.Count > 1)
186179
{
187-
exactMatches.RemoveAll(m => !IsMatchingWithInterface(m, @interface));
180+
exactMatches.RemoveAll(m => !IsMatchingWithInterface(m, iface));
188181
}
189182

190183
foreach (var type in exactMatches)
191184
{
192-
services.TryAddTransient(@interface, type);
185+
services.TryAddTransient(iface, type);
193186
}
194187
}
195188

196-
if (!@interface.IsOpenGeneric())
189+
if (!iface.IsOpenGeneric())
197190
{
198-
AddConcretionsThatCouldBeClosed(@interface, concretions, services);
191+
AddConcretionsThatCouldBeClosed(iface, concretions, services);
199192
}
200193
}
201194

202-
foreach (var @interface in genericInterfaces)
195+
foreach (var iface in genericInterfaces)
203196
{
204-
var exactMatches = genericConcretions.Where(x => x.CanBeCastTo(@interface)).ToList();
205-
AddAllConcretionsThatClose(@interface, exactMatches, services, assembliesToScan, cancellationToken);
197+
var exactMatches = genericConcretions.Where(x => x.CanBeCastTo(iface)).ToList();
198+
AddAllConcretionsThatClose(iface, exactMatches, services, assembliesToScan, cancellationToken);
206199
}
207200
}
208201

@@ -454,13 +447,21 @@ private static bool IsConcrete(this Type type)
454447
return !type.IsAbstract && !type.IsInterface;
455448
}
456449

457-
private static void Fill<T>(this IList<T> list, T value)
450+
private static void AddRangeDistinct<T>(ISet<T> set, IEnumerable<T> values)
458451
{
459-
if (list.Contains(value))
452+
if (set == null)
453+
{
454+
throw new ArgumentNullException(nameof(set));
455+
}
456+
if (values == null)
457+
{
458+
throw new ArgumentNullException(nameof(values));
459+
}
460+
461+
foreach (var value in values)
460462
{
461-
return;
463+
set.Add(value);
462464
}
463-
list.Add(value);
464465
}
465466

466467
/// <summary>
@@ -498,13 +499,13 @@ public static void AddRequiredServices(IServiceCollection services, MediatorServ
498499
RegisterBehaviorIfImplementationsExist(services, typeof(RequestExceptionActionProcessorBehavior<,>), typeof(IRequestExceptionAction<,>));
499500
}
500501

501-
if (serviceConfiguration.RequestPreProcessorsToRegister.Any())
502+
if (serviceConfiguration.RequestPreProcessorsToRegister.Count > 0)
502503
{
503504
services.TryAddEnumerable(new ServiceDescriptor(typeof(IPipelineBehavior<,>), typeof(RequestPreProcessorBehavior<,>), ServiceLifetime.Transient));
504505
services.TryAddEnumerable(serviceConfiguration.RequestPreProcessorsToRegister);
505506
}
506507

507-
if (serviceConfiguration.RequestPostProcessorsToRegister.Any())
508+
if (serviceConfiguration.RequestPostProcessorsToRegister.Count > 0)
508509
{
509510
services.TryAddEnumerable(new ServiceDescriptor(typeof(IPipelineBehavior<,>), typeof(RequestPostProcessorBehavior<,>), ServiceLifetime.Transient));
510511
services.TryAddEnumerable(serviceConfiguration.RequestPostProcessorsToRegister);

Sources/EasyExtensions.Windows/ShellLink.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,6 @@ void GetPath([Out(), MarshalAs(UnmanagedType.LPStr)] StringBuilder pszFile,
194194
void SetPath([MarshalAs(UnmanagedType.LPStr)] string pszFile);
195195
}
196196

197-
198197
/// <summary>
199198
/// IShellLinkW interface for managing shell links (shortcuts) in Windows.
200199
/// </summary>

0 commit comments

Comments
 (0)