Skip to content

Commit

Permalink
Migração para .NET 8
Browse files Browse the repository at this point in the history
  • Loading branch information
Alberto Monteiro committed Nov 28, 2023
1 parent e559cc2 commit a930531
Show file tree
Hide file tree
Showing 15 changed files with 98 additions and 94 deletions.
12 changes: 6 additions & 6 deletions src/HandlingErrors.Core/HandlingErrors.Core.csproj
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<LangVersion>10</LangVersion>
<TargetFramework>net8.0</TargetFramework>
<LangVersion>12</LangVersion>
<Nullable>enable</Nullable>
<ImplicitUsings>true</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Divino.OperationResult" Version="3.0.0" />
<PackageReference Include="FluentValidation" Version="11.2.1" />
<PackageReference Include="Mediatr" Version="10.0.1" />
<PackageReference Include="Refit" Version="6.3.2" />
<PackageReference Include="Divino.OperationResult" Version="3.1.0" />
<PackageReference Include="FluentValidation" Version="11.8.1" />
<PackageReference Include="Mediatr" Version="12.2.0" />
<PackageReference Include="Refit" Version="7.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,23 @@ namespace HandlingErrors.Core.RequestHandlers.Pipelines;
public sealed class ExceptionPipelineBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
where TRequest : IRequest<TResponse>
{
private readonly MethodInfo _ResultError;
private readonly MethodInfo? _resultError;
private readonly Type _type = typeof(TResponse);
private readonly Type _typeResult = typeof(Result);

public ExceptionPipelineBehavior()
{
if (_type.IsGenericType)
{
_ResultError = _typeResult.GetMethods().FirstOrDefault(m => m.Name == "Error" && m.IsGenericMethod);
_ResultError = _ResultError.MakeGenericMethod(_type.GetGenericArguments().First());
}
}
public async Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate<TResponse> next)
=> _resultError = ResultUtils.GetGenericError(_type);

public async Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> next, CancellationToken cancellationToken)
{
try
{
return await next?.Invoke();
return await next.Invoke();
}
catch (Exception e)
{
return _type == _typeResult
? (TResponse)Convert.ChangeType(Result.Error(e), _type)
: (TResponse)Convert.ChangeType(_ResultError.Invoke(null, new object[] { e }), _type);
return _type == ResultUtils.TypeResult || _resultError is null
? (TResponse)Convert.ChangeType(Result.Error(e), _type)!
: (TResponse)Convert.ChangeType(_resultError.Invoke(null, new object[] { e }), _type)!;
}
}
}
17 changes: 17 additions & 0 deletions src/HandlingErrors.Core/RequestHandlers/Pipelines/ResultUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using OperationResult;
using System.Reflection;

namespace HandlingErrors.Core.RequestHandlers.Pipelines;

