Skip to content

Commit

Permalink
style: formatting codes with csharpier
Browse files Browse the repository at this point in the history
  • Loading branch information
Mehdi Hadeli committed Jul 27, 2024
1 parent 32be58b commit 8a491eb
Show file tree
Hide file tree
Showing 312 changed files with 2,547 additions and 2,010 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ namespace BuildingBlocks.Test.Factories;
public class CustomApplicationFactory<TEntryPoint> : WebApplicationFactory<TEntryPoint>
where TEntryPoint : class
{
public CustomApplicationFactory() : this(null)
{
}
public CustomApplicationFactory()
: this(null) { }

public CustomApplicationFactory(Action<IServiceCollection> testRegistrationServices = null)
{
Expand All @@ -38,15 +37,18 @@ protected override IHostBuilder CreateHostBuilder()
{
var builder = base.CreateHostBuilder();

builder = builder.UseSerilog((_, _, configuration) =>
{
if (OutputHelper is not null)
configuration.WriteTo.Xunit(OutputHelper);
builder = builder.UseSerilog(
(_, _, configuration) =>
{
if (OutputHelper is not null)
configuration.WriteTo.Xunit(OutputHelper);

configuration.MinimumLevel.Is(LogEventLevel.Information)
.MinimumLevel.Override("Microsoft.AspNetCore", LogEventLevel.Warning)
.WriteTo.Console();
});
configuration
.MinimumLevel.Is(LogEventLevel.Information)
.MinimumLevel.Override("Microsoft.AspNetCore", LogEventLevel.Warning)
.WriteTo.Console();
}
);

return builder;
}
Expand Down Expand Up @@ -77,16 +79,19 @@ protected override void ConfigureWebHost(IWebHostBuilder builder)
using var scope = sp.CreateScope();
var seeders = scope.ServiceProvider.GetServices<IDataSeeder>();

foreach (var seeder in seeders) seeder.SeedAllAsync().GetAwaiter().GetResult();
foreach (var seeder in seeders)
seeder.SeedAllAsync().GetAwaiter().GetResult();
});

builder.UseDefaultServiceProvider((env, c) =>
{
// Handling Captive Dependency Problem
// https://ankitvijay.net/2020/03/17/net-core-and-di-beware-of-captive-dependency/
// https://blog.ploeh.dk/2014/06/02/captive-dependency/
if (env.HostingEnvironment.IsEnvironment("test") || env.HostingEnvironment.IsDevelopment())
c.ValidateScopes = true;
});
builder.UseDefaultServiceProvider(
(env, c) =>
{
// Handling Captive Dependency Problem
// https://ankitvijay.net/2020/03/17/net-core-and-di-beware-of-captive-dependency/
// https://blog.ploeh.dk/2014/06/02/captive-dependency/
if (env.HostingEnvironment.IsEnvironment("test") || env.HostingEnvironment.IsDevelopment())
c.ValidateScopes = true;
}
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,14 @@ public Task<T> ExecuteDbContextAsync<T>(Func<TDbContext, IMediator, Task<T>> act
return ExecuteScopeAsync(sp => action(sp.GetService<TDbContext>(), sp.GetService<IMediator>()));
}

public Task InsertAsync<T>(params T[] entities) where T : class, IAggregate
public Task InsertAsync<T>(params T[] entities)
where T : class, IAggregate
{
return ExecuteDbContextAsync(db => db.GetCollection<T>().InsertManyAsync(entities));
}

public Task InsertAsync<TEntity>(TEntity entity) where TEntity : class, IAggregate
public Task InsertAsync<TEntity>(TEntity entity)
where TEntity : class, IAggregate
{
return ExecuteDbContextAsync(db => db.GetCollection<TEntity>().InsertOneAsync(entity));
}
Expand Down Expand Up @@ -117,8 +119,12 @@ public Task InsertAsync<TEntity, TEntity2, TEntity3>(TEntity entity, TEntity2 en
});
}

public Task InsertAsync<TEntity, TEntity2, TEntity3, TEntity4>(TEntity entity, TEntity2 entity2,
TEntity3 entity3, TEntity4 entity4)
public Task InsertAsync<TEntity, TEntity2, TEntity3, TEntity4>(
TEntity entity,
TEntity2 entity2,
TEntity3 entity3,
TEntity4 entity4
)
where TEntity : class, IAggregate
where TEntity2 : class, IAggregate
where TEntity3 : class, IAggregate
Expand All @@ -134,10 +140,10 @@ public Task InsertAsync<TEntity, TEntity2, TEntity3, TEntity4>(TEntity entity, T
});
}

