Skip to content

Commit

Permalink
refactor: building blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
hamed-shirbandi committed Nov 22, 2024
1 parent 0b059dc commit 0078411
Show file tree
Hide file tree
Showing 99 changed files with 298 additions and 397 deletions.

This file was deleted.

31 changes: 0 additions & 31 deletions src/1-BuildingBlocks/Application/Notifications/Notification.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using TaskoMask.BuildingBlocks.Contracts.Models;

namespace TaskoMask.BuildingBlocks.Contracts.Services;
namespace TaskoMask.BuildingBlocks.Application.Services;

public interface IAuthenticatedUserService
{
Expand Down
13 changes: 13 additions & 0 deletions src/1-BuildingBlocks/Application/Services/INotificationService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System.Collections.Generic;

namespace TaskoMask.BuildingBlocks.Application.Services;

public interface INotificationService
{
void Add(string notification);
void AddRange(List<string> notifications);
List<string> GetList();
List<string> GetListAndReset();
bool HasAny();
void Reset();
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.Threading.Tasks;
using TaskoMask.BuildingBlocks.Domain.Entities;

namespace TaskoMask.BuildingBlocks.Domain.Data;
namespace TaskoMask.BuildingBlocks.Domain.Services;

public interface IBaseAggregateRepository<TEntity> : IBaseRepository<TEntity>
where TEntity : AggregateRoot
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System.Threading.Tasks;
using TaskoMask.BuildingBlocks.Domain.Entities;

namespace TaskoMask.BuildingBlocks.Domain.Data;
namespace TaskoMask.BuildingBlocks.Domain.Services;

public interface IBaseRepository<TEntity> : IDisposable
where TEntity : Entity
Expand Down
11 changes: 11 additions & 0 deletions src/1-BuildingBlocks/Domain/Services/ISpecification.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace TaskoMask.BuildingBlocks.Domain.Services;

public interface ISpecification<TEntity>
{
/// <summary>
/// Checks if a given entity satisfies the specification's criteria.
/// </summary>
/// <param name="entity">The entity to evaluate.</param>
/// <returns>True if the entity satisfies the specification; otherwise, false.</returns>
bool IsSatisfiedBy(TEntity entity);
}
6 changes: 0 additions & 6 deletions src/1-BuildingBlocks/Domain/Specifications/ISpecification.cs

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,20 @@
using System.Threading.Tasks;
using MediatR;
using TaskoMask.BuildingBlocks.Domain.Events;
using TaskoMask.BuildingBlocks.Infrastructure.Services.EventStoring;

namespace TaskoMask.BuildingBlocks.Infrastructure.Behaviors;

/// <summary>
/// Each command must have at least one event to save its changes in event store
/// So this notification handler act as a behavior and makes it easy to store events without repeating the creation of event handler
/// However events can have other handlers to do other things like sending an email or update some other entities, etc.
/// This behavior ensures that every command's resulting domain events are saved in the event store.
///
/// The EventStoringBehavior acts as a notification handler for domain events, abstracting the logic for
/// persisting events. This prevents the need to repeatedly implement event storage logic in multiple places.
///
/// Additionally, domain events can have other handlers for various purposes, such as:
/// - Sending notifications (e.g., emails)
/// - Updating other entities or performing additional business logic
/// This behavior specifically focuses on saving events to the event store.
/// </summary>
public class EventStoringBehavior : INotificationHandler<DomainEvent>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using FluentValidation;
using MediatR;
using TaskoMask.BuildingBlocks.Application.Commands;
using TaskoMask.BuildingBlocks.Application.Notifications;
using TaskoMask.BuildingBlocks.Application.Services;
using TaskoMask.BuildingBlocks.Contracts.Extensions;

namespace TaskoMask.BuildingBlocks.Infrastructure.Behaviors;
Expand All @@ -19,13 +19,13 @@ public class ValidationBehaviour<TRequest, TResponse> : IPipelineBehavior<TReque
#region Fields

private readonly IEnumerable<IValidator<TRequest>> _validators;
private readonly INotificationHandler _notifications;
private readonly INotificationService _notifications;

#endregion

#region Ctors

public ValidationBehaviour(IEnumerable<IValidator<TRequest>> validators, INotificationHandler notifications)
public ValidationBehaviour(IEnumerable<IValidator<TRequest>> validators, INotificationService notifications)
{
_validators = validators;
_notifications = notifications;
Expand Down Expand Up @@ -72,7 +72,7 @@ private bool ValidateDataAnnotationValidation(BaseCommand request)

// add data annotation errors to notifications
foreach (var result in results)
NotifyValidationError(request, result.ErrorMessage);
NotifyValidationError(result.ErrorMessage);

return false;
}
Expand All @@ -90,17 +90,17 @@ private async Task<bool> ValidateFluentValidation(TRequest request, Cancellation

// add data annotation errors to notifications
foreach (var failure in failures)
NotifyValidationError(request, failure.ErrorMessage);
NotifyValidationError(failure.ErrorMessage);

return false;
}

/// <summary>
/// add error to notifications
/// </summary>
protected void NotifyValidationError(BaseCommand request, string error)
protected void NotifyValidationError(string error)
{
_notifications.Add(request.GetType().Name, error);
_notifications.Add(error);
}

#endregion
Expand Down
32 changes: 0 additions & 32 deletions src/1-BuildingBlocks/Infrastructure/Bus/BusExtensions.cs

This file was deleted.

10 changes: 5 additions & 5 deletions src/1-BuildingBlocks/Infrastructure/Bus/MediatRDispatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
using MediatR;
using TaskoMask.BuildingBlocks.Application.Bus;
using TaskoMask.BuildingBlocks.Application.Commands;
using TaskoMask.BuildingBlocks.Application.Notifications;
using TaskoMask.BuildingBlocks.Application.Queries;
using TaskoMask.BuildingBlocks.Application.Services;
using TaskoMask.BuildingBlocks.Contracts.Helpers;
using TaskoMask.BuildingBlocks.Domain.Events;

Expand All @@ -17,13 +17,13 @@ public class MediatRDispatcher : IRequestDispatcher
#region Fields

private readonly IMediator _mediator;
private readonly INotificationHandler _notifications;
private readonly INotificationService _notifications;

#endregion

#region Ctors

public MediatRDispatcher(IMediator mediator, INotificationHandler notifications)
public MediatRDispatcher(IMediator mediator, INotificationService notifications)
{
_mediator = mediator;
_notifications = notifications;
Expand All @@ -47,7 +47,7 @@ public async Task<Result<CommandResult>> SendCommand<TCommand>(TCommand cmd)
var result = await _mediator.Send(cmd);

//get notification errors
var errors = _notifications.GetErrors();
var errors = _notifications.GetList();

//result is null when throw application or domain exception
if (result == null)
Expand All @@ -67,7 +67,7 @@ public async Task<Result<TQueryResult>> SendQuery<TQueryResult>(BaseQuery<TQuery
{
var result = await _mediator.Send(query);
if (_notifications.HasAny())
return Result.Failure<TQueryResult>(_notifications.GetErrors());
return Result.Failure<TQueryResult>(_notifications.GetList());

return Result.Success(result);
}
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System.Threading.Tasks;
using MediatR.Pipeline;
using Microsoft.Extensions.Logging;
using TaskoMask.BuildingBlocks.Application.Notifications;
using TaskoMask.BuildingBlocks.Application.Services;
using TaskoMask.BuildingBlocks.Contracts.Exceptions;

namespace TaskoMask.BuildingBlocks.Infrastructure.Exceptions;
Expand All @@ -17,15 +17,15 @@ public class ManagedExceptionHandler<TRequest, TResponse, TException> : IRequest
#region Fields


private readonly INotificationHandler _notifications;
private readonly INotificationService _notifications;
private readonly ILogger<ManagedExceptionHandler<TRequest, TResponse, TException>> _logger;

#endregion

#region Ctors


public ManagedExceptionHandler(INotificationHandler notifications, ILogger<ManagedExceptionHandler<TRequest, TResponse, TException>> logger)
public ManagedExceptionHandler(INotificationService notifications, ILogger<ManagedExceptionHandler<TRequest, TResponse, TException>> logger)
{
_notifications = notifications;
_logger = logger;
Expand All @@ -42,11 +42,9 @@ public ManagedExceptionHandler(INotificationHandler notifications, ILogger<Manag
/// </summary>
public Task Handle(TRequest request, TException exception, RequestExceptionHandlerState<TResponse> state, CancellationToken cancellationToken)
{
var exceptionType = exception.GetType();

//notify exception message if any
if (!string.IsNullOrEmpty(exception.Message))
_notifications.Add(exceptionType.Name, exception.Message);
_notifications.Add(exception.Message);

_logger.LogWarning(exception, $"request : {JsonSerializer.Serialize(request)}");

Expand Down
Loading

0 comments on commit 0078411

Please sign in to comment.