public static class ResultUtils
{
internal static MethodInfo ResultError { get; }
internal static Type TypeResultGeneric { get; } = typeof(Result<>);
internal static Type TypeResult { get; } = typeof(Result);

static ResultUtils()
=> ResultError = TypeResult.GetMethods().First(m => m.Name == "Error" && m.IsGenericMethod);

internal static MethodInfo? GetGenericError(Type type)
=> type.IsGenericType ? ResultError.MakeGenericMethod(type.GetGenericArguments().First()) : null;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using FluentValidation;
using FluentValidation.Results;
using HandlingErrors.Shared;
using HandlingErrors.Shared.Exceptions;
using MediatR;
Expand All @@ -12,43 +11,37 @@ public sealed class ValidationPipelineBehavior<TRequest, TResponse> : IPipelineB
where TRequest : IValidatable, IRequest<TResponse>
{
private readonly AbstractValidator<TRequest> _validator;
private readonly MethodInfo _ResultError;
private readonly MethodInfo? _resultError;
private readonly Type _type = typeof(TResponse);
private readonly Type _typeResultGeneric = typeof(Result<>);
private readonly Type _typeResult = typeof(Result);

public ValidationPipelineBehavior(AbstractValidator<TRequest> validator)
{
_validator = validator;
if (_type.IsGenericType)
{
_ResultError = _typeResult.GetMethods().FirstOrDefault(m => m.Name == "Error" && m.IsGenericMethod);
_ResultError = _ResultError.MakeGenericMethod(_type.GetGenericArguments().First());
}
_resultError = ResultUtils.GetGenericError(_type);
}

public Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate<TResponse> next)
public Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> next, CancellationToken cancellationToken)
{
ValidationResult validationResult;
FluentValidation.Results.ValidationResult validationResult;
ModeloInvalidoException validationError;
if (_type == _typeResult)
if (_type == ResultUtils.TypeResult || _resultError is null)
{
validationResult = _validator.Validate(request);
if (validationResult.IsValid)
return next?.Invoke();
return next.Invoke();

validationError = new ModeloInvalidoException(validationResult.Errors.GroupBy(v => v.PropertyName, v => v.ErrorMessage).ToDictionary(v => v.Key, v => v.Select(y => y)));
return Task.FromResult((TResponse)Convert.ChangeType(Result.Error(validationError), _type));
}

if (!_type.IsGenericType || _type.GetGenericTypeDefinition() != _typeResultGeneric)
return next?.Invoke();
if (!_type.IsGenericType || _type.GetGenericTypeDefinition() != ResultUtils.TypeResultGeneric)
return next.Invoke();

validationResult = _validator.Validate(request);
if (validationResult.IsValid)
return next?.Invoke();
return next.Invoke();

validationError = new ModeloInvalidoException(validationResult.Errors.GroupBy(v => v.PropertyName, v => v.ErrorMessage).ToDictionary(v => v.Key, v => v.Select(y => y)));
return Task.FromResult((TResponse)Convert.ChangeType(_ResultError.Invoke(null, new object[] { validationError }), _type));
return Task.FromResult((TResponse)Convert.ChangeType(_resultError.Invoke(null, new object[] { validationError }), _type)!);
}
}
10 changes: 5 additions & 5 deletions src/HandlingErrors.Data/HandlingErrors.Data.csproj
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<LangVersion>10</LangVersion>
<TargetFramework>net8.0</TargetFramework>
<LangVersion>12</LangVersion>
<Nullable>enable</Nullable>
<ImplicitUsings>true</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AutoMapper" Version="11.0.1" />
<PackageReference Include="AutoMapper.Extensions.ExpressionMapping" Version="5.1.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.8" />
<PackageReference Include="AutoMapper" Version="12.0.1" />
<PackageReference Include="AutoMapper.Extensions.ExpressionMapping" Version="6.0.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
11 changes: 5 additions & 6 deletions src/HandlingErrors.IoC/HandlingErrors.IoC.csproj
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<LangVersion>10</LangVersion>
<TargetFramework>net8.0</TargetFramework>
<LangVersion>12</LangVersion>
<Nullable>enable</Nullable>
<ImplicitUsings>true</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AutoMapper" Version="11.0.1" />
<PackageReference Include="FluentValidation" Version="11.2.1" />
<PackageReference Include="Mediatr" Version="10.0.1" />
<PackageReference Include="MediatR.Extensions.Microsoft.DependencyInjection" Version="10.0.1" />
<PackageReference Include="AutoMapper" Version="12.0.1" />
<PackageReference Include="FluentValidation" Version="11.8.1" />
<PackageReference Include="Mediatr" Version="12.2.0" />
</ItemGroup>

<ItemGroup>
Expand Down
7 changes: 4 additions & 3 deletions src/HandlingErrors.IoC/SimpleInjectorBootstrap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,10 @@ private static void ConfigureValidators(IServiceCollection container)
private static void ConfigureMediatR(IServiceCollection container)
{
var assemblies = GetAssemblies().ToArray();
container.AddMediatR(typeof(ExceptionPipelineBehavior<,>).Assembly);
container.AddSingleton(typeof(ExceptionPipelineBehavior<,>));
container.AddSingleton(typeof(ValidationPipelineBehavior<,>));
container.AddMediatR(x => x
.RegisterServicesFromAssemblies(typeof(ExceptionPipelineBehavior<,>).Assembly)
.AddOpenBehavior(typeof(ExceptionPipelineBehavior<,>))
.AddOpenBehavior(typeof(ValidationPipelineBehavior<,>)));
}

private static IEnumerable<Assembly> GetAssemblies()
Expand Down
10 changes: 5 additions & 5 deletions src/HandlingErrors.Shared/HandlingErrors.Shared.csproj
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<LangVersion>10</LangVersion>
<TargetFramework>net8.0</TargetFramework>
<LangVersion>12</LangVersion>
<Nullable>enable</Nullable>
<ImplicitUsings>true</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AutoMapper" Version="11.0.1" />
<PackageReference Include="Divino.OperationResult" Version="3.0.0" />
<PackageReference Include="MediatR" Version="10.0.1" />
<PackageReference Include="AutoMapper" Version="12.0.1" />
<PackageReference Include="Divino.OperationResult" Version="3.1.0" />
<PackageReference Include="MediatR" Version="12.2.0" />
</ItemGroup>

