Skip to content

Commit

Permalink
chore: apply dotnet format
Browse files Browse the repository at this point in the history
  • Loading branch information
hamed-shirbandi committed Oct 30, 2023
1 parent 040cb09 commit 10355db
Show file tree
Hide file tree
Showing 643 changed files with 16,827 additions and 17,503 deletions.
8 changes: 7 additions & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@ dotnet_diagnostic.IDE0058.severity = silent # Expression value is never used
dotnet_diagnostic.IDE0072.severity = silent # missing cases to "=>" switch expression
dotnet_diagnostic.IDE0090.severity = silent # Use implicit new
dotnet_diagnostic.IDE0270.severity = silent # Simplify null check
dotnet_diagnostic.IDE0005.severity = silent # Remove unnecessary using directives (VS new update has a problem with IDE0005. We are using CS8019 instead)
dotnet_diagnostic.IDE0005.severity = error # Remove unnecessary using directives (using CS8019 instead)


dotnet_diagnostic.IDE0060.severity = silent # Remove unused parameter
dotnet_diagnostic.IDE1006.severity = error # Naming rule violation
dotnet_diagnostic.IDE0130.severity = silent # Namespace does not match folder structure (using dotnet_style_namespace_match_folder instead)


# https://github.com/dotnet/aspnetcore/issues/47912
dotnet_diagnostic.IDE0005.severity = none # bugfix
Expand Down
4 changes: 4 additions & 0 deletions .nuke/build.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@
"enum": [
"Clean",
"Compile",
"Lint",
"LintCheck",
"LogInformation",
"Preparation",
"Restore",
Expand All @@ -94,6 +96,8 @@
"enum": [
"Clean",
"Compile",
"Lint",
"LintCheck",
"LogInformation",
"Preparation",
"Restore",
Expand Down
69 changes: 34 additions & 35 deletions src/1-BuildingBlocks/Application/Behaviors/BehaviorExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,45 @@
using System;
using TaskoMask.BuildingBlocks.Domain.Events;

namespace TaskoMask.BuildingBlocks.Application.Behaviors
namespace TaskoMask.BuildingBlocks.Application.Behaviors;

public static class BehaviorExtensions
{
public static class BehaviorExtensions
/// <summary>
///
/// </summary>
public static void AddApplicationBehaviors(this IServiceCollection services, Type validatorAssemblyMarkerType)
{
/// <summary>
///
/// </summary>
public static void AddApplicationBehaviors(this IServiceCollection services, Type validatorAssemblyMarkerType)
{
services.AddValidationBehaviour(validatorAssemblyMarkerType);
services.AddCachingBehavior();
services.AddEventStoringBehavior();
}
services.AddValidationBehaviour(validatorAssemblyMarkerType);
services.AddCachingBehavior();
services.AddEventStoringBehavior();
}

/// <summary>
///
/// </summary>
public static void AddValidationBehaviour(this IServiceCollection services, Type validatorAssemblyMarkerType)
{
//Load all fluent validation classes to be used in ValidationBehaviour
services.AddValidatorsFromAssembly(validatorAssemblyMarkerType.Assembly);
/// <summary>
///
/// </summary>
public static void AddValidationBehaviour(this IServiceCollection services, Type validatorAssemblyMarkerType)
{
//Load all fluent validation classes to be used in ValidationBehaviour
services.AddValidatorsFromAssembly(validatorAssemblyMarkerType.Assembly);

services.AddScoped(typeof(IPipelineBehavior<,>), typeof(ValidationBehaviour<,>));
}
services.AddScoped(typeof(IPipelineBehavior<,>), typeof(ValidationBehaviour<,>));
}

/// <summary>
///
/// </summary>
public static void AddEventStoringBehavior(this IServiceCollection services)
{
services.AddScoped<INotificationHandler<DomainEvent>, EventStoringBehavior>();
}
/// <summary>
///
/// </summary>
public static void AddEventStoringBehavior(this IServiceCollection services)
{
services.AddScoped<INotificationHandler<DomainEvent>, EventStoringBehavior>();
}

/// <summary>
///
/// </summary>
public static void AddCachingBehavior(this IServiceCollection services)
{
services.AddEasyCaching(option => option.UseInMemory());
services.AddScoped(typeof(IPipelineBehavior<,>), typeof(CachingBehavior<,>));
}
/// <summary>
///
/// </summary>
public static void AddCachingBehavior(this IServiceCollection services)
{
services.AddEasyCaching(option => option.UseInMemory());
services.AddScoped(typeof(IPipelineBehavior<,>), typeof(CachingBehavior<,>));
}
}
125 changes: 62 additions & 63 deletions src/1-BuildingBlocks/Application/Behaviors/CachingBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,92 +8,91 @@
using System.Threading.Tasks;
using TaskoMask.BuildingBlocks.Application.Queries;

namespace TaskoMask.BuildingBlocks.Application.Behaviors
namespace TaskoMask.BuildingBlocks.Application.Behaviors;

/// <summary>
/// Caching response for queries that are mareked by ICacheableQuery
/// </summary>
public class CachingBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
where TRequest : BaseQuery<TResponse>
{
/// <summary>
/// Caching response for queries that are mareked by ICacheableQuery
/// </summary>
public class CachingBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
where TRequest : BaseQuery<TResponse>
{
#region Fields
#region Fields

private readonly IEasyCachingProvider _cachingProvider;
private readonly IConfiguration _configuration;
private readonly IEasyCachingProvider _cachingProvider;
private readonly IConfiguration _configuration;

#endregion
#endregion

#region Ctors
#region Ctors


public CachingBehavior(IEasyCachingProvider cachingProvider, IConfiguration configuration)
{
_cachingProvider = cachingProvider;
_configuration = configuration;
}
public CachingBehavior(IEasyCachingProvider cachingProvider, IConfiguration configuration)
{
_cachingProvider = cachingProvider;
_configuration = configuration;
}

#endregion
#endregion

#region Public Methods
#region Public Methods

/// <summary>
///
/// </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();

//ignore caching if it is not enabled globaly from configurations
var configurationCachingEnabled = bool.Parse(_configuration["Caching:Enabled"]);
if (!configurationCachingEnabled)
return await next();
/// <summary>
///
/// </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();

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

var cacheKey = GenerateKeyFromRequest(request);
var cachedResponse = await _cachingProvider.GetAsync<TResponse>(cacheKey);
//ignore caching for this request if caching is not enabled
if (!cacheableQuery.CachingIsEnabled())
return await next();

if (cachedResponse.Value != null)
return cachedResponse.Value;
var cacheKey = GenerateKeyFromRequest(request);
var cachedResponse = await _cachingProvider.GetAsync<TResponse>(cacheKey);

var response = await next();
if (cachedResponse.Value != null)
return cachedResponse.Value;

var cacheTimeInMinutes = int.Parse(_configuration["Caching:CacheTimeInMinutes"]);
var expirationTime = DateTime.Now.AddMinutes(cacheTimeInMinutes);
await _cachingProvider.SetAsync(cacheKey, response, expirationTime.TimeOfDay);
var response = await next();

return response;
}
var cacheTimeInMinutes = int.Parse(_configuration["Caching:CacheTimeInMinutes"]);
var expirationTime = DateTime.Now.AddMinutes(cacheTimeInMinutes);
await _cachingProvider.SetAsync(cacheKey, response, expirationTime.TimeOfDay);

#endregion
return response;
}

#region Private Methods
#endregion

#region Private Methods

/// <summary>
///
/// </summary>
private string GenerateKeyFromRequest(TRequest request)
{
var properties = new List<PropertyInfo>(request.GetType().GetProperties());
var key = request.GetType().Name;

foreach (PropertyInfo property in properties)
{
object propValue = property.GetValue(request, property.GetIndexParameters());
/// <summary>
///
/// </summary>
private string GenerateKeyFromRequest(TRequest request)
{
var properties = new List<PropertyInfo>(request.GetType().GetProperties());
var key = request.GetType().Name;

string name = property.Name;
string value = propValue != null ? propValue.ToString() : "";
key += $"_{name}:{value}";
}
foreach (PropertyInfo property in properties)
{
object propValue = property.GetValue(request, property.GetIndexParameters());

return key;
string name = property.Name;
string value = propValue != null ? propValue.ToString() : "";
key += $"_{name}:{value}";
}

#endregion
return key;
}

#endregion
}
63 changes: 28 additions & 35 deletions src/1-BuildingBlocks/Application/Behaviors/EventStoringBehavior.cs
Original file line number Diff line number Diff line change
@@ -1,56 +1,49 @@
using MediatR;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using TaskoMask.BuildingBlocks.Application.Commands;
using TaskoMask.BuildingBlocks.Domain.Events;

namespace TaskoMask.BuildingBlocks.Application.Behaviors
namespace TaskoMask.BuildingBlocks.Application.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.
/// </summary>
public class EventStoringBehavior : INotificationHandler<DomainEvent>
{
/// <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.
/// </summary>
public class EventStoringBehavior : INotificationHandler<DomainEvent>
{
#region Fields
#region Fields

private readonly IEventStoreService _eventStore;
private readonly IEventStoreService _eventStore;

#endregion
#endregion

#region Ctors
#region Ctors


public EventStoringBehavior(IEventStoreService eventStore)
{
_eventStore = eventStore;
}
public EventStoringBehavior(IEventStoreService eventStore)
{
_eventStore = eventStore;
}

#endregion
#endregion

#region Public Methods
#region Public Methods


/// <summary>
///
/// </summary>
public async Task Handle(DomainEvent request, CancellationToken cancellationToken)
{
await _eventStore.SaveAsync(request);
}
/// <summary>
///
/// </summary>
public async Task Handle(DomainEvent request, CancellationToken cancellationToken)
{
await _eventStore.SaveAsync(request);
}

#endregion
#endregion

#region Private Methods
#region Private Methods



#endregion
}
#endregion
}
Loading

0 comments on commit 10355db

Please sign in to comment.