public Task<T> FindAsync<T>(int id) where T : class, IAggregate<int>
public Task<T> FindAsync<T>(int id)
where T : class, IAggregate<int>
{
return ExecuteDbContextAsync(db =>
db.GetCollection<T>().AsQueryable().SingleOrDefaultAsync(x => x.Id == id));
return ExecuteDbContextAsync(db => db.GetCollection<T>().AsQueryable().SingleOrDefaultAsync(x => x.Id == id));
}

public override Task InitializeAsync()
Expand All @@ -161,16 +167,14 @@ public IntegrationTestFixture()
{
Factory = new CustomApplicationFactory<TEntryPoint>();
}

// protected readonly LaunchSettingsFixture LaunchSettings;

protected HttpClient Client => Factory.CreateClient();

public IHttpClientFactory HttpClientFactory => ServiceProvider.GetRequiredService<IHttpClientFactory>();

public IHttpClientFactory HttpClientFactory =>
ServiceProvider.GetRequiredService<IHttpClientFactory>();

public IHttpContextAccessor HttpContextAccessor =>
ServiceProvider.GetRequiredService<IHttpContextAccessor>();
public IHttpContextAccessor HttpContextAccessor => ServiceProvider.GetRequiredService<IHttpContextAccessor>();

public IServiceProvider ServiceProvider => Factory.Services;
public IConfiguration Configuration => Factory.Configuration;
Expand Down Expand Up @@ -234,7 +238,8 @@ public Task<TResponse> SendAsync<TResponse>(ICommand<TResponse> request, Cancell
});
}

public Task SendAsync<T>(T request, CancellationToken cancellationToken) where T : class, ICommand
public Task SendAsync<T>(T request, CancellationToken cancellationToken)
where T : class, ICommand
{
return ExecuteScopeAsync(sp =>
{
Expand All @@ -254,4 +259,4 @@ public Task<TResponse> QueryAsync<TResponse>(IQuery<TResponse> query, Cancellati
return mediator.Send(query, cancellationToken);
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ public void Dispose()
{
// ... clean up
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ public void Dispose()
{
_mongoRunner.Dispose();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ public void Dispose()
var client = new MongoClient(MongoOptions.ConnectionString);
client.DropDatabase(MongoOptions.DatabaseName);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ public abstract class WebApiTestFixture<TEntryPoint, TDbContext> : IntegrationTe
where TEntryPoint : class
where TDbContext : class, IMongoDbContext
{
private static readonly JsonSerializerOptions SerializerOptions = new()
{
PropertyNameCaseInsensitive = true,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
Converters = {new JsonStringEnumConverter()}
};
private static readonly JsonSerializerOptions SerializerOptions =
new()
{
PropertyNameCaseInsensitive = true,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
Converters = { new JsonStringEnumConverter() }
};

private string _route;

Expand All @@ -30,9 +31,11 @@ protected void SetPath(string route)
return;
}

if (route.StartsWith("/")) route = route.Substring(1, route.Length - 1);
if (route.StartsWith("/"))
route = route.Substring(1, route.Length - 1);

if (route.EndsWith("/")) route = route.Substring(0, route.Length - 1);
if (route.EndsWith("/"))
route = route.Substring(0, route.Length - 1);

_route = $"{route}/";
}
Expand Down Expand Up @@ -104,19 +107,19 @@ protected static async Task<T> ReadAsync<T>(HttpResponseMessage response)
return JsonSerializer.Deserialize<T>(await response.Content.ReadAsStringAsync(), SerializerOptions);
}

protected virtual void ConfigureServices(IServiceCollection services)
{
}
protected virtual void ConfigureServices(IServiceCollection services) { }
}

public abstract class WebApiTestFixture<TEntryPoint> : IntegrationTestFixture<TEntryPoint> where TEntryPoint : class
public abstract class WebApiTestFixture<TEntryPoint> : IntegrationTestFixture<TEntryPoint>
where TEntryPoint : class
{
private static readonly JsonSerializerOptions SerializerOptions = new()
{
PropertyNameCaseInsensitive = true,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
Converters = {new JsonStringEnumConverter()}
};
private static readonly JsonSerializerOptions SerializerOptions =
new()
{
PropertyNameCaseInsensitive = true,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
Converters = { new JsonStringEnumConverter() }
};

private string _route;

Expand All @@ -128,9 +131,11 @@ protected void SetPath(string route)
return;
}

if (route.StartsWith("/")) route = route.Substring(1, route.Length - 1);
if (route.StartsWith("/"))
route = route.Substring(1, route.Length - 1);

if (route.EndsWith("/")) route = route.Substring(0, route.Length - 1);
if (route.EndsWith("/"))
route = route.Substring(0, route.Length - 1);

_route = $"{route}/";
}
Expand Down Expand Up @@ -202,7 +207,5 @@ protected static async Task<T> ReadAsync<T>(HttpResponseMessage response)
return JsonSerializer.Deserialize<T>(await response.Content.ReadAsStringAsync(), SerializerOptions);
}

protected virtual void ConfigureServices(IServiceCollection services)
{
}
}
protected virtual void ConfigureServices(IServiceCollection services) { }
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,13 @@ public static TSettings GetOptions<TSettings>(string section, string settingsFil
settingsFileName ??= "appsettings.test.json";
var configuration = new TSettings();

GetConfigurationRoot(settingsFileName)
.GetSection(section)
.Bind(configuration);
GetConfigurationRoot(settingsFileName).GetSection(section).Bind(configuration);

return configuration;
}

