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

Global Code Refactoring #159

Merged
merged 12 commits into from
Sep 27, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using MediatR;
using Microsoft.Extensions.DependencyInjection;
using System;
using TaskoMask.BuildingBlocks.Domain.Models;
using TaskoMask.BuildingBlocks.Domain.Events;

namespace TaskoMask.BuildingBlocks.Application.Behaviors
{
Expand All @@ -27,7 +27,7 @@ public static void AddApplicationBehaviors(this IServiceCollection services, Typ
/// </summary>
public static void AddValidationBehaviour(this IServiceCollection services, Type validatorAssemblyMarkerType)
{
//Load all fluent validation to use in ValidationBehaviour
//Load all fluent validation classes to be used in ValidationBehaviour
services.AddValidatorsFromAssembly(validatorAssemblyMarkerType.Assembly);

services.AddScoped(typeof(IPipelineBehavior<,>), typeof(ValidationBehaviour<,>));
Expand Down
11 changes: 7 additions & 4 deletions src/1-BuildingBlocks/Application/Behaviors/CachingBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace TaskoMask.BuildingBlocks.Application.Behaviors
{

/// <summary>
/// Caching response of queries mareked with ICacheableQuery
/// Caching response for queries that are mareked by ICacheableQuery
/// </summary>
public class CachingBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse> where TRequest : BaseQuery<TResponse>
{
Expand Down Expand Up @@ -42,14 +42,17 @@ public CachingBehavior(IEasyCachingProvider cachingProvider, IConfiguration conf
/// </summary>
public async Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate<TResponse> next)
{
// ignore command requests
if (request is not ICacheableQuery cacheableQuery)
return await next();

var configurationCacheEnabled = bool.Parse(_configuration["Caching:Enabled"]);
if (!configurationCacheEnabled)
//ignore caching if it is not enabled globaly from configurations
var configurationCachingEnabled = bool.Parse(_configuration["Caching:Enabled"]);
if (!configurationCachingEnabled)
return await next();

if (!cacheableQuery.EnableCache)
//ignore caching for this request if caching is not enabled
if (!cacheableQuery.CachingIsEnabled())
return await next();


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,15 @@
using System.Threading;
using System.Threading.Tasks;
using TaskoMask.BuildingBlocks.Application.Commands;
using TaskoMask.BuildingBlocks.Domain.Models;
using TaskoMask.BuildingBlocks.Domain.Services;
using TaskoMask.BuildingBlocks.Domain.Events;

namespace TaskoMask.BuildingBlocks.Application.Behaviors
{

/// <summary>
/// Each command must have at least one event to save changes in event store
/// 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 another handlers to do another things like sending an email or update some other entities
/// However events can have other handlers to do other things like sending an email or update some other entities, etc.
/// </summary>
public class EventStoringBehavior : INotificationHandler<DomainEvent>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
namespace TaskoMask.BuildingBlocks.Application.Behaviors
{
/// <summary>
/// automatic command validation by check annotation and fluent validation (if exist)
/// Automatic validation by checking data annotation and fluent validations (if any)
/// </summary>
public class ValidationBehaviour<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse> where TRequest : BaseCommand
{
Expand Down
7 changes: 4 additions & 3 deletions src/1-BuildingBlocks/Application/Bus/IInMemoryBus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@
using TaskoMask.BuildingBlocks.Application.Commands;
using TaskoMask.BuildingBlocks.Application.Queries;
using TaskoMask.BuildingBlocks.Contracts.Helpers;
using TaskoMask.BuildingBlocks.Domain.Models;
using TaskoMask.BuildingBlocks.Domain.Events;

namespace TaskoMask.BuildingBlocks.Application.Bus
{
/// <summary>
/// It is used as a mediator to send and handle requests inside a single service
/// It is used as a mediator to send and handle requests inside a service (in-process)
/// </summary>
public interface IInMemoryBus
{
Task<Result<CommandResult>> SendCommand<TCommand>(TCommand cmd) where TCommand : BaseCommand;
Task<Result<CommandResult>> SendCommand<TCommand>(TCommand cmd)
where TCommand : BaseCommand;
Task<Result<TQueryResult>> SendQuery<TQueryResult>(BaseQuery<TQueryResult> query);
Task PublishEvent(DomainEvent @event);
}
Expand Down
6 changes: 3 additions & 3 deletions src/1-BuildingBlocks/Application/Bus/IMessageBus.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using System.Threading.Tasks;
using TaskoMask.BuildingBlocks.Contracts.Models;
using TaskoMask.BuildingBlocks.Domain.Models;
using TaskoMask.BuildingBlocks.Contracts.Events;


namespace TaskoMask.BuildingBlocks.Application.Bus
{
/// <summary>
/// It is used as a message broker to enable microservices communicating each other
/// It is used as a message broker to enable microservices communicating each other (out-process)
/// </summary>
public interface IMessageBus
{
Expand Down
6 changes: 2 additions & 4 deletions src/1-BuildingBlocks/Application/Commands/BaseCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,5 @@

namespace TaskoMask.BuildingBlocks.Application.Commands
{
public abstract class BaseCommand : InternalCommand<CommandResult>
{
}
}
public abstract class BaseCommand : IRequest<CommandResult> { }
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System.Threading.Tasks;
using System.Collections.Generic;
using TaskoMask.BuildingBlocks.Application.Bus;
using TaskoMask.BuildingBlocks.Domain.Models;
using TaskoMask.BuildingBlocks.Contracts.Models;
using TaskoMask.BuildingBlocks.Contracts.Events;
using TaskoMask.BuildingBlocks.Domain.Events;

namespace TaskoMask.BuildingBlocks.Application.Commands
{
Expand Down
8 changes: 0 additions & 8 deletions src/1-BuildingBlocks/Application/Commands/InternalCommand.cs

This file was deleted.

17 changes: 13 additions & 4 deletions src/1-BuildingBlocks/Application/Queries/BaseQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,27 @@ namespace TaskoMask.BuildingBlocks.Application.Queries
{

/// <summary>
/// Mark BaseQuery with ICacheableQuery to catche the queries by CachingBehavior.
/// If you need to cache a query just set the EnableCache to true beafore sending the query to bus
/// Mark BaseQuery with ICacheableQuery to enable catching for queries by CachingBehavior.
/// </summary>
public abstract class BaseQuery<TQueryResult> : ICacheableQuery, IRequest<TQueryResult>
{
protected BaseQuery()
{
//By default cache is disabled
//Ching is disabled by default
EnableCache = false;
}


public bool EnableCache { get; set; }
public bool EnableCache { get; private set; }

public bool CachingIsEnabled()
{
return EnableCache ;
}

public void EnableCaching()
{
EnableCache = true;
}
}
}
7 changes: 4 additions & 3 deletions src/1-BuildingBlocks/Application/Queries/ICacheableQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ namespace TaskoMask.BuildingBlocks.Application.Queries
public interface ICacheableQuery
{
/// <summary>
/// for enable caching set it to true beafor sending the query to bus like below:
/// To enable caching for a query, call EnableCaching() beafor sending the query:
/// var query=new GetSomeQuery()
/// query.EnableCache = false;
/// query.EnableCaching();
/// _inMemoryBus.SendQuery(query)
/// </summary>
bool EnableCache { get; set; }
void EnableCaching();
bool CachingIsEnabled();
}
}
23 changes: 0 additions & 23 deletions src/1-BuildingBlocks/Contracts/Enums/ContractsEnums.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,6 @@
namespace TaskoMask.BuildingBlocks.Contracts.Enums
{


/// <summary>
///
/// </summary>
public enum BoardMemberAccessLevel
{
[Display(Name = nameof(ContractsMetadata.BoardMemberAccessLevel_Reader), ResourceType = typeof(ContractsMetadata))]
Reader = 0,
[Display(Name = nameof(ContractsMetadata.BoardMemberAccessLevel_Writer), ResourceType = typeof(ContractsMetadata))]
Writer = 1,
}



/// <summary>
///
/// </summary>
Expand All @@ -35,13 +21,4 @@ public enum BoardCardType
}


/// <summary>
///
/// </summary>
public enum UserType
{
Owner = 0,
Operator = 1
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@

using TaskoMask.BuildingBlocks.Contracts.Enums;
using TaskoMask.BuildingBlocks.Contracts.Models;

namespace TaskoMask.BuildingBlocks.Contracts.Events
{
Expand Down
6 changes: 6 additions & 0 deletions src/1-BuildingBlocks/Contracts/Events/IIntegrationEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace TaskoMask.BuildingBlocks.Contracts.Events
{
public interface IntegrationEvent
{
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@

using TaskoMask.BuildingBlocks.Contracts.Models;

namespace TaskoMask.BuildingBlocks.Contracts.Events
namespace TaskoMask.BuildingBlocks.Contracts.Events
{
public record UserRegistered(string Email) : IntegrationEvent;
public record UserUpdated(string Email) : IntegrationEvent;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using TaskoMask.BuildingBlocks.Contracts.Models;

namespace TaskoMask.BuildingBlocks.Contracts.Events
namespace TaskoMask.BuildingBlocks.Contracts.Events
{
public record OwnerRegistered(string Id, string Email, string Password) : IntegrationEvent;
public record OwnerRegisterationCompleted(string Id, string Email, string DisplayName) : IntegrationEvent;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using TaskoMask.BuildingBlocks.Contracts.Models;

namespace TaskoMask.BuildingBlocks.Contracts.Events
namespace TaskoMask.BuildingBlocks.Contracts.Events
{
public record TaskAdded(string Id, string Title, string Description, string CardId, string BoardId) : IntegrationEvent;
public record TaskDeleted(string Id) : IntegrationEvent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,5 @@ public static bool Validate<TObject>(this TObject obj, out ICollection<Validatio
return Validator.TryValidateObject(obj, new ValidationContext(obj), results, true);
}



/// <summary>
///
/// </summary>
public static object GetDisplayName<TObject>(this TObject obj) where TObject : class
{
return typeof(TObject).CustomAttributes.Any() ?
typeof(TObject).CustomAttributes.First().ConstructorArguments.First().Value :
nameof(TObject);
}


}
}
38 changes: 0 additions & 38 deletions src/1-BuildingBlocks/Contracts/Extensions/UtilityExtensions.cs

This file was deleted.

Loading
Loading