Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code refactoring #360

Merged
merged 7 commits into from
Apr 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using CleanArchitecture.Blazor.Application.Common.ExceptionHandlers;
using CleanArchitecture.Blazor.Application.Common.Interfaces.Identity;
using CleanArchitecture.Blazor.Application.Common.Security;
using System.Reflection;

namespace CleanArchitecture.Blazor.Application.Common.Behaviours;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ ILogger<CacheInvalidationBehaviour<TRequest, TResponse>> logger
}
public async Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> next, CancellationToken cancellationToken)
{
_logger.LogTrace("{Name} cache expire with {@Request}.", nameof(request), request);
_logger.LogTrace("{Name} cache expire with {@Request}", nameof(request), request);
var response = await next().ConfigureAwait(false);
if (!string.IsNullOrEmpty(request.CacheKey))
{
Expand Down
4 changes: 1 addition & 3 deletions src/Application/Common/Behaviours/MemoryCacheBehaviour.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using LazyCache;

namespace CleanArchitecture.Blazor.Application.Common.Behaviours;

public class MemoryCacheBehaviour<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
Expand All @@ -21,7 +19,7 @@ ILogger<MemoryCacheBehaviour<TRequest, TResponse>> logger
}
public async Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> next, CancellationToken cancellationToken)
{
_logger.LogTrace("{Name} is caching with {@Request}.", nameof(request),request);
_logger.LogTrace("{Name} is caching with {@Request}", nameof(request),request);
var response = await _cache.GetOrAddAsync(
request.CacheKey,
async () =>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Threading;

namespace CleanArchitecture.Blazor.Application.Common.Behaviours;

public class UnhandledExceptionBehaviour<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse> where TRequest : IRequest<TResponse>
Expand Down
8 changes: 4 additions & 4 deletions src/Application/Common/Behaviours/ValidationBehaviour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ public ValidationBehaviour(
public async Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> next, CancellationToken cancellationToken)
{
var context = new ValidationContext<TRequest>(request);
var failrules = _validators
var failRules = _validators
.Select(validator => validator.Validate(context))
.SelectMany(result => result.Errors)
.Where(failrules => failrules != null)
.Where(failRules => failRules != null)
.ToList();
if (failrules.Any())
if (failRules.Any())
{
throw new ValidationException(failrules);
throw new ValidationException(failRules);
}
return await next().ConfigureAwait(false);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace CleanArchitecture.Blazor.Application.Configurations;
namespace CleanArchitecture.Blazor.Application.Common.Configurations;

public class AppConfigurationSettings
{
Expand Down
6 changes: 3 additions & 3 deletions src/Application/Common/Configurations/DashbordSettings.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace CleanArchitecture.Blazor.Application.Configurations;
namespace CleanArchitecture.Blazor.Application.Common.Configurations;

public class DashboardSettings
{
public const string SectionName = nameof(DashboardSettings);

public string Version { get; set; }="6.0.2";
public string Version { get; set; } = "6.0.2";
public string App { get; set; } = "Dashboard";
public string AppName { get; set; } = "Admin Dashboard";
public string AppFlavor { get; set; } = String.Empty;
public string AppFlavorSubscript { get; set; } = String.Empty;
public string Company { get; set; } = "Company";
public string Copyright { get; set; } = "@2022 Copyright";
public string Copyright { get; set; } = "@2023 Copyright";

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace CleanArchitecture.Blazor.Application.Common.Exceptions;
namespace CleanArchitecture.Blazor.Application.Common.ExceptionHandlers;
public class ConflictException : ServerException
{
public ConflictException(string message)
Expand Down
54 changes: 25 additions & 29 deletions src/Application/Common/ExceptionHandlers/DbExceptionHandler.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading.Tasks;
using CleanArchitecture.Blazor.Application.Features.Products.Commands.AddEdit;
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
using Microsoft.Data.SqlClient;

namespace CleanArchitecture.Blazor.Application.Common.ExceptionHandlers;
public class DbExceptionHandler<TRequest, TResponse, TException> : IRequestExceptionHandler<TRequest, TResponse, TException>

public class
DbExceptionHandler<TRequest, TResponse, TException> : IRequestExceptionHandler<TRequest, TResponse, TException>
where TRequest : IRequest<Result>
where TException : DbUpdateException
{
Expand All @@ -20,51 +14,53 @@ public DbExceptionHandler(ILogger<DbExceptionHandler<TRequest, TResponse, TExcep
_logger = logger;
}

public Task Handle(TRequest request, TException exception, RequestExceptionHandlerState<TResponse> state, CancellationToken cancellationToken)
public Task Handle(TRequest request, TException exception, RequestExceptionHandlerState<TResponse> state,
CancellationToken cancellationToken)
{
var response = Activator.CreateInstance<TResponse>();
TResponse response = Activator.CreateInstance<TResponse>();
if (response is Result result)
{
result.Succeeded = false;
result.Errors = getErrors(exception);
result.Errors = GetErrors(exception);
state.SetHandled(response);
}

return Task.CompletedTask;
}

private string[] getErrors(DbUpdateException exception)
private static string[] GetErrors(DbUpdateException exception)
{
IList<string> errors = new List<string>();
if (exception.InnerException != null
&& exception.InnerException != null
&& exception.InnerException is SqlException sqlException
)
&& exception.InnerException != null
&& exception.InnerException is SqlException sqlException
)
{
switch (sqlException.Number)
{
case 2627: // Unique constraint error
errors.Add("A Unique Constraint Error Has Occured While Updating the record! Duplicate Record cannot be inserted in the System.");
case 2627: // Unique constraint error
errors.Add(
"A Unique Constraint Error Has Occured While Updating the record! Duplicate Record cannot be inserted in the System.");
break;
case 544: // Cannot insert explicit value for identity column in table 'Departments' when IDENTITY_INSERT is set to OFF
errors.Add("Cannot insert explicit value for identity column in the system when the id is set to OFF");
case 544
: // Cannot insert explicit value for identity column in table 'Departments' when IDENTITY_INSERT is set to OFF
errors.Add(
"Cannot insert explicit value for identity column in the system when the id is set to OFF");
break;
case 547: // Constraint check violation, Conflict in the database
case 547: // Constraint check violation, Conflict in the database
errors.Add("A Constraint Check violation Error Has Occured While Updating the record(s)!");
break;
case 2601: // Duplicated key row error // Constraint violation exception // A custom exception of yours for concurrency issues
case 2601
: // Duplicated key row error // Constraint violation exception // A custom exception of yours for concurrency issues
errors.Add("A Duplicate Key Error Has Occured While Updating the record(s)!");
break;
case 201: // Proceudre missing parameters
case 201: // Procedure missing parameters
errors.Add("A Missing Parameter has led to Error While Creating the record(s)!");
break;
default:
break;
}
}



return errors.ToArray();

}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace CleanArchitecture.Blazor.Application.Common.Exceptions;
namespace CleanArchitecture.Blazor.Application.Common.ExceptionHandlers;

public class ForbiddenException : ServerException
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace CleanArchitecture.Blazor.Application.Common.Exceptions;
namespace CleanArchitecture.Blazor.Application.Common.ExceptionHandlers;
public class InternalServerException : ServerException
{
public InternalServerException(string message)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace CleanArchitecture.Blazor.Application.Common.Exceptions;
namespace CleanArchitecture.Blazor.Application.Common.ExceptionHandlers;

public class NotFoundException : ServerException
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Net;

namespace CleanArchitecture.Blazor.Application.Common.Exceptions;
namespace CleanArchitecture.Blazor.Application.Common.ExceptionHandlers;
public class ServerException : Exception
{
public IEnumerable<string> ErrorMessages { get; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace CleanArchitecture.Blazor.Application.Common.Exceptions;
namespace CleanArchitecture.Blazor.Application.Common.ExceptionHandlers;
public class UnauthorizedException : ServerException
{
public UnauthorizedException(string message)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace CleanArchitecture.Blazor.Application.Common.ExceptionHandlers;
namespace CleanArchitecture.Blazor.Application.Common.ExceptionHandlers;
public class ValidationExceptionHandler<TRequest, TResponse, TException> : IRequestExceptionHandler<TRequest, TResponse, TException>
where TRequest : IRequest<Result>
where TException : ValidationException
Expand Down
2 changes: 0 additions & 2 deletions src/Application/Common/Extensions/DataRowExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Text;

namespace CleanArchitecture.Blazor.Application.Common.Extensions;

public static class DataRowExtensions
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System.ComponentModel;
using System.Reflection;

namespace CleanArchitecture.Blazor.Application.Common.Extensions;
namespace CleanArchitecture.Blazor.Application.Common.Extensions;
public static class DescriptionAttributeExtensions
{
public static string GetDescription(this Enum e)
Expand Down
Loading