Skip to content

Commit

Permalink
Passes IConfiguration while register modules.
Browse files Browse the repository at this point in the history
  • Loading branch information
Codespilot committed Jul 7, 2023
1 parent a02f963 commit 5eea8f3
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 28 deletions.
17 changes: 9 additions & 8 deletions Source/Euonia.Modularity/Core/ApplicationFactory.cs
Original file line number Diff line number Diff line change
@@ -1,34 +1,35 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace Nerosoft.Euonia.Modularity;

/// <summary>
///
/// Contains methods to create new instance of <see cref="IApplicationWithServiceProvider"/>.
/// </summary>
public static class ApplicationFactory
{
/// <summary>
///
/// Create new instance of <see cref="IApplicationWithServiceProvider"/>.
/// </summary>
/// <param name="services"></param>
/// <param name="optionsAction"></param>
/// <typeparam name="TStartupModule"></typeparam>
/// <returns></returns>
public static IApplicationWithServiceProvider Create<TStartupModule>(IServiceCollection services, Action<ApplicationCreationOptions> optionsAction = null)
public static IApplicationWithServiceProvider Create<TStartupModule>(IServiceCollection services, IConfiguration configuration, Action<ApplicationCreationOptions> optionsAction = null)

Check warning on line 18 in Source/Euonia.Modularity/Core/ApplicationFactory.cs

View workflow job for this annotation

GitHub Actions / build (7.0.x, Release)

Parameter 'configuration' has no matching param tag in the XML comment for 'ApplicationFactory.Create<TStartupModule>(IServiceCollection, IConfiguration, Action<ApplicationCreationOptions>)' (but other parameters do)

Check warning on line 18 in Source/Euonia.Modularity/Core/ApplicationFactory.cs

View workflow job for this annotation

GitHub Actions / build (7.0.x, Release)

Parameter 'configuration' has no matching param tag in the XML comment for 'ApplicationFactory.Create<TStartupModule>(IServiceCollection, IConfiguration, Action<ApplicationCreationOptions>)' (but other parameters do)

Check warning on line 18 in Source/Euonia.Modularity/Core/ApplicationFactory.cs

View workflow job for this annotation

GitHub Actions / build (6.0.x, Release)

Parameter 'configuration' has no matching param tag in the XML comment for 'ApplicationFactory.Create<TStartupModule>(IServiceCollection, IConfiguration, Action<ApplicationCreationOptions>)' (but other parameters do)

Check warning on line 18 in Source/Euonia.Modularity/Core/ApplicationFactory.cs

View workflow job for this annotation

GitHub Actions / build (6.0.x, Release)

Parameter 'configuration' has no matching param tag in the XML comment for 'ApplicationFactory.Create<TStartupModule>(IServiceCollection, IConfiguration, Action<ApplicationCreationOptions>)' (but other parameters do)

Check warning on line 18 in Source/Euonia.Modularity/Core/ApplicationFactory.cs

View workflow job for this annotation

GitHub Actions / build (6.0.x, Release)

Parameter 'configuration' has no matching param tag in the XML comment for 'ApplicationFactory.Create<TStartupModule>(IServiceCollection, IConfiguration, Action<ApplicationCreationOptions>)' (but other parameters do)

Check warning on line 18 in Source/Euonia.Modularity/Core/ApplicationFactory.cs

View workflow job for this annotation

GitHub Actions / build (7.0.x, Release)

Parameter 'configuration' has no matching param tag in the XML comment for 'ApplicationFactory.Create<TStartupModule>(IServiceCollection, IConfiguration, Action<ApplicationCreationOptions>)' (but other parameters do)

Check warning on line 18 in Source/Euonia.Modularity/Core/ApplicationFactory.cs

View workflow job for this annotation

GitHub Actions / build (6.0.x, Release)

Parameter 'configuration' has no matching param tag in the XML comment for 'ApplicationFactory.Create<TStartupModule>(IServiceCollection, IConfiguration, Action<ApplicationCreationOptions>)' (but other parameters do)

Check warning on line 18 in Source/Euonia.Modularity/Core/ApplicationFactory.cs

View workflow job for this annotation

GitHub Actions / build (6.0.x, Release)

Parameter 'configuration' has no matching param tag in the XML comment for 'ApplicationFactory.Create<TStartupModule>(IServiceCollection, IConfiguration, Action<ApplicationCreationOptions>)' (but other parameters do)

Check warning on line 18 in Source/Euonia.Modularity/Core/ApplicationFactory.cs

View workflow job for this annotation

GitHub Actions / build (6.0.x, Release)

Parameter 'configuration' has no matching param tag in the XML comment for 'ApplicationFactory.Create<TStartupModule>(IServiceCollection, IConfiguration, Action<ApplicationCreationOptions>)' (but other parameters do)
where TStartupModule : IModuleContext
{
return Create(typeof(TStartupModule), services, optionsAction);
return Create(typeof(TStartupModule), services, configuration, optionsAction);
}

/// <summary>
///
/// Create new instance of <see cref="IApplicationWithServiceProvider"/>.
/// </summary>
/// <param name="startupModuleType"></param>
/// <param name="services"></param>
/// <param name="optionsAction"></param>
/// <returns></returns>
public static IApplicationWithServiceProvider Create(Type startupModuleType, IServiceCollection services, Action<ApplicationCreationOptions> optionsAction = null)
public static IApplicationWithServiceProvider Create(Type startupModuleType, IServiceCollection services, IConfiguration configuration, Action<ApplicationCreationOptions> optionsAction = null)

Check warning on line 31 in Source/Euonia.Modularity/Core/ApplicationFactory.cs

View workflow job for this annotation

GitHub Actions / build (7.0.x, Release)

Parameter 'configuration' has no matching param tag in the XML comment for 'ApplicationFactory.Create(Type, IServiceCollection, IConfiguration, Action<ApplicationCreationOptions>)' (but other parameters do)

Check warning on line 31 in Source/Euonia.Modularity/Core/ApplicationFactory.cs

View workflow job for this annotation

GitHub Actions / build (7.0.x, Release)

Parameter 'configuration' has no matching param tag in the XML comment for 'ApplicationFactory.Create(Type, IServiceCollection, IConfiguration, Action<ApplicationCreationOptions>)' (but other parameters do)

Check warning on line 31 in Source/Euonia.Modularity/Core/ApplicationFactory.cs

View workflow job for this annotation

GitHub Actions / build (6.0.x, Release)

Parameter 'configuration' has no matching param tag in the XML comment for 'ApplicationFactory.Create(Type, IServiceCollection, IConfiguration, Action<ApplicationCreationOptions>)' (but other parameters do)

Check warning on line 31 in Source/Euonia.Modularity/Core/ApplicationFactory.cs

View workflow job for this annotation

GitHub Actions / build (6.0.x, Release)

Parameter 'configuration' has no matching param tag in the XML comment for 'ApplicationFactory.Create(Type, IServiceCollection, IConfiguration, Action<ApplicationCreationOptions>)' (but other parameters do)

Check warning on line 31 in Source/Euonia.Modularity/Core/ApplicationFactory.cs

View workflow job for this annotation

GitHub Actions / build (6.0.x, Release)

Parameter 'configuration' has no matching param tag in the XML comment for 'ApplicationFactory.Create(Type, IServiceCollection, IConfiguration, Action<ApplicationCreationOptions>)' (but other parameters do)

Check warning on line 31 in Source/Euonia.Modularity/Core/ApplicationFactory.cs

View workflow job for this annotation

GitHub Actions / build (6.0.x, Release)

Parameter 'configuration' has no matching param tag in the XML comment for 'ApplicationFactory.Create(Type, IServiceCollection, IConfiguration, Action<ApplicationCreationOptions>)' (but other parameters do)

Check warning on line 31 in Source/Euonia.Modularity/Core/ApplicationFactory.cs

View workflow job for this annotation

GitHub Actions / build (6.0.x, Release)

Parameter 'configuration' has no matching param tag in the XML comment for 'ApplicationFactory.Create(Type, IServiceCollection, IConfiguration, Action<ApplicationCreationOptions>)' (but other parameters do)

Check warning on line 31 in Source/Euonia.Modularity/Core/ApplicationFactory.cs

View workflow job for this annotation

GitHub Actions / build (6.0.x, Release)

Parameter 'configuration' has no matching param tag in the XML comment for 'ApplicationFactory.Create(Type, IServiceCollection, IConfiguration, Action<ApplicationCreationOptions>)' (but other parameters do)
{
return new ApplicationWithServiceProvider(startupModuleType, services, optionsAction);
return new ApplicationWithServiceProvider(startupModuleType, services, configuration, optionsAction);
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace Nerosoft.Euonia.Modularity;

/// <summary>
///
/// A base class for applications that provides access to the <see cref="IServiceProvider"/>.
/// </summary>
public class ApplicationWithServiceProvider : ModularityApplicationBase, IApplicationWithServiceProvider
{
/// <inheritdoc />
public ApplicationWithServiceProvider(Type startupModuleType, IServiceCollection services, Action<ApplicationCreationOptions> optionsAction)
: base(startupModuleType, services, optionsAction)
public ApplicationWithServiceProvider(Type startupModuleType, IServiceCollection services, IConfiguration configuration, Action<ApplicationCreationOptions> optionsAction)
: base(startupModuleType, services, configuration, optionsAction)
{
services.AddSingleton<IApplicationWithServiceProvider>(this);
}
Expand Down
36 changes: 23 additions & 13 deletions Source/Euonia.Modularity/Core/ModularityApplicationBase.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Nerosoft.Euonia.Dependency;

namespace Nerosoft.Euonia.Modularity;

/// <summary>
///
/// To be added.
/// </summary>
public abstract class ModularityApplicationBase : IModularityApplication
{
internal ModularityApplicationBase(Type startupModuleType, IServiceCollection services, Action<ApplicationCreationOptions> optionsAction)
internal ModularityApplicationBase(Type startupModuleType, IServiceCollection services, IConfiguration configuration, Action<ApplicationCreationOptions> optionsAction)
{
StartupModuleType = startupModuleType;
Services = services;
Configuration = configuration;

services.TryAddObjectAccessor<IServiceProvider>();

Expand All @@ -31,30 +33,36 @@ internal ModularityApplicationBase(Type startupModuleType, IServiceCollection se
}

/// <summary>
///
/// Gets the startup module type of current application.
/// </summary>
public Type StartupModuleType { get; }

/// <summary>
///
/// Gets the service collection of current application.
/// </summary>
public IServiceCollection Services { get; }

/// <summary>
///
/// Gets the service provider of current application.
/// </summary>
public IServiceProvider ServiceProvider { get; private set; }

/// <summary>
///
/// Gets the configuration context instance of current application.
/// </summary>
public IConfiguration Configuration { get; private set; }

/// <summary>
/// Gets the registered modules.
/// </summary>
public IReadOnlyList<IModuleDescriptor> Modules { get; }

/// <summary>
///
/// Disposes the application.
/// </summary>
public virtual void Dispose()
{
GC.SuppressFinalize(this);
}

/// <summary>
Expand All @@ -69,17 +77,18 @@ public void Shutdown()
}

/// <summary>
///
/// Gets the application service provider.
/// </summary>
/// <param name="serviceProvider"></param>
protected virtual void SetServiceProvider(IServiceProvider serviceProvider)
{
ServiceProvider = serviceProvider;
ServiceProvider.GetRequiredService<ObjectAccessor<IServiceProvider>>().Value = ServiceProvider;
Configuration ??= serviceProvider.GetService<IConfiguration>();
}

/// <summary>
///
/// Initializes the dependent modules.
/// </summary>
protected virtual void InitializeModules()
{
Expand All @@ -90,7 +99,7 @@ protected virtual void InitializeModules()
}

/// <summary>
///
/// Loads the dependent modules.
/// </summary>
/// <param name="services"></param>
/// <param name="options"></param>
Expand All @@ -102,7 +111,7 @@ protected virtual IReadOnlyList<IModuleDescriptor> LoadModules(IServiceCollectio
}

/// <summary>
///
/// Configures the services of the application.
/// </summary>
/// <exception cref="Exception"></exception>
protected virtual void ConfigureServices()
Expand All @@ -115,6 +124,7 @@ protected virtual void ConfigureServices()
if (module.Instance is ModuleContextBase moduleContext)
{
moduleContext.ConfigurationContext = context;
moduleContext.Configuration = Configuration;
}
}

Expand Down Expand Up @@ -158,7 +168,7 @@ protected virtual void ConfigureServices()
throw new Exception($"An error occurred during {nameof(ModuleContextBase.AfterConfigureServices)} phase of the module {module.Type.AssemblyQualifiedName}. See the inner exception for details.", exception);
}
}

foreach (var module in Modules)
{
if (module.Instance is ModuleContextBase moduleContext)
Expand Down
7 changes: 6 additions & 1 deletion Source/Euonia.Modularity/Core/ModuleContextBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public abstract class ModuleContextBase : IModuleContext
protected internal virtual bool AutomaticRegisterService => true;

/// <summary>
///
/// Gets or sets the service configuration context.
/// </summary>
/// <exception cref="Exception"></exception>
protected internal ServiceConfigurationContext ConfigurationContext
Expand All @@ -33,6 +33,11 @@ protected internal ServiceConfigurationContext ConfigurationContext

private ServiceConfigurationContext _configurationContext;

/// <summary>
/// Gets or sets the application configuration service.
/// </summary>
protected internal IConfiguration Configuration { get; internal set; }

/// <inheritdoc />
public virtual void AheadConfigureServices(ServiceConfigurationContext context)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ public static class ServiceCollectionModularityExtensions
/// Register a modularity application context.
/// </summary>
/// <param name="services"></param>
/// <param name="configuration"></param>
/// <param name="optionsAction"></param>
/// <typeparam name="TStartupModel"></typeparam>
/// <returns></returns>
public static IApplicationWithServiceProvider AddModularityApplication<TStartupModel>(this IServiceCollection services, Action<ApplicationCreationOptions> optionsAction = null)
public static IApplicationWithServiceProvider AddModularityApplication<TStartupModel>(this IServiceCollection services, IConfiguration configuration = null, Action<ApplicationCreationOptions> optionsAction = null)
where TStartupModel : class, IModuleContext
{
return ApplicationFactory.Create<TStartupModel>(services, optionsAction);
return ApplicationFactory.Create<TStartupModel>(services, configuration, optionsAction);
}

internal static void AddCoreServices(this IServiceCollection services)
Expand Down
12 changes: 12 additions & 0 deletions Source/Euonia.Modularity/Reflection/AssemblyFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,33 @@

namespace Nerosoft.Euonia.Modularity;

/// <summary>
/// Finds all assemblies for the specified module container.
/// </summary>
public class AssemblyFinder : IAssemblyFinder
{
private readonly IModuleContainer _moduleContainer;

private readonly Lazy<IReadOnlyList<Assembly>> _assemblies;

/// <summary>
/// Initialize a new instance of <see cref="AssemblyFinder"/>.
/// </summary>
/// <param name="moduleContainer"></param>
public AssemblyFinder(IModuleContainer moduleContainer)
{
_moduleContainer = moduleContainer;

_assemblies = new Lazy<IReadOnlyList<Assembly>>(FindAll, LazyThreadSafetyMode.ExecutionAndPublication);
}

/// <inheritdoc />
public IReadOnlyList<Assembly> Assemblies => _assemblies.Value;

/// <summary>
/// Finds all assemblies for the specified module container.
/// </summary>
/// <returns></returns>
public IReadOnlyList<Assembly> FindAll()
{
var assemblies = new List<Assembly>();
Expand Down
7 changes: 7 additions & 0 deletions Source/Euonia.Modularity/Reflection/IAssemblyFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@

namespace Nerosoft.Euonia.Modularity;

/// <summary>
/// Finds the assemblies used by the application.
/// </summary>
public interface IAssemblyFinder
{
/// <summary>
/// Gets the assemblies.
/// </summary>
/// <returns>An immutable list containing the assemblies used by the application.</returns>
IReadOnlyList<Assembly> Assemblies { get; }
}
6 changes: 6 additions & 0 deletions Source/Euonia.Modularity/Reflection/ITypeFinder.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
namespace Nerosoft.Euonia.Modularity;

/// <summary>
/// This interface defines a contract for a type finder that retrieves an read-only list of types.
/// </summary>
public interface ITypeFinder
{
/// <summary>
/// Gets the list of types.
/// </summary>
IReadOnlyList<Type> Types { get; }
}
14 changes: 14 additions & 0 deletions Source/Euonia.Modularity/Reflection/TypeFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,35 @@

namespace Nerosoft.Euonia.Modularity;

/// <summary>
/// This class uses reflection to find types within assemblies
/// </summary>
public class TypeFinder : ITypeFinder
{
private readonly IAssemblyFinder _assemblyFinder;

private readonly Lazy<IReadOnlyList<Type>> _types;

/// <summary>
/// Initialize a new instalce of <see cref="TypeFinder"/>.
/// </summary>
/// <param name="assemblyFinder"></param>
public TypeFinder(IAssemblyFinder assemblyFinder)
{
_assemblyFinder = assemblyFinder;

_types = new Lazy<IReadOnlyList<Type>>(FindAll, LazyThreadSafetyMode.ExecutionAndPublication);
}

/// <summary>
/// Gets the types list.
/// </summary>
public IReadOnlyList<Type> Types => _types.Value;

/// <summary>
/// Finds all types within the assembly.
/// </summary>
/// <returns></returns>
private IReadOnlyList<Type> FindAll()
{
var allTypes = new List<Type>();
Expand Down

0 comments on commit 5eea8f3

Please sign in to comment.