Skip to content

Commit

Permalink
Merge pull request #57 from neuroglia-io/fix-mediation
Browse files Browse the repository at this point in the history
Fixed the WhenNullReference Guard Clause to use the camel-cased name of the reference type instead of its full name
  • Loading branch information
cdavernas authored Nov 20, 2023
2 parents 585a4f2 + 7abec62 commit 1eb98a3
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/Neuroglia.Data.Guards/GuardClause.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public IGuardClause<T> WhenNotNull(GuardException ex)
}

/// <inheritdoc/>
public IGuardClause<T> WhenNullReference<TKey>(TKey key, string keyName) => this.WhenNullReference(key, new GuardException(StringFormatter.NamedFormat(GuardExceptionMessages.when_null_reference, new { type = typeof(T), key, keyName }), this.ArgumentName));
public IGuardClause<T> WhenNullReference<TKey>(TKey key, string keyName) => this.WhenNullReference(key, new GuardException(StringFormatter.NamedFormat(GuardExceptionMessages.when_null_reference, new { type = typeof(T).Name.ToCamelCase(), key, keyName }), this.ArgumentName));

/// <inheritdoc/>
public IGuardClause<T> WhenNullReference<TKey>(TKey key, GuardException ex)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public static ActionResult Process<TResult>(this ControllerBase controller, TRes
if (result.Status == (int)HttpStatusCode.Forbidden) return controller.StatusCode((int)HttpStatusCode.Forbidden);
if (result.Status == (int)HttpStatusCode.BadRequest)
{
result.Errors?.ToList().ForEach(e => controller.ModelState.AddModelError(e.Title!, e.Detail!));
result.Errors?.ToList().SelectMany(e => e.Errors == null ? new Dictionary<string, string[]>() { { e.Title!, new string[] { e.Detail! } } }.ToList() : e.Errors.ToList()).ToList().ForEach(e => controller.ModelState.AddModelError(e.Key, string.Join(Environment.NewLine, e.Value)));
return controller.ValidationProblem(controller.ModelState);
}
if (result.Status == (int)HttpStatusCode.NotFound)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ protected virtual bool TryCreateErrorResponse(int resultCode, out TResult result
result = default!;
return false;
}
result = (TResult)Activator.CreateInstance(responseType, resultCode, errors)!;
result = (TResult)Activator.CreateInstance(responseType, resultCode, null, errors)!;
return true;
}

Expand Down
10 changes: 5 additions & 5 deletions src/Neuroglia.Mediation/Services/Mediator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ public class Mediator
/// </summary>
public const string ActivitySourceName = "Neuroglia.Mediation.Diagnostics.ActivitySource";

private static readonly ActivitySource _ActivitySource = new(ActivitySourceName);
private static readonly ConcurrentDictionary<Type, object> _RequestHandlers = new();
static readonly ActivitySource _activitySource = new(ActivitySourceName);
static readonly ConcurrentDictionary<Type, object> _requestHandlers = [];

/// <summary>
/// Initializes a new <see cref="Mediator"/>
Expand All @@ -52,10 +52,10 @@ public virtual async Task<TResult> ExecuteAsync<TResult>(IRequest<TResult> reque
{
if (request == null) throw new ArgumentNullException(nameof(request));
var requestType = request.GetType();
using var activity = _ActivitySource.StartActivity(requestType.Name);
using var activity = _activitySource.StartActivity(requestType.Name);
activity?.AddTag("request.type", requestType.FullName);
activity?.AddTag("request.cqrs_type", request is ICommand ? "command" : "query");
var pipeline = (RequestPipeline<TResult>)_RequestHandlers.GetOrAdd(requestType, t => Activator.CreateInstance(typeof(RequestPipeline<,>).MakeGenericType(requestType, typeof(TResult)))!);
var pipeline = (RequestPipeline<TResult>)_requestHandlers.GetOrAdd(requestType, t => Activator.CreateInstance(typeof(RequestPipeline<,>).MakeGenericType(requestType, typeof(TResult)))!);
var result = await pipeline.HandleAsync(request, this.ServiceProvider, cancellationToken).ConfigureAwait(false);
return result;
}
Expand All @@ -64,7 +64,7 @@ public virtual async Task<TResult> ExecuteAsync<TResult>(IRequest<TResult> reque
public virtual async Task PublishAsync<TNotification>(TNotification notification, CancellationToken cancellationToken = default)
{
if (notification == null) throw new ArgumentNullException(nameof(notification));
using var activity = _ActivitySource.StartActivity(typeof(TNotification).Name);
using var activity = _activitySource.StartActivity(typeof(TNotification).Name);
activity?.AddTag("notification.type", typeof(TNotification).FullName);
foreach (var handler in this.ServiceProvider.GetServices<INotificationHandler<TNotification>>())
{
Expand Down

0 comments on commit 1eb98a3

Please sign in to comment.