Skip to content

Commit

Permalink
Pull provider configuration from database (#203)
Browse files Browse the repository at this point in the history
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
Shawn Vause and github-actions[bot] authored Sep 27, 2023
1 parent 3294481 commit 362d1c2
Show file tree
Hide file tree
Showing 36 changed files with 1,569 additions and 473 deletions.
38 changes: 18 additions & 20 deletions src/TagzApp.Common/IConfigureProvider.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace TagzApp.Common;

/// <summary>
/// An interface to configure the services for a Social Media Provider
/// </summary>
public interface IConfigureProvider
{

/// <summary>
/// Register all of the services with dependency injection for the social media provider
/// </summary>
/// <param name="services">The dependency injection services</param>
/// <param name="configuration">Application Configuration</param>
/// <returns></returns>
IServiceCollection RegisterServices(IServiceCollection services, IConfiguration configuration);

}
using Microsoft.Extensions.DependencyInjection;

namespace TagzApp.Common;

/// <summary>
/// An interface to configure the services for a Social Media Provider
/// </summary>
public interface IConfigureProvider
{
/// <summary>
/// Register all of the services with dependency injection for the social media provider
/// </summary>
/// <param name="services">The dependency injection services</param>
/// <param name="cancellationToken"></param>
/// <returns>Service Collection</returns>
Task<IServiceCollection> RegisterServices(IServiceCollection services, CancellationToken cancellationToken = default);

}
6 changes: 6 additions & 0 deletions src/TagzApp.Common/IProviderConfigurationRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace TagzApp.Common;

public interface IProviderConfigurationRepository
{
Task<ProviderConfiguration?> GetConfigurationSettingsAsync(string name, CancellationToken cancellationToken = default);
}
42 changes: 42 additions & 0 deletions src/TagzApp.Common/InMemoryProviderConfigurationRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using Microsoft.Extensions.Configuration;

namespace TagzApp.Common;
public class InMemoryProviderConfigurationRepository : IProviderConfigurationRepository
{
private readonly IConfiguration _Configuration;

public InMemoryProviderConfigurationRepository(IConfiguration configuration)
{
_Configuration = configuration;
}

public async Task<ProviderConfiguration> GetConfigurationSettingsAsync(string name, CancellationToken cancellationToken = default)
{
var appSettingsSection = $"providers:{name.ToLower()}";
var providerConfig = new ProviderConfiguration
{
Name = name,
};

var configSettings = _Configuration.GetSection(appSettingsSection).GetChildren();

foreach (var configSetting in configSettings)
{
if (configSetting.Key == "Activated")
{
providerConfig.Activated = bool.Parse(configSetting.Value ?? "false");
continue;
}

if (configSetting.Key == "Description")
{
providerConfig.Description = configSetting.Value ?? string.Empty;
continue;
}

providerConfig.ConfigurationSettings!.Add(configSetting.Key, configSetting.Value ?? string.Empty);
}

return await Task.FromResult(providerConfig);
}
}
15 changes: 15 additions & 0 deletions src/TagzApp.Common/Models/ProviderConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace TagzApp.Common.Models;

public class ProviderConfiguration
{
public int Id { get; set; }
public string Name { get; set; } = string.Empty;
public string Description { get; set; } = string.Empty;
public Dictionary<string, string>? ConfigurationSettings { get; set; }
public bool Activated { get; set; }

public ProviderConfiguration()
{
ConfigurationSettings = new Dictionary<string, string>();
}
}
23 changes: 23 additions & 0 deletions src/TagzApp.Communication/BaseConfigurationProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace TagzApp.Communication;

public abstract class BaseConfigurationProvider
{
private readonly IProviderConfigurationRepository _ProviderConfigurationRepository;

public BaseConfigurationProvider(IProviderConfigurationRepository providerConfigurationRepository)
{
_ProviderConfigurationRepository = providerConfigurationRepository;
}

protected abstract void MapConfigurationValues(ProviderConfiguration providerConfiguration);

protected async Task LoadConfigurationValuesAsync(string providerName, CancellationToken cancellationToken = default)
{
var providerConfiguration = await _ProviderConfigurationRepository.GetConfigurationSettingsAsync(providerName, cancellationToken);

if (providerConfiguration != null)
{
MapConfigurationValues(providerConfiguration);
}
}
}
22 changes: 13 additions & 9 deletions src/TagzApp.Communication/BaseProviderManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,31 @@ public class BaseProviderManager
private readonly IServiceCollection _Services;
private readonly IConfiguration _Configuration;
private readonly ILogger<BaseProviderManager> _Logger;
private readonly IProviderConfigurationRepository? _ProviderConfigurationRepository;

public IEnumerable<ISocialMediaProvider> Providers { get; private set; }

public BaseProviderManager(IConfiguration configuration, ILogger<BaseProviderManager> logger,
IEnumerable<ISocialMediaProvider>? socialMediaProviders)
IEnumerable<ISocialMediaProvider>? socialMediaProviders,
IProviderConfigurationRepository? providerConfigurationRepository)
{
_Services = new ServiceCollection();
_Configuration = configuration;
_Logger = logger;
_ProviderConfigurationRepository = providerConfigurationRepository;
Providers = socialMediaProviders != null && socialMediaProviders.Count() > 0
? socialMediaProviders : new List<ISocialMediaProvider>();
}

public void InitProviders()
public async Task InitProviders()
{
if (!Providers.Any())
{
LoadConfigurationProviders();
await LoadConfigurationProviders();
}
}

private void LoadConfigurationProviders()
private async Task LoadConfigurationProviders()
{
List<IConfigureProvider> configProviders = new();
var path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) ?? string.Empty;
Expand All @@ -41,7 +45,7 @@ private void LoadConfigurationProviders()
foreach (string dllPath in Directory.GetFiles(path, "*.dll", SearchOption.AllDirectories))
{

if (dllPath.Contains("Microsoft.") || dllPath.Contains("System.")) continue;
if (dllPath.Contains("Microsoft.") || dllPath.Contains("System.") || dllPath.Contains("AspNet.") || dllPath.Contains("Azure.")) continue;

try
{
Expand All @@ -53,7 +57,7 @@ private void LoadConfigurationProviders()
{
foreach (var provider in providerAssemblies)
{
var providerInstance = Activator.CreateInstance(provider) as IConfigureProvider;
var providerInstance = Activator.CreateInstance(provider, _ProviderConfigurationRepository) as IConfigureProvider;

if (providerInstance != null)
{
Expand All @@ -72,17 +76,17 @@ private void LoadConfigurationProviders()
}
}

ConfigureProviders(configProviders);
await ConfigureProviders(configProviders);
}
}

private void ConfigureProviders(IEnumerable<IConfigureProvider> configurationProviders)
private async Task ConfigureProviders(IEnumerable<IConfigureProvider> configurationProviders)
{
var socialMediaProviders = new List<ISocialMediaProvider>();

foreach (var provider in configurationProviders)
{
provider.RegisterServices(_Services, _Configuration);
await provider.RegisterServices(_Services);
}

_Services.AddPolicies(_Configuration);
Expand Down
Loading

0 comments on commit 362d1c2

Please sign in to comment.