Skip to content

Commit

Permalink
fix analyzer issues
Browse files Browse the repository at this point in the history
  • Loading branch information
pwelter34 committed Nov 8, 2024
1 parent be5ced0 commit c888a0b
Show file tree
Hide file tree
Showing 22 changed files with 83 additions and 63 deletions.
1 change: 1 addition & 0 deletions src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
<ItemGroup>
<PackageReference Include="AssemblyMetadata.Generators" Version="2.1.0" PrivateAssets="All" />
<PackageReference Include="MinVer" Version="6.0.0" PrivateAssets="All" />
<PackageReference Include="Meziantou.Analyzer" Version="2.0.177" PrivateAssets="All" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 2 additions & 0 deletions src/MediatR.CommandQuery.Audit/ChangeCollector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,9 @@ public IReadOnlyList<AuditRecord<TKey>> CollectGroupChanges<TProperty>(IEnumerab
/// </returns>
/// <exception cref="ArgumentNullException">When entities or entityName is null</exception>
/// <exception cref="ArgumentException">When entity name is empty</exception>
#pragma warning disable MA0051 // Method is too long
public IEnumerable<AuditRecord<TKey>> CollectChanges(IEnumerable<TEntity> entities, string entityName, Func<TEntity, string>? descriptionFunction = null)
#pragma warning restore MA0051 // Method is too long
{
ArgumentNullException.ThrowIfNull(entities);
ArgumentNullException.ThrowIfNull(entityName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ protected override async Task<IReadOnlyCollection<TReadModel>> Process(EntityIde
{
ArgumentNullException.ThrowIfNull(request);

var keys = new HashSet<string>();
var keys = new HashSet<string>(StringComparer.Ordinal);
foreach (var requestId in request.Ids)
{
if (CosmosKey.TryDecode(requestId, out var id, out PartitionKey partitionKey))
Expand Down
2 changes: 1 addition & 1 deletion src/MediatR.CommandQuery.Endpoints/DispatcherEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ protected virtual async Task<IResult> Send(
try
{
var request = dispatchRequest.Request;
var result = await _sender.Send(request, cancellationToken);
var result = await _sender.Send(request, cancellationToken).ConfigureAwait(false);
return Results.Ok(result);
}
catch (Exception ex)
Expand Down
14 changes: 8 additions & 6 deletions src/MediatR.CommandQuery.Endpoints/EntityCommandEndpointBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ protected EntityCommandEndpointBase(IMediator mediator, string entityName) : bas
{
}

#pragma warning disable MA0051 // Method is too long
protected override void MapGroup(RouteGroupBuilder group)
#pragma warning restore MA0051 // Method is too long
{
base.MapGroup(group);

Expand Down Expand Up @@ -90,7 +92,7 @@ protected override void MapGroup(RouteGroupBuilder group)
CancellationToken cancellationToken = default)
{
var command = new EntityIdentifierQuery<TKey, TUpdateModel>(user, id);
return await Mediator.Send(command, cancellationToken);
return await Mediator.Send(command, cancellationToken).ConfigureAwait(false);
}

protected virtual async Task<TReadModel?> CreateCommand(
Expand All @@ -99,7 +101,7 @@ protected override void MapGroup(RouteGroupBuilder group)
CancellationToken cancellationToken = default)
{
var command = new EntityCreateCommand<TCreateModel, TReadModel>(user, createModel);
return await Mediator.Send(command, cancellationToken);
return await Mediator.Send(command, cancellationToken).ConfigureAwait(false);
}

protected virtual async Task<TReadModel?> UpdateCommand(
Expand All @@ -109,7 +111,7 @@ protected override void MapGroup(RouteGroupBuilder group)
CancellationToken cancellationToken = default)
{
var command = new EntityUpdateCommand<TKey, TUpdateModel, TReadModel>(user, id, updateModel);
return await Mediator.Send(command, cancellationToken);
return await Mediator.Send(command, cancellationToken).ConfigureAwait(false);
}

protected virtual async Task<TReadModel?> UpsertCommand(
Expand All @@ -119,7 +121,7 @@ protected override void MapGroup(RouteGroupBuilder group)
CancellationToken cancellationToken = default)
{
var command = new EntityUpsertCommand<TKey, TUpdateModel, TReadModel>(user, id, updateModel);
return await Mediator.Send(command, cancellationToken);
return await Mediator.Send(command, cancellationToken).ConfigureAwait(false);
}

protected virtual async Task<TReadModel?> PatchCommand(
Expand All @@ -129,7 +131,7 @@ protected override void MapGroup(RouteGroupBuilder group)
CancellationToken cancellationToken = default)
{
var command = new EntityPatchCommand<TKey, TReadModel>(user, id, jsonPatch);
return await Mediator.Send(command, cancellationToken);
return await Mediator.Send(command, cancellationToken).ConfigureAwait(false);
}

protected virtual async Task<TReadModel?> DeleteCommand(
Expand All @@ -138,7 +140,7 @@ protected override void MapGroup(RouteGroupBuilder group)
CancellationToken cancellationToken = default)
{
var command = new EntityDeleteCommand<TKey, TReadModel>(user, id);
return await Mediator.Send(command, cancellationToken);
return await Mediator.Send(command, cancellationToken).ConfigureAwait(false);
}

}
20 changes: 13 additions & 7 deletions src/MediatR.CommandQuery.Endpoints/EntityQueryEndpointBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ public override void AddRoutes(IEndpointRouteBuilder app)
}


#pragma warning disable MA0051 // Method is too long
protected virtual void MapGroup(RouteGroupBuilder group)
#pragma warning restore MA0051 // Method is too long
{
group
.MapGet("{id}", GetQuery)
Expand Down Expand Up @@ -117,7 +119,7 @@ protected virtual void MapGroup(RouteGroupBuilder group)
CancellationToken cancellationToken = default)
{
var command = new EntityIdentifierQuery<TKey, TReadModel>(user, id);
return await Mediator.Send(command, cancellationToken);
return await Mediator.Send(command, cancellationToken).ConfigureAwait(false);
}

protected virtual async Task<EntityPagedResult<TListModel>> GetPagedQuery(
Expand All @@ -130,7 +132,7 @@ protected virtual async Task<EntityPagedResult<TListModel>> GetPagedQuery(
{
var entityQuery = new EntityQuery(q, page ?? 1, size ?? 20, sort);
var command = new EntityPagedQuery<TListModel>(user, entityQuery);
return await Mediator.Send(command, cancellationToken);
return await Mediator.Send(command, cancellationToken).ConfigureAwait(false);
}

protected virtual async Task<EntityPagedResult<TListModel>> PostPagedQuery(
Expand All @@ -139,7 +141,7 @@ protected virtual async Task<EntityPagedResult<TListModel>> PostPagedQuery(
CancellationToken cancellationToken = default)
{
var command = new EntityPagedQuery<TListModel>(user, entityQuery);
return await Mediator.Send(command, cancellationToken);
return await Mediator.Send(command, cancellationToken).ConfigureAwait(false);
}

protected virtual async Task<IReadOnlyCollection<TListModel>> GetSelectQuery(
Expand All @@ -150,7 +152,7 @@ protected virtual async Task<IReadOnlyCollection<TListModel>> GetSelectQuery(
{
var entitySelect = new EntitySelect(q, sort);
var command = new EntitySelectQuery<TListModel>(user, entitySelect);
return await Mediator.Send(command, cancellationToken);
return await Mediator.Send(command, cancellationToken).ConfigureAwait(false);
}

protected virtual async Task<IReadOnlyCollection<TListModel>> PostSelectQuery(
Expand All @@ -159,7 +161,7 @@ protected virtual async Task<IReadOnlyCollection<TListModel>> PostSelectQuery(
CancellationToken cancellationToken = default)
{
var command = new EntitySelectQuery<TListModel>(user, entitySelect);
return await Mediator.Send(command, cancellationToken);
return await Mediator.Send(command, cancellationToken).ConfigureAwait(false);
}

protected virtual async Task<IResult> PostExportQuery(
Expand All @@ -169,13 +171,15 @@ protected virtual async Task<IResult> PostExportQuery(
CancellationToken cancellationToken = default)
{
var command = new EntitySelectQuery<TListModel>(user, entitySelect);
var results = await Mediator.Send(command, cancellationToken);
var results = await Mediator.Send(command, cancellationToken).ConfigureAwait(false);

csvConfiguration ??= new CsvConfiguration(CultureInfo.InvariantCulture) { HasHeaderRecord = true };

#pragma warning disable MA0004 // Use Task.ConfigureAwait
await using var memoryStream = new MemoryStream();
await using var streamWriter = new StreamWriter(memoryStream);
await using var csvWriter = new CsvWriter(streamWriter, csvConfiguration);
#pragma warning restore MA0004 // Use Task.ConfigureAwait

WriteExportData(csvWriter, results);

Expand All @@ -197,13 +201,15 @@ protected virtual async Task<IResult> GetExportQuery(

var entitySelect = QueryStringEncoder.Decode<EntitySelect>(encodedQuery, jsonSerializerOptions) ?? new EntitySelect();
var command = new EntitySelectQuery<TListModel>(user, entitySelect);
var results = await Mediator.Send(command, cancellationToken);
var results = await Mediator.Send(command, cancellationToken).ConfigureAwait(false);

csvConfiguration ??= new CsvConfiguration(CultureInfo.InvariantCulture) { HasHeaderRecord = true };

#pragma warning disable MA0004 // Use Task.ConfigureAwait
await using var memoryStream = new MemoryStream();
await using var streamWriter = new StreamWriter(memoryStream);
await using var csvWriter = new CsvWriter(streamWriter, csvConfiguration);
#pragma warning restore MA0004 // Use Task.ConfigureAwait

WriteExportData(csvWriter, results);

Expand Down
17 changes: 10 additions & 7 deletions src/MediatR.CommandQuery.Endpoints/ProblemDetailsCustomizer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using Microsoft.AspNetCore.Diagnostics;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
Expand All @@ -24,8 +23,12 @@ public static void CustomizeProblemDetails(ProblemDetailsContext context)
case FluentValidation.ValidationException fluentException:
{

var errors = fluentException.Errors.GroupBy(x => x.PropertyName)
.ToDictionary(g => g.Key, g => g.Select(x => x.ErrorMessage).ToArray());
var errors = fluentException.Errors
.GroupBy(x => x.PropertyName, StringComparer.Ordinal)
.ToDictionary(
keySelector: g => g.Key,
elementSelector: g => g.Select(x => x.ErrorMessage).ToArray(),
comparer: StringComparer.Ordinal);

AddValidationErrors(context, errors);

Expand All @@ -35,7 +38,7 @@ public static void CustomizeProblemDetails(ProblemDetailsContext context)
}
case System.ComponentModel.DataAnnotations.ValidationException validationException:
{
var errors = new Dictionary<string, string[]>();
var errors = new Dictionary<string, string[]>(StringComparer.Ordinal);

if (validationException.ValidationResult.ErrorMessage != null)
foreach (var memberName in validationException.ValidationResult.MemberNames)
Expand Down Expand Up @@ -95,8 +98,8 @@ public static ProblemDetails ToProblemDetails(this Exception exception)
case FluentValidation.ValidationException fluentException:
{
var errors = fluentException.Errors
.GroupBy(x => x.PropertyName)
.ToDictionary(g => g.Key, g => g.Select(x => x.ErrorMessage).ToArray());
.GroupBy(x => x.PropertyName, StringComparer.Ordinal)
.ToDictionary(g => g.Key, g => g.Select(x => x.ErrorMessage).ToArray(), StringComparer.Ordinal);

problemDetails.Title = "One or more validation errors occurred.";
problemDetails.Status = StatusCodes.Status400BadRequest;
Expand All @@ -105,7 +108,7 @@ public static ProblemDetails ToProblemDetails(this Exception exception)
}
case System.ComponentModel.DataAnnotations.ValidationException validationException:
{
var errors = new Dictionary<string, string[]>();
var errors = new Dictionary<string, string[]>(StringComparer.Ordinal);

if (validationException.ValidationResult.ErrorMessage != null)
foreach (var memberName in validationException.ValidationResult.MemberNames)
Expand Down
6 changes: 3 additions & 3 deletions src/MediatR.CommandQuery.Hangfire/MediatorDispatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public async Task Send<TRequest>(TRequest request, PerformContext? context, Canc
LogStart(_logger, request);
var watch = Stopwatch.StartNew();

var response = await _mediator.Send(request, cancellationToken);
var response = await _mediator.Send(request, cancellationToken).ConfigureAwait(false);

watch.Stop();
LogFinish(_logger, request, watch.ElapsedMilliseconds);
Expand All @@ -46,8 +46,8 @@ public async Task Send<TRequest>(TRequest request, PerformContext? context, Canc
}
finally
{
var elaspsed = ActivityTimer.GetElapsedTime(startTime);
LogFinish(_logger, request, elaspsed.TotalMilliseconds);
var elapsed = ActivityTimer.GetElapsedTime(startTime);
LogFinish(_logger, request, elapsed.TotalMilliseconds);

scope?.Dispose();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ protected override async Task<EntityPagedResult<TReadModel>> Process(EntityPaged
return new EntityPagedResult<TReadModel> { Data = new List<TReadModel>() };

// page the query and convert to read model
var result = await QueryPaged(request, query, cancellationToken);
var result = await QueryPaged(request, query, cancellationToken).ConfigureAwait(false);

return new EntityPagedResult<TReadModel>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ protected override Task<IReadOnlyCollection<TReadModel>> Process(EntitySelectQue
// page the query and convert to read model
var result = QueryList(request, query, cancellationToken);

//TODO make async?
return Task.FromResult(result);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ protected virtual bool HasDeletedFilter(EntityFilter? originalFilter)
while (stack.Count > 0)
{
var filter = stack.Pop();
if (!string.IsNullOrEmpty(filter.Name) && filter.Name == nameof(ITrackDeleted.IsDeleted))
if (!string.IsNullOrEmpty(filter.Name) && string.Equals(filter.Name, nameof(ITrackDeleted.IsDeleted), StringComparison.Ordinal))
return true;

if (filter.Filters == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ protected override async Task<TResponse> Process(
var response = await next().ConfigureAwait(false);

if (response is not null)
await SendNotification(request, response, cancellationToken);
await SendNotification(request, response, cancellationToken).ConfigureAwait(false);

return response;
}
Expand All @@ -47,6 +47,6 @@ private async Task SendNotification(
};

var notification = new EntityChangeNotification<TResponse>(response, operation);
await _mediator.Publish(notification, cancellationToken);
await _mediator.Publish(notification, cancellationToken).ConfigureAwait(false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ protected override async Task<TResponse> Process(

var cacheTag = cacheRequest.GetCacheTag();
if (!string.IsNullOrEmpty(cacheTag))
await _hybridCache.RemoveByTagAsync(cacheTag, cancellationToken);
await _hybridCache.RemoveByTagAsync(cacheTag, cancellationToken).ConfigureAwait(false);

return response;
}
Expand Down
14 changes: 8 additions & 6 deletions src/MediatR.CommandQuery/Behaviors/HybridCacheQueryBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,13 @@ protected override async Task<TResponse> Process(
LocalCacheExpiration = cacheRequest.SlidingExpiration(),
};

return await _hybridCache.GetOrCreateAsync(
key: cacheKey,
factory: async token => await next().ConfigureAwait(false),
options: cacheOptions,
tags: string.IsNullOrEmpty(cacheTag) ? null : [cacheTag],
cancellationToken: cancellationToken);
return await _hybridCache
.GetOrCreateAsync(
key: cacheKey,
factory: async token => await next().ConfigureAwait(false),
options: cacheOptions,
tags: string.IsNullOrEmpty(cacheTag) ? null : [cacheTag],
cancellationToken: cancellationToken)
.ConfigureAwait(false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ private async Task Authorize(EntityModelCommand<TEntityModel, TResponse> request
if (request.Model is not IHaveTenant<TKey> tenantModel)
return;

var tenantId = await _tenantResolver.GetTenantId(principal);
var tenantId = await _tenantResolver.GetTenantId(principal).ConfigureAwait(false);
if (Equals(tenantId, tenantModel.TenantId))
return;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ private async Task SetTenantId(EntityModelCommand<TEntityModel, TResponse> reque
if (!Equals(tenantModel.TenantId, default(TKey)))
return;

var tenantId = await _tenantResolver.GetTenantId(request.Principal);
var tenantId = await _tenantResolver.GetTenantId(request.Principal).ConfigureAwait(false);
tenantModel.TenantId = tenantId!;
}
}
2 changes: 1 addition & 1 deletion src/MediatR.CommandQuery/Dispatcher/MediatorDispatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ public MediatorDispatcher(ISender sender)

public async Task<TResponse?> Send<TResponse>(IRequest<TResponse> request, CancellationToken cancellationToken = default)
{
return await _sender.Send(request, cancellationToken);
return await _sender.Send(request, cancellationToken).ConfigureAwait(false);
}
}
Loading

0 comments on commit c888a0b

Please sign in to comment.