private static IConfigurationRoot GetConfigurationRoot(string settingsFileName)
{
return new ConfigurationBuilder()
.AddJsonFile(settingsFileName, true)
.AddEnvironmentVariables()
.Build();
return new ConfigurationBuilder().AddJsonFile(settingsFileName, true).AddEnvironmentVariables().Build();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ public Task SeedAllAsync()
{
return Task.CompletedTask;
}
}
}
2 changes: 1 addition & 1 deletion src/BuildingBlocks/BulidingBlocks/Caching/CacheKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ public static string With(Type ownerType, params string[] keys)
{
return With($"{ownerType.GetCacheKey()}:{string.Join("-", keys)}");
}
}
}
33 changes: 21 additions & 12 deletions src/BuildingBlocks/BulidingBlocks/Caching/CachingBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,22 @@ public class CachingBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest,
private readonly ILogger<CachingBehavior<TRequest, TResponse>> _logger;
private readonly int defaultCacheExpirationInHours = 1;


public CachingBehavior(IEasyCachingProviderFactory cachingFactory,
public CachingBehavior(
IEasyCachingProviderFactory cachingFactory,
ILogger<CachingBehavior<TRequest, TResponse>> logger,
IEnumerable<ICachePolicy<TRequest, TResponse>> cachePolicies)
IEnumerable<ICachePolicy<TRequest, TResponse>> cachePolicies
)
{
_logger = logger;
_cachingProvider = cachingFactory.GetCachingProvider("mem");
_cachePolicies = cachePolicies;
}

public async Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> next, CancellationToken
cancellationToken)
public async Task<TResponse> Handle(
TRequest request,
RequestHandlerDelegate<TResponse> next,
CancellationToken cancellationToken
)
{
var cachePolicy = _cachePolicies.FirstOrDefault();
if (cachePolicy == null)
Expand All @@ -40,20 +44,25 @@ public async Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TRe
var cachedResponse = await _cachingProvider.GetAsync<TResponse>(cacheKey);
if (cachedResponse.Value != null)
{
_logger.LogDebug("Response retrieved {TRequest} from cache. CacheKey: {CacheKey}",
typeof(TRequest).FullName, cacheKey);
_logger.LogDebug(
"Response retrieved {TRequest} from cache. CacheKey: {CacheKey}",
typeof(TRequest).FullName,
cacheKey
);
return cachedResponse.Value;
}

var response = await next();

var time = cachePolicy.AbsoluteExpirationRelativeToNow ??
DateTime.Now.AddHours(defaultCacheExpirationInHours);
var time = cachePolicy.AbsoluteExpirationRelativeToNow ?? DateTime.Now.AddHours(defaultCacheExpirationInHours);
await _cachingProvider.SetAsync(cacheKey, response, time.TimeOfDay);

_logger.LogDebug("Caching response for {TRequest} with cache key: {CacheKey}", typeof(TRequest).FullName,
cacheKey);
_logger.LogDebug(
"Caching response for {TRequest} with cache key: {CacheKey}",
typeof(TRequest).FullName,
cacheKey
);

return response;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ public ExpirationOptions(DateTimeOffset absoluteExpiration)
}

public DateTimeOffset AbsoluteExpiration { get; }
}
}
Loading

0 comments on commit 8a491eb

Please sign in to comment.