</Project>
18 changes: 9 additions & 9 deletions src/HandlingErrors.Web/HandlingErrors.Web.csproj
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<LangVersion>10</LangVersion>
<TargetFramework>net8.0</TargetFramework>
<LangVersion>12</LangVersion>
<Nullable>enable</Nullable>
<ImplicitUsings>true</ImplicitUsings>
<TypescriptCompilerBlocked>true</TypescriptCompilerBlocked>
Expand All @@ -15,13 +15,13 @@

<ItemGroup>
<PackageReference Include="Aurelia.DotNet" Version="1.1.0" />
<PackageReference Include="Divino.OperationResult" Version="3.0.0" />
<PackageReference Include="Mediatr" Version="10.0.1" />
<PackageReference Include="Microsoft.AspNetCore.OData" Version="8.0.11" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="6.0.8" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.8" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="6.0.8" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
<PackageReference Include="Divino.OperationResult" Version="3.1.0" />
<PackageReference Include="Mediatr" Version="12.2.0" />
<PackageReference Include="Microsoft.AspNetCore.OData" Version="8.2.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="8.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.0" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="8.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
</ItemGroup>

<ItemGroup>
Expand Down
14 changes: 7 additions & 7 deletions tests/HandlingErrors.Core.Tests/HandlingErrors.Core.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<LangVersion>10</LangVersion>
<TargetFramework>net8.0</TargetFramework>
<LangVersion>12</LangVersion>
<Nullable>enable</Nullable>
<ImplicitUsings>true</ImplicitUsings>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.1" />
<PackageReference Include="NSubstitute" Version="4.4.0" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="NSubstitute" Version="5.1.0" />
<PackageReference Include="xunit" Version="2.6.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.1.2">
<PackageReference Include="coverlet.collector" Version="6.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public async Task WhenHandlerThrowsAnExceptionPipelineConvertToResultWithError()
static Task<Result<long>> Next() => throw new Exception("Fail");

//Act
var result = await _sut.Handle(null, CancellationToken.None, Next);
var result = await _sut.Handle(null, Next, CancellationToken.None);

//Assert
Assert.False(result.IsSuccess);
Expand All @@ -31,7 +31,7 @@ public async Task WhenHandlerDoesNotThrowsAnExceptionPipelineDoesNotChangeResult
static Task<Result<long>> Next() => Task.FromResult(Result.Success(1L));

//Act
var result = await _sut.Handle(null, CancellationToken.None, Next);
var result = await _sut.Handle(null, Next, CancellationToken.None);

//Assert
Assert.True(result.IsSuccess);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public async Task WhenModelContainsErrorsShouldReturnResultWithError()
var request = new SampleRequestGeneric();

//Act
var (success, result, exception) = await _sut.Handle(request, CancellationToken.None, null);
var (success, result, exception) = await _sut.Handle(request, null, CancellationToken.None);

//Assert
Assert.False(success);
Expand All @@ -41,7 +41,7 @@ public async Task WhenModelIsValidShouldReturnResultSuccess()
static Task<Result<long>> Next() => Task.FromResult(Result.Success(1L));

//Act
var (success, result, exception) = await _sut.Handle(request, CancellationToken.None, Next);
var (success, result, exception) = await _sut.Handle(request, Next, CancellationToken.None);

//Assert
Assert.True(success);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public async Task WhenModelContainsErrorsShouldReturnResultWithError()
var request = new SampleRequest();

//Act
var (success, exception) = await _sut.Handle(request, CancellationToken.None, null);
var (success, exception) = await _sut.Handle(request, null, CancellationToken.None);

//Assert
Assert.False(success);
Expand All @@ -41,7 +41,7 @@ public async Task WhenModelIsValidShouldReturnResultSuccess()
static Task<Result> Next() => Task.FromResult(Result.Success());

//Act
var (success, exception) = await _sut.Handle(request, CancellationToken.None, Next);
var (success, exception) = await _sut.Handle(request, Next, CancellationToken.None);

//Assert
Assert.True(success);
Expand Down
14 changes: 7 additions & 7 deletions tests/HandlingErrors.Data.Tests/HandlingErrors.Data.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<LangVersion>10</LangVersion>
<TargetFramework>net8.0</TargetFramework>
<LangVersion>12</LangVersion>
<Nullable>enable</Nullable>
<ImplicitUsings>true</ImplicitUsings>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.8" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.1" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="xunit" Version="2.6.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.1.2">
<PackageReference Include="coverlet.collector" Version="6.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
Loading

0 comments on commit a930531

Please sign in to comment.