-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added mediatr structure and some sample. * Upgrade EF Core to 7.0.7 * Added back the dbcontext as quick fix for the di, will be removed later. * User exp years calc.
- Loading branch information
Showing
29 changed files
with
535 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
{ | ||
// Use IntelliSense to learn about possible attributes. | ||
// Hover to view descriptions of existing attributes. | ||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 | ||
"version": "0.2.0", | ||
"configurations": [ | ||
|
||
{ | ||
"name": ".NET Core Launch (API)", | ||
"type": "coreclr", | ||
"request": "launch", | ||
"preLaunchTask": "build", | ||
"program": "${workspaceFolder}/backend/src/ThreeTee.Api/bin/Debug/net7.0/ThreeTee.Api.dll", | ||
"args": [], | ||
"cwd": "${workspaceFolder}/backend/src/ThreeTee.Api", | ||
"stopAtEntry": false, | ||
"serverReadyAction": { | ||
"action": "openExternally", | ||
"pattern": "\\bNow listening on:\\s+(https?://\\S+)" | ||
}, | ||
"env": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
}, | ||
"sourceFileMap": { | ||
"/Views": "${workspaceFolder}/Views" | ||
} | ||
}, | ||
{ | ||
"name": ".NET Core Launch (Authentication)", | ||
"type": "coreclr", | ||
"request": "launch", | ||
"preLaunchTask": "build", | ||
"program": "${workspaceFolder}/backend/src/ThreeTee.Authentication/bin/Debug/net7.0/ThreeTee.Authentication.dll", | ||
"args": [], | ||
"cwd": "${workspaceFolder}/backend/src/ThreeTee.Authentication", | ||
"stopAtEntry": false, | ||
"serverReadyAction": { | ||
"action": "openExternally", | ||
"pattern": "\\bNow listening on:\\s+(https?://\\S+)" | ||
}, | ||
"env": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
}, | ||
"sourceFileMap": { | ||
"/Views": "${workspaceFolder}/Views" | ||
} | ||
}, | ||
{ | ||
"name": ".NET Core Attach", | ||
"type": "coreclr", | ||
"request": "attach" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"dotnet.defaultSolution": "backend\\ThreeTee.sln" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
{ | ||
"version": "2.0.0", | ||
"tasks": [ | ||
{ | ||
"label": "build", | ||
"command": "dotnet", | ||
"type": "process", | ||
"args": [ | ||
"build", | ||
"${workspaceFolder}/backend/ThreeTee.sln", | ||
"/property:GenerateFullPaths=true", | ||
"/consoleloggerparameters:NoSummary" | ||
], | ||
"problemMatcher": "$msCompile" | ||
}, | ||
{ | ||
"label": "publish", | ||
"command": "dotnet", | ||
"type": "process", | ||
"args": [ | ||
"publish", | ||
"${workspaceFolder}/backend/ThreeTee.sln", | ||
"/property:GenerateFullPaths=true", | ||
"/consoleloggerparameters:NoSummary" | ||
], | ||
"problemMatcher": "$msCompile" | ||
}, | ||
{ | ||
"label": "watch", | ||
"command": "dotnet", | ||
"type": "process", | ||
"args": [ | ||
"watch", | ||
"run", | ||
"--project", | ||
"${workspaceFolder}/backend/ThreeTee.sln" | ||
], | ||
"problemMatcher": "$msCompile" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using Microsoft.AspNetCore.Mvc; | ||
using MediatR; | ||
|
||
namespace ThreeTee.Api.Controllers; | ||
|
||
[ApiController] | ||
[Route("api/[controller]")] | ||
public abstract class ApiControllerBase : ControllerBase | ||
{ | ||
private ISender? _mediator; | ||
|
||
protected ISender Mediator => _mediator ??= HttpContext.RequestServices.GetRequiredService<ISender>(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
backend/src/ThreeTee.Application/Behaviours/LoggingBehaviour.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using MediatR.Pipeline; | ||
using Microsoft.Extensions.Logging; | ||
|
||
public class LoggingBehaviour<TRequest> : IRequestPreProcessor<TRequest> where TRequest : notnull | ||
{ | ||
private readonly ILogger _logger; | ||
|
||
public LoggingBehaviour(ILogger<TRequest> logger) | ||
{ | ||
_logger = logger; | ||
} | ||
|
||
public async Task Process(TRequest request, CancellationToken cancellationToken) | ||
{ | ||
var requestName = typeof(TRequest).Name; | ||
_logger.LogInformation("Request: {Name} {@Request}", | ||
requestName, request); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
backend/src/ThreeTee.Application/Behaviours/PerformanceBehaviour.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using System.Diagnostics; | ||
using MediatR; | ||
using Microsoft.Extensions.Logging; | ||
|
||
public class PerformanceBehaviour<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse> where TRequest : notnull | ||
{ | ||
private readonly Stopwatch _timer; | ||
private readonly ILogger<TRequest> _logger; | ||
|
||
public PerformanceBehaviour( | ||
ILogger<TRequest> logger) | ||
{ | ||
_timer = new Stopwatch(); | ||
|
||
_logger = logger; | ||
} | ||
|
||
public async Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> next, CancellationToken cancellationToken) | ||
{ | ||
_timer.Start(); | ||
|
||
var response = await next(); | ||
|
||
_timer.Stop(); | ||
|
||
var elapsedMilliseconds = _timer.ElapsedMilliseconds; | ||
|
||
if (elapsedMilliseconds > 500) | ||
{ | ||
var requestName = typeof(TRequest).Name; | ||
var userName = string.Empty; | ||
|
||
|
||
_logger.LogWarning("Long Running Request: {Name} ({ElapsedMilliseconds} milliseconds) {@UserName} {@Request}", | ||
requestName, elapsedMilliseconds, userName, request); | ||
} | ||
|
||
return response; | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
backend/src/ThreeTee.Application/Behaviours/UnhandledExceptionBehaviour.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using MediatR; | ||
using Microsoft.Extensions.Logging; | ||
|
||
public class UnhandledExceptionBehaviour<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse> where TRequest : notnull | ||
{ | ||
private readonly ILogger<TRequest> _logger; | ||
|
||
public UnhandledExceptionBehaviour(ILogger<TRequest> logger) | ||
{ | ||
_logger = logger; | ||
} | ||
|
||
public async Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> next, CancellationToken cancellationToken) | ||
{ | ||
try | ||
{ | ||
return await next(); | ||
} | ||
catch (Exception ex) | ||
{ | ||
var requestName = typeof(TRequest).Name; | ||
|
||
_logger.LogError(ex, "CleanArchitecture Request: Unhandled Exception for Request {Name} {@Request}", requestName, request); | ||
|
||
throw; | ||
} | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
backend/src/ThreeTee.Application/Behaviours/ValidationBehaviour.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using FluentValidation; | ||
using MediatR; | ||
|
||
public class ValidationBehaviour<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse> | ||
where TRequest : notnull | ||
{ | ||
private readonly IEnumerable<IValidator<TRequest>> _validators; | ||
|
||
public ValidationBehaviour(IEnumerable<IValidator<TRequest>> validators) | ||
{ | ||
_validators = validators; | ||
} | ||
|
||
public async Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> next, CancellationToken cancellationToken) | ||
{ | ||
if (_validators.Any()) | ||
{ | ||
var context = new ValidationContext<TRequest>(request); | ||
|
||
var validationResults = await Task.WhenAll( | ||
_validators.Select(v => | ||
v.ValidateAsync(context, cancellationToken))); | ||
|
||
var failures = validationResults | ||
.Where(r => r.Errors.Any()) | ||
.SelectMany(r => r.Errors) | ||
.ToList(); | ||
|
||
if (failures.Any()) | ||
throw new ValidationException(failures); | ||
} | ||
return await next(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using System.Reflection; | ||
using FluentValidation; | ||
using Mapster; | ||
using MapsterMapper; | ||
using MediatR; | ||
// using MapsterMapper; | ||
using ThreeTee.Application.Interfaces; | ||
using ThreeTee.Application.Services; | ||
|
||
namespace Microsoft.Extensions.DependencyInjection; | ||
|
||
public static class ConfigureServices | ||
{ | ||
public static IServiceCollection AddApplicationServices(this IServiceCollection services) | ||
{ | ||
//remove these services later | ||
services.AddScoped<IProjectService, ProjectService>(); | ||
services.AddScoped<IBillingTypeService, BillingTypeService>(); | ||
services.AddScoped<IClientService, ClientService>(); | ||
services.AddScoped<IStatusService, StatusService>(); | ||
services.AddScoped<IDepartmentService, DepartmentService>(); | ||
services.AddScoped<IDesignationService, DesignationService>(); | ||
services.AddScoped<IProjectUserService, ProjectUserService>(); | ||
//Mapster | ||
var config = new TypeAdapterConfig(); | ||
services.AddSingleton(config); | ||
services.AddScoped<IMapper, ServiceMapper>(); | ||
|
||
//Fluent validation | ||
services.AddValidatorsFromAssembly(Assembly.GetExecutingAssembly()); | ||
//MediatR | ||
services.AddMediatR(cfg => | ||
{ | ||
cfg.RegisterServicesFromAssembly(Assembly.GetExecutingAssembly()); | ||
cfg.AddBehavior(typeof(IPipelineBehavior<,>), typeof(UnhandledExceptionBehaviour<,>)); | ||
cfg.AddBehavior(typeof(IPipelineBehavior<,>), typeof(ValidationBehaviour<,>)); | ||
cfg.AddBehavior(typeof(IPipelineBehavior<,>), typeof(PerformanceBehaviour<,>)); | ||
}); | ||
return services; | ||
} | ||
} |
Oops, something went wrong.
1f8037c
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
three-t – ./
three-t-succkarz.vercel.app
three-t-git-main-succkarz.vercel.app
three-t.vercel.app
www.threetee.online
threetee.online