Core - Refactor service registration

This commit is contained in:
Robert committed 2023-02-04 23:25:48 +01:00
1 parent a1086df41a
commit f2ef657d00
18 files changed
+234 -231

No files matched your search

@@ -0,0 +1,66 @@
using System;
using System.Linq;
using System.Reflection;
using Artemis.Core.DryIoc.Factories;
using Artemis.Core.Services;
using Artemis.Storage;
using Artemis.Storage.Migrations.Interfaces;
using Artemis.Storage.Repositories.Interfaces;
using DryIoc;
namespace Artemis.Core.DryIoc;
/// <summary>
/// Provides an extension method to register services onto a DryIoc <see cref="IContainer"/>.
/// </summary>
public static class CoreContainerExtensions
{
/// <summary>
/// Registers core services into the container.
/// </summary>
/// <param name="container">The builder building the current container</param>
public static void RegisterCore(this IContainer container)
{
Assembly[] coreAssembly = {typeof(IArtemisService).Assembly};
Assembly[] storageAssembly = {typeof(IRepository).Assembly};
// Bind all services as singletons
container.RegisterMany(coreAssembly, type => type.IsAssignableTo<IArtemisService>(), Reuse.Singleton);
container.RegisterMany(coreAssembly, type => type.IsAssignableTo<IProtectedArtemisService>(), Reuse.Singleton, setup: Setup.With(condition: HasAccessToProtectedService));
// Bind storage
container.RegisterDelegate(() => StorageManager.CreateRepository(Constants.DataFolder), Reuse.Singleton);
container.Register<StorageMigrationService>(Reuse.Singleton);
container.RegisterMany(storageAssembly, type => type.IsAssignableTo<IRepository>(), Reuse.Singleton);
// Bind migrations
container.RegisterMany(storageAssembly, type => type.IsAssignableTo<IStorageMigration>(), Reuse.Singleton, nonPublicServiceTypes: true);
container.Register<IPluginSettingsFactory, PluginSettingsFactory>(Reuse.Singleton);
container.Register(made: Made.Of(_ => ServiceInfo.Of<IPluginSettingsFactory>(), f => f.CreatePluginSettings(Arg.Index<Type>(0)), r => r.Parent.ImplementationType));
container.Register<ILoggerFactory, LoggerFactory>(Reuse.Singleton);
container.Register(made: Made.Of(_ => ServiceInfo.Of<ILoggerFactory>(), f => f.CreateLogger(Arg.Index<Type>(0)), r => r.Parent.ImplementationType));
}
/// <summary>
/// Registers plugin services into the container, this is typically a child container.
/// </summary>
/// <param name="container">The builder building the current container</param>
/// <param name="plugin">The plugin to register</param>
public static void RegisterPlugin(this IContainer container, Plugin plugin)
{
container.RegisterInstance(plugin, setup: Setup.With(preventDisposal: true));
// Bind plugin service interfaces, DryIoc expects at least one match when calling RegisterMany so ensure there is something to register first
if (plugin.Assembly != null && plugin.Assembly.GetTypes().Any(t => t.IsAssignableTo<IPluginService>()))
container.RegisterMany(new[] {plugin.Assembly}, type => type.IsAssignableTo<IPluginService>(), Reuse.Singleton, ifAlreadyRegistered: IfAlreadyRegistered.Keep);
}
private static bool HasAccessToProtectedService(Request request)
{
// Plugin assembly locations may not be set for some reason, that case it's also not allowed >:(
return request.Parent.ImplementationType != null &&
!string.IsNullOrWhiteSpace(request.Parent.ImplementationType.Assembly.Location) &&
!request.Parent.ImplementationType.Assembly.Location.StartsWith(Constants.PluginsFolder);
}
}
-48
View File
@@ -1,48 +0,0 @@
using System;
using System.Reflection;
using Artemis.Core.DryIoc.Factories;
using Artemis.Core.Services;
using Artemis.Storage;
using Artemis.Storage.Migrations.Interfaces;
using Artemis.Storage.Repositories.Interfaces;
using DryIoc;
namespace Artemis.Core.DryIoc;
/// <summary>
/// The main <see cref="IModule" /> of the Artemis Core that binds all services
/// </summary>
public class CoreModule : IModule
{
/// <inheritdoc />
public void Load(IRegistrator builder)
{
Assembly coreAssembly = typeof(IArtemisService).Assembly;
Assembly storageAssembly = typeof(IRepository).Assembly;
// Bind all services as singletons
builder.RegisterMany(new[] {coreAssembly}, type => type.IsAssignableTo<IArtemisService>(), Reuse.Singleton);
builder.RegisterMany(new[] {coreAssembly}, type => type.IsAssignableTo<IProtectedArtemisService>(), Reuse.Singleton, setup: Setup.With(condition: HasAccessToProtectedService));
// Bind storage
builder.RegisterDelegate(() => StorageManager.CreateRepository(Constants.DataFolder), Reuse.Singleton);
builder.Register<StorageMigrationService>(Reuse.Singleton);
builder.RegisterMany(new[] {storageAssembly}, type => type.IsAssignableTo<IRepository>(), Reuse.Singleton);
// Bind migrations
builder.RegisterMany(new[] { storageAssembly }, type => type.IsAssignableTo<IStorageMigration>(), Reuse.Singleton, nonPublicServiceTypes: true);
builder.Register<IPluginSettingsFactory, PluginSettingsFactory>(Reuse.Singleton);
builder.Register(made: Made.Of(_ => ServiceInfo.Of<IPluginSettingsFactory>(), factory => factory.CreatePluginSettings(Arg.Index<Type>(0)), r => r.Parent.ImplementationType));
builder.Register<ILoggerFactory, LoggerFactory>(Reuse.Singleton);
builder.Register(made: Made.Of(_ => ServiceInfo.Of<ILoggerFactory>(), f => f.CreateLogger(Arg.Index<Type>(0)), r => r.Parent.ImplementationType));
}
private bool HasAccessToProtectedService(Request request)
{
// Plugin assembly locations may not be set for some reason, that case it's also not allowed >:(
return request.Parent.ImplementationType != null &&
!string.IsNullOrWhiteSpace(request.Parent.ImplementationType.Assembly.Location) &&
!request.Parent.ImplementationType.Assembly.Location.StartsWith(Constants.PluginsFolder);
}
}
-15
View File
@@ -1,15 +0,0 @@
using DryIoc;
namespace Artemis.Core.DryIoc;
/**
* Represents a service module.
*/
public interface IModule
{
/// <summary>
/// Registers the services provided by the module.
/// </summary>
/// <param name="builder">The builder to register the services with.</param>
void Load(IRegistrator builder);
}
-26
View File
@@ -1,26 +0,0 @@
using System;
using System.Linq;
using Artemis.Core.Services;
using DryIoc;
namespace Artemis.Core.DryIoc;
internal class PluginModule : IModule
{
public PluginModule(Plugin plugin)
{
Plugin = plugin ?? throw new ArgumentNullException(nameof(plugin));
}
public Plugin Plugin { get; }
/// <inheritdoc />
public void Load(IRegistrator builder)
{
builder.RegisterInstance(Plugin, setup: Setup.With(preventDisposal: true));
// Bind plugin service interfaces, DryIoc expects at least one match when calling RegisterMany so ensure there is something to register first
if (Plugin.Assembly != null && Plugin.Assembly.GetTypes().Any(t => t.IsAssignableTo<IPluginService>()))
builder.RegisterMany(new[] {Plugin.Assembly}, type => type.IsAssignableTo<IPluginService>(), Reuse.Singleton, ifAlreadyRegistered: IfAlreadyRegistered.Keep);
}
}
@@ -435,7 +435,7 @@ internal class PluginManagementService : IPluginManagementService
plugin.Container = _container.CreateChild(newRules: _container.Rules.WithConcreteTypeDynamicRegistrations());
try
{
new PluginModule(plugin).Load(plugin.Container);
plugin.Container.RegisterPlugin(plugin);
}
catch (Exception e)
{