Skip to content

Commit

Permalink
Merge pull request #230 from Avanade/feature/structV8
Browse files Browse the repository at this point in the history
feat: Swap LiquidConfiguration for IOptions.
  • Loading branch information
lucianareginalino authored Jun 17, 2024
2 parents 916c58d + 73d0f87 commit a5c4af4
Show file tree
Hide file tree
Showing 17 changed files with 64 additions and 178 deletions.
9 changes: 5 additions & 4 deletions src/Liquid.Core/Decorators/LiquidContextDecorator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Liquid.Core.Exceptions;
using Liquid.Core.Interfaces;
using Liquid.Core.Settings;
using Microsoft.Extensions.Options;
using System;
using System.Globalization;
using System.Threading;
Expand All @@ -17,15 +18,15 @@ public class LiquidContextDecorator<TEntity> : ILiquidWorker<TEntity>
{
private readonly ILiquidWorker<TEntity> _inner;
private readonly ILiquidContext _context;
private readonly ILiquidConfiguration<ScopedContextSettings> _options;
private readonly IOptions<ScopedContextSettings> _options;

/// <summary>
/// Initialize a new instance of <see cref="LiquidContextDecorator{TEntity}"/>
/// </summary>
/// <param name="inner">Decorated service.</param>
/// <param name="context">Scoped Context service.</param>
/// <param name="options">Scoped context keys set.</param>
public LiquidContextDecorator(ILiquidWorker<TEntity> inner, ILiquidContext context, ILiquidConfiguration<ScopedContextSettings> options)
public LiquidContextDecorator(ILiquidWorker<TEntity> inner, ILiquidContext context, IOptions<ScopedContextSettings> options)
{
_inner = inner ?? throw new ArgumentNullException(nameof(inner));
_context = context ?? throw new ArgumentNullException(nameof(context));
Expand All @@ -37,7 +38,7 @@ public async Task ProcessMessageAsync(ConsumerMessageEventArgs<TEntity> args, Ca
{
object value = default;

foreach (var key in _options.Settings.Keys)
foreach (var key in _options.Value.Keys)
{
args.Headers?.TryGetValue(key.KeyName, out value);

Expand All @@ -47,7 +48,7 @@ public async Task ProcessMessageAsync(ConsumerMessageEventArgs<TEntity> args, Ca
_context.Upsert(key.KeyName, value);
}

if (_options.Settings.Culture)
if (_options.Value.Culture)
{
_context.Upsert("culture", CultureInfo.CurrentCulture.Name);
}
Expand Down
9 changes: 5 additions & 4 deletions src/Liquid.Core/Decorators/LiquidCultureDecorator.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Liquid.Core.Entities;
using Liquid.Core.Interfaces;
using Liquid.Core.Settings;
using Microsoft.Extensions.Options;
using System;
using System.Globalization;
using System.Threading;
Expand All @@ -15,15 +16,15 @@ namespace Liquid.Core.Decorators
public class LiquidCultureDecorator<TEntity> : ILiquidWorker<TEntity>
{
private const string _culture = "culture";
private readonly ILiquidConfiguration<CultureSettings> _options;
private readonly IOptions<CultureSettings> _options;
private readonly ILiquidWorker<TEntity> _inner;

/// <summary>
/// Initialize a new instance of <see cref="LiquidCultureDecorator{TEntity}"/>
/// </summary>
/// <param name="inner">Decorated service.</param>
/// <param name="options">Default culture configuration.</param>
public LiquidCultureDecorator(ILiquidWorker<TEntity> inner, ILiquidConfiguration<CultureSettings> options)
public LiquidCultureDecorator(ILiquidWorker<TEntity> inner, IOptions<CultureSettings> options)
{
_inner = inner ?? throw new ArgumentNullException(nameof(inner));
_options = options ?? throw new ArgumentNullException(nameof(options));
Expand All @@ -36,9 +37,9 @@ public async Task ProcessMessageAsync(ConsumerMessageEventArgs<TEntity> args, Ca

args.Headers?.TryGetValue(_culture, out cultureCode);

if (cultureCode is null && !string.IsNullOrEmpty(_options.Settings.DefaultCulture))
if (cultureCode is null && !string.IsNullOrEmpty(_options.Value.DefaultCulture))
{
cultureCode = _options.Settings.DefaultCulture;
cultureCode = _options.Value.DefaultCulture;
}

if (cultureCode != null)
Expand Down
7 changes: 4 additions & 3 deletions src/Liquid.Core/Decorators/LiquidScopedLoggingDecorator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Liquid.Core.Interfaces;
using Liquid.Core.Settings;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Threading;
Expand All @@ -17,7 +18,7 @@ namespace Liquid.Core.Decorators
public class LiquidScopedLoggingDecorator<TEntity> : ILiquidWorker<TEntity>
{
private readonly ILogger<LiquidScopedLoggingDecorator<TEntity>> _logger;
private readonly ILiquidConfiguration<ScopedLoggingSettings> _options;
private readonly IOptions<ScopedLoggingSettings> _options;
private readonly ILiquidWorker<TEntity> _inner;

/// <summary>
Expand All @@ -27,7 +28,7 @@ public class LiquidScopedLoggingDecorator<TEntity> : ILiquidWorker<TEntity>
/// <param name="options">Default culture configuration.</param>
/// <param name="logger">Logger service instance.</param>
public LiquidScopedLoggingDecorator(ILiquidWorker<TEntity> inner
, ILiquidConfiguration<ScopedLoggingSettings> options
, IOptions<ScopedLoggingSettings> options
, ILogger<LiquidScopedLoggingDecorator<TEntity>> logger)
{
_inner = inner ?? throw new ArgumentNullException(nameof(inner));
Expand All @@ -42,7 +43,7 @@ public async Task ProcessMessageAsync(ConsumerMessageEventArgs<TEntity> args, Ca

object value = default;

foreach (var key in _options.Settings.Keys)
foreach (var key in _options.Value.Keys)
{
args.Headers?.TryGetValue(key.KeyName, out value);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,7 @@ namespace Liquid.Core.Extensions.DependencyInjection
/// </summary>
public static class IServiceCollectionLiquidExtension
{
/// <summary>
/// Inject a <see cref="LiquidConfiguration{T}"/> for each configuration section entity.
/// </summary>
/// <param name="services">Extended IServiceCollection instance.</param>
public static IServiceCollection AddLiquidConfiguration(this IServiceCollection services)
{
services.TryAddTransient(typeof(ILiquidConfiguration<>), typeof(LiquidConfiguration<>));

return services;
}


/// <summary>
/// Register telemetry interceptor <see cref="LiquidTelemetryInterceptor"/> for defined <typeparamref name="TService"/> services.
/// </summary>
Expand Down
36 changes: 0 additions & 36 deletions src/Liquid.Core/Implementations/LiquidConfiguration.cs

This file was deleted.

14 changes: 0 additions & 14 deletions src/Liquid.Core/Interfaces/ILiquidConfiguration.cs

This file was deleted.

9 changes: 4 additions & 5 deletions src/Liquid.Core/Localization/JsonFileLocalization.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Liquid.Core.Extensions;
using Liquid.Core.Interfaces;
using Liquid.Core.Localization.Entities;
using Liquid.Core.Settings;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
Expand All @@ -26,7 +26,7 @@ public class JsonFileLocalization : ILocalization
/// Initializes a new instance of the <see cref="JsonFileLocalization" /> class.
/// </summary>
/// <param name="configuration">The configuration.</param>
public JsonFileLocalization(ILiquidConfiguration<CultureSettings> configuration)
public JsonFileLocalization(IOptions<CultureSettings> configuration)
{
_localizationItems = ReadLocalizationFiles(configuration);
}
Expand Down Expand Up @@ -101,9 +101,8 @@ public string Get(string key, CultureInfo culture, string channel = null)
/// Reads the resource collection from file.
/// </summary>
/// <param name="configuration">The configuration.</param>
/// <returns></returns>
/// <exception cref="LocalizationReaderException"></exception>
private static IDictionary<CultureInfo, LocalizationCollection> ReadLocalizationFiles(ILiquidConfiguration<CultureSettings> configuration)
private static IDictionary<CultureInfo, LocalizationCollection> ReadLocalizationFiles(IOptions<CultureSettings> configuration)
{
var items = new ConcurrentDictionary<CultureInfo, LocalizationCollection>();
try
Expand All @@ -115,7 +114,7 @@ private static IDictionary<CultureInfo, LocalizationCollection> ReadLocalization
{
var fileInfo = new FileInfo(fileName);
var filenameParts = fileInfo.Name.Split('.');
var culture = filenameParts.Length == 2 ? configuration.Settings.DefaultCulture : filenameParts[1];
var culture = filenameParts.Length == 2 ? configuration.Value.DefaultCulture : filenameParts[1];

using var fileReader = new StreamReader(fileName);
var json = fileReader.ReadToEnd();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Liquid.WebApi.Http.Settings;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;

namespace Liquid.WebApi.Http.Extensions.DependencyInjection
{
Expand Down Expand Up @@ -55,9 +56,9 @@ public static IApplicationBuilder UseLiquidScopedLogging(this IApplicationBuilde
/// <param name="builder">Extended application builder.</param>
public static IApplicationBuilder UseLiquidSwagger(this IApplicationBuilder builder)
{
var configuration = builder.ApplicationServices.GetService<ILiquidConfiguration<SwaggerSettings>>();
var configuration = builder.ApplicationServices.GetService<IOptions<SwaggerSettings>>();

var swaggerSettings = configuration.Settings;
var swaggerSettings = configuration.Value;
builder.UseSwagger().UseSwaggerUI(options =>
{
options.SwaggerEndpoint(swaggerSettings.SwaggerEndpoint.Url, swaggerSettings.SwaggerEndpoint.Name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
using Liquid.Core.Interfaces;
using Liquid.WebApi.Http.Filters.Swagger;
using Liquid.WebApi.Http.Settings;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Microsoft.OpenApi.Models;
using System;
using System.Diagnostics.CodeAnalysis;
Expand All @@ -23,18 +25,22 @@ public static class IServiceCollectionExtensions
{
/// <summary>
/// Registers a <see cref="LiquidContext"/> service and execute registration methods
/// to configure <see cref="IServiceCollectionLiquidExtension.AddLiquidConfiguration(IServiceCollection)"/>,
/// set mapping <see cref="IServiceCollectionAutoMapperExtensions.AddAutoMapper(IServiceCollection, Action{AutoMapper.IMapperConfigurationExpression}, Assembly[])"/>,
/// register domain handlers <see cref="IServiceCollectionCoreExtensions.AddLiquidHandlers(IServiceCollection, bool, bool, Assembly[])"/>,
/// and swagger <see cref="AddLiquidSwagger(IServiceCollection)"/>
/// </summary>
/// <param name="services">Extended service collection instance.</param>
/// <param name="assemblies">Array of assemblies that the domain handlers are implemented.</param>
public static IServiceCollection AddLiquidHttp(this IServiceCollection services, params Assembly[] assemblies)
/// <param name="sectionName">Swagger configuration section name.</param>
public static IServiceCollection AddLiquidHttp(this IServiceCollection services, string sectionName, params Assembly[] assemblies)
{
services.AddScoped<ILiquidContext, LiquidContext>();
services.AddLiquidSerializers();
services.AddLiquidConfiguration();
services.AddOptions<SwaggerSettings>()
.Configure<IConfiguration>((settings, configuration) =>
{
configuration.GetSection(sectionName).Bind(settings);
});
services.AddAutoMapper(assemblies);
services.AddLiquidHandlers(true, true, assemblies);

Expand All @@ -52,10 +58,10 @@ public static IServiceCollection AddLiquidSwagger(this IServiceCollection servic
{
var serviceProvider = services.BuildServiceProvider();

var configuration = serviceProvider.GetService<ILiquidConfiguration<SwaggerSettings>>();
if (configuration?.Settings == null) throw new LiquidException("'swagger' settings does not exist in appsettings.json file. Please check the file.");
var configuration = serviceProvider.GetService<IOptions<SwaggerSettings>>();
if (configuration?.Value == null) throw new LiquidException("'swagger' settings does not exist in appsettings.json file. Please check the file.");

var swaggerSettings = configuration.Settings;
var swaggerSettings = configuration.Value;
services.AddSwaggerGen(options =>
{
options.SwaggerDoc(swaggerSettings.Name,
Expand Down
9 changes: 5 additions & 4 deletions src/Liquid.WebApi.Http/Middlewares/LiquidContextMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Liquid.WebApi.Http.Extensions;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Threading.Tasks;
Expand All @@ -19,14 +20,14 @@ namespace Liquid.WebApi.Http.Middlewares
public class LiquidContextMiddleware
{
private readonly RequestDelegate _next;
private readonly ILiquidConfiguration<ScopedContextSettings> _options;
private readonly IOptions<ScopedContextSettings> _options;

/// <summary>
/// Initialize a instance of <see cref="LiquidContextMiddleware"/>
/// </summary>
/// <param name="next">Invoked request.</param>
/// <param name="options">Context keys configuration.</param>
public LiquidContextMiddleware(RequestDelegate next, ILiquidConfiguration<ScopedContextSettings> options)
public LiquidContextMiddleware(RequestDelegate next, IOptions<ScopedContextSettings> options)
{
_next = next;
_options = options;
Expand All @@ -44,7 +45,7 @@ public async Task InvokeAsync(HttpContext context)

var value = string.Empty;

foreach (var key in _options.Settings.Keys)
foreach (var key in _options.Value.Keys)
{
value = context.GetValueFromHeader(key.KeyName);

Expand All @@ -57,7 +58,7 @@ public async Task InvokeAsync(HttpContext context)
liquidContext.Upsert(key.KeyName, value);
}

if (_options.Settings.Culture)
if (_options.Value.Culture)
{
liquidContext.Upsert("culture", CultureInfo.CurrentCulture.Name);
}
Expand Down
9 changes: 5 additions & 4 deletions src/Liquid.WebApi.Http/Middlewares/LiquidCultureMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Liquid.Core.Settings;
using Liquid.WebApi.Http.Extensions;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Options;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Threading.Tasks;
Expand All @@ -16,15 +17,15 @@ namespace Liquid.WebApi.Http.Middlewares
public sealed class LiquidCultureMiddleware
{
private const string _culture = "culture";
private readonly ILiquidConfiguration<CultureSettings> _options;
private readonly IOptions<CultureSettings> _options;
private readonly RequestDelegate _next;

/// <summary>
/// Initializes a new instance of the <see cref="LiquidCultureMiddleware" /> class.
/// </summary>
/// <param name="next">The next.</param>
/// <param name="options"></param>
public LiquidCultureMiddleware(RequestDelegate next, ILiquidConfiguration<CultureSettings> options)
public LiquidCultureMiddleware(RequestDelegate next, IOptions<CultureSettings> options)
{
_next = next;
_options = options;
Expand All @@ -44,9 +45,9 @@ public async Task InvokeAsync(HttpContext context)
cultureCode = context.GetValueFromQuery(_culture).ToString();
}

if (string.IsNullOrEmpty(cultureCode) && !string.IsNullOrEmpty(_options.Settings.DefaultCulture))
if (string.IsNullOrEmpty(cultureCode) && !string.IsNullOrEmpty(_options.Value.DefaultCulture))
{
cultureCode = _options.Settings.DefaultCulture;
cultureCode = _options.Value.DefaultCulture;
}

if (!string.IsNullOrEmpty(cultureCode))
Expand Down
Loading

0 comments on commit a5c4af4

Please sign in to comment.