Skip to content

Commit

Permalink
feat: Switch from Unity Container to DryIOC
Browse files Browse the repository at this point in the history
This commit switches the dependency injection container in the YumeChan.ConsoleRunner project from Unity Container to DryIOC. The following changes were made:

- Removed references to Unity and related namespaces
- Added references to DryIoc and DryIoc.Microsoft.DependencyInjection
- Updated the CreateHostBuilder method to use DryIocServiceProviderFactory instead of UseUnityServiceProvider
- Updated the ConfigureServices method in Program.cs to use IServiceCollection instead of IUnityContainer
- Replaced calls to Resolve<Microsoft.Extensions.Logging.ILogger> with GetRequiredService<Microsoft.Extensions.Logging.ILogger>
- Updated YumeCore class constructor parameter type from IUnityContainer to IContainer
- Removed unused using statements
  • Loading branch information
SakuraIsayeki committed Jul 16, 2023
1 parent e0e9edf commit ac6376f
Show file tree
Hide file tree
Showing 19 changed files with 58 additions and 87 deletions.
44 changes: 21 additions & 23 deletions src/YumeChan.ConsoleRunner/Program.cs
Original file line number Diff line number Diff line change
@@ -1,44 +1,42 @@
using Microsoft.Extensions.DependencyInjection;
using System;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Serilog;
using Serilog.Events;
using Serilog.Extensions.Logging;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using Unity;
using Unity.Microsoft.DependencyInjection;
using Unity.Microsoft.Logging;
using DryIoc;
using DryIoc.Microsoft.DependencyInjection;
using YumeChan.Core;
using YumeChan.PluginBase.Tools;

namespace YumeChan.ConsoleRunner;

public static class Program
{
private static IUnityContainer _container = new UnityContainer();

private static readonly LoggerConfiguration SerilogConfiguration = new LoggerConfiguration()
.MinimumLevel.Debug()
.MinimumLevel.Override("Microsoft", LogEventLevel.Information)
.MinimumLevel.Override("DSharpPlus", LogEventLevel.Information)
.Enrich.FromLogContext()
.WriteTo.Console();

private static readonly string _informationalVersion = Assembly.GetEntryAssembly()?.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion;

public static async Task Main(string[] _)
{
string informationalVersion = Assembly.GetEntryAssembly()?.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion;


Log.Logger = SerilogConfiguration.CreateLogger();

IHost host = CreateHostBuilder().Build();

await using AsyncServiceScope scope = host.Services.CreateAsyncScope();
IServiceProvider services = scope.ServiceProvider;

YumeCore.Instance.Services = _container;
_container.RegisterInstance(new ConsoleRunnerContext(RunnerType.Console, typeof(Program).Assembly.GetName().Name, informationalVersion));

Microsoft.Extensions.Logging.ILogger logger = _container.Resolve<Microsoft.Extensions.Logging.ILogger>();
logger.LogInformation("Yume-Chan ConsoleRunner v{version}.", informationalVersion);
Microsoft.Extensions.Logging.ILogger logger = services.GetRequiredService<Microsoft.Extensions.Logging.ILogger>();
logger.LogInformation("Yume-Chan ConsoleRunner v{version}.", _informationalVersion);

await Task.WhenAll(
host.StartAsync(),
Expand All @@ -48,17 +46,17 @@ public static async Task Main(string[] _)
await host.WaitForShutdownAsync();
}

public static IHostBuilder CreateHostBuilder(UnityContainer serviceRegistry = null) => new HostBuilder()
.UseUnityServiceProvider(serviceRegistry ?? new())
public static IHostBuilder CreateHostBuilder() => new HostBuilder()
.UseServiceProviderFactory(new DryIocServiceProviderFactory())
.ConfigureLogging(static x => x.ClearProviders())
.UseSerilog()
.ConfigureContainer<IUnityContainer>(static (_, container) =>
.ConfigureContainer<Container>(static (_, container) =>
{
_container = container; // This assignment is necessary, as configuration only affects the child container.
container.AddExtension(new LoggingExtension(new SerilogLoggerFactory()));
container.AddServices(new ServiceCollection()
.AddYumeCoreServices()
.AddLogging(static x => x.AddSerilog()));
ServiceCollection services = new();
services.AddSingleton(new ConsoleRunnerContext(RunnerType.Console, typeof(Program).Assembly.GetName().Name, _informationalVersion));
services.AddYumeCoreServices();
container.Populate(services);
});
}
10 changes: 5 additions & 5 deletions src/YumeChan.Core/CommandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System.Reflection;
using Unity;
using Unity.Microsoft.DependencyInjection;
using DryIoc;
using DryIoc.Microsoft.DependencyInjection;
using YumeChan.Core.Config;
using YumeChan.Core.Services.Formatters;
using YumeChan.PluginBase;
Expand All @@ -36,7 +36,7 @@ public sealed class CommandHandler

private readonly DiscordClient _client;
private readonly IServiceProvider _services;
private readonly IUnityContainer _container;
private readonly IContainer _container;
private readonly NugetPluginsFetcher _pluginsFetcher;
private readonly PluginLifetimeListener _pluginLifetimeListener;
private readonly ILogger<CommandHandler> _logger;
Expand All @@ -52,7 +52,7 @@ static CommandHandler()
}


public CommandHandler(DiscordClient client, ILogger<CommandHandler> logger, IServiceProvider services, IUnityContainer container, NugetPluginsFetcher pluginsFetcher,
public CommandHandler(DiscordClient client, ILogger<CommandHandler> logger, IServiceProvider services, IContainer container, NugetPluginsFetcher pluginsFetcher,
PluginLifetimeListener pluginLifetimeListener, PluginsLoader pluginsLoader)
{
_client = client;
Expand Down Expand Up @@ -121,7 +121,7 @@ public async Task RegisterCommandsAsync()

foreach (DependencyInjectionHandler handler in _pluginsLoader.LoadDependencyInjectionHandlers())
{
_container.AddServices(handler.ConfigureServices(new ServiceCollection()));
_container.Populate(handler.ConfigureServices(new ServiceCollection()));
}

_pluginsLoader.LoadPluginManifests();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public static class YumeCoreDependencyInjectionExtensions
{
public static IServiceCollection AddYumeCoreServices(this IServiceCollection services)
{
services.AddSingleton<YumeCore>();
services.AddSingleton<DiscordClient>(static services => new(new()
{
Intents = DiscordIntents.All,
Expand All @@ -35,9 +36,9 @@ public static IServiceCollection AddYumeCoreServices(this IServiceCollection ser
services.AddSingleton<CommandHandler>();
services.AddSingleton<LavalinkHandler>();
services.AddSingleton<NugetPluginsFetcher>();
services.AddSingleton<PluginsDependenciesManager>();
services.AddSingleton<DiscordBotTokenProvider>();

services.AddSingleton(typeof(JsonConfigProvider<>));
services.AddSingleton(typeof(InterfaceConfigProvider<>));

services.AddSingleton(typeof(IMongoDatabaseProvider<>), typeof(UnifiedDatabaseProvider<>));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System.Diagnostics.CodeAnalysis;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Logging;
using YumeChan.Core.Config;
using YumeChan.Core.Modules;
using YumeChan.PluginBase.Tools;

Expand Down
4 changes: 2 additions & 2 deletions src/YumeChan.Core/Services/Plugins/PluginsLoader.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System.Reflection;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Runtime.Loader;
using System.Security;
using DryIoc;
using Microsoft.Extensions.Logging;
using Unity;
using YumeChan.PluginBase;

namespace YumeChan.Core.Services.Plugins;
Expand Down
4 changes: 1 addition & 3 deletions src/YumeChan.Core/YumeChan.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

<ItemGroup>
<PackageReference Include="Castle.Core" Version="5.1.1" />
<PackageReference Include="DryIoc.Microsoft.DependencyInjection" Version="6.2.0" />
<PackageReference Include="DSharpPlus" Version="4.4.*" />
<PackageReference Include="DSharpPlus.CommandsNext" Version="4.4.*" />
<PackageReference Include="DSharpPlus.Interactivity" Version="4.4.*" />
Expand All @@ -39,9 +40,6 @@
<PackageReference Include="NuGet.Configuration" Version="6.5.0" />
<PackageReference Include="NuGet.Resolver" Version="6.5.0" />
<PackageReference Include="System.Text.Json" Version="6.0.6" />
<PackageReference Include="Unity" Version="5.11.10" />
<PackageReference Include="Nodsoft.Unity.Microsoft.DependencyInjection" Version="5.11.5" />
<PackageReference Include="Unity.Microsoft.Logging" Version="5.11.1" />
</ItemGroup>

<ItemGroup>
Expand Down
15 changes: 3 additions & 12 deletions src/YumeChan.Core/YumeCore.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,9 @@
using DSharpPlus;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Runtime.InteropServices;
using Unity;
using Unity.Microsoft.DependencyInjection;
using DryIoc;
using YumeChan.Core.Config;
using YumeChan.Core.Services;
using YumeChan.Core.Services.Config;
using YumeChan.Core.Services.Plugins;
using YumeChan.PluginBase.Database.MongoDB;
using YumeChan.PluginBase.Database.Postgres;
using YumeChan.PluginBase.Tools;

#nullable enable
namespace YumeChan.Core;
Expand All @@ -34,15 +25,15 @@ public sealed class YumeCore
public DiscordClient Client { get; set; }
public CommandHandler CommandHandler { get; set; }
public LavalinkHandler LavalinkHandler { get; set; }
public IUnityContainer Services { get; set; }
public IContainer Services { get; set; }

internal ILogger<YumeCore> Logger { get; set; }

internal InterfaceConfigProvider<ICoreProperties> ConfigProvider { get; private set; }
public ICoreProperties CoreProperties { get; private set; }


public YumeCore(IUnityContainer services)
public YumeCore(IContainer services)
{
Services = services;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Microsoft.OpenApi.Models;
using Nodsoft.MoltenObsidian.Blazor;
using Nodsoft.MoltenObsidian.Blazor.Services;
using Nodsoft.MoltenObsidian.Vault;
using Swashbuckle.AspNetCore.SwaggerGen;
using YumeChan.NetRunner.Plugins.Infrastructure.Api;
using YumeChan.NetRunner.Plugins.Infrastructure.Swagger;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using Microsoft.AspNetCore.Mvc.ApiExplorer;
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;

namespace YumeChan.NetRunner.Plugins.Infrastructure.Swagger;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Collections;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerUI;
using Microsoft.OpenApi.Models;

namespace YumeChan.NetRunner.Plugins.Infrastructure.Swagger;

Expand Down
1 change: 0 additions & 1 deletion src/YumeChan.NetRunner.Plugins/Services/ApiPluginLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
using Swashbuckle.AspNetCore.SwaggerGen;
using YumeChan.Core;
using YumeChan.Core.Services.Plugins;
using YumeChan.NetRunner.Plugins.Infrastructure;
using YumeChan.NetRunner.Plugins.Infrastructure.Api;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using Nodsoft.MoltenObsidian.Vaults.FileSystem;
using YumeChan.Core.Config;
using YumeChan.Core.Services.Plugins;
using YumeChan.PluginBase;
using YumeChan.PluginBase.Infrastructure;

namespace YumeChan.NetRunner.Plugins.Services.Docs;
Expand Down
1 change: 0 additions & 1 deletion src/YumeChan.NetRunner/App.razor
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
*@

@using YumeChan.NetRunner.Plugins.Infrastructure.Blazor.Router
@using System.Text.RegularExpressions

@* ReSharper disable once Blazor.EditorRequired *@
<HandoffRouter AppAssembly="@typeof(Program).Assembly">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
using Microsoft.AspNetCore.Components;
using System;
using Unity;
using DryIoc;

namespace YumeChan.NetRunner.Infrastructure.Blazor
{
public sealed class ComponentActivator : IComponentActivator
{
private readonly IUnityContainer container;
private readonly IContainer _container;

public ComponentActivator(IUnityContainer container)
public ComponentActivator(IContainer container)
{
this.container = container;
this._container = container;
}

public IComponent CreateInstance(Type type)
{
object component = container.Resolve(type) ?? Activator.CreateInstance(type);
return (IComponent)component;
object component = _container.Resolve(type, IfUnresolved.ReturnDefaultIfNotRegistered) ?? Activator.CreateInstance(type);
return (IComponent)component ?? throw new InvalidOperationException($"Cannot create an instance of {type}.");
}
}
}
1 change: 0 additions & 1 deletion src/YumeChan.NetRunner/Pages/Docs/DocsBrowser.razor
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
@using YumeChan.NetRunner.Plugins.Services.Docs
@using Nodsoft.MoltenObsidian.Vault
@using Nodsoft.MoltenObsidian.Blazor
@using Nodsoft.MoltenObsidian.Blazor.Helpers
@using Nodsoft.MoltenObsidian.Blazor.Templates
@using YumeChan.NetRunner.Shared.Docs
@using System.IO
Expand Down
24 changes: 9 additions & 15 deletions src/YumeChan.NetRunner/Program.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,21 @@
using System.Reflection;
using DryIoc;
using DryIoc.Microsoft.DependencyInjection;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Serilog;
using Serilog.Events;
using Serilog.Extensions.Logging;
using Unity;
using Unity.Microsoft.DependencyInjection;
using Unity.Microsoft.Logging;
using YumeChan.PluginBase.Tools;

namespace YumeChan.NetRunner;

public static class Program
{
private static IUnityContainer _container = new UnityContainer();
private static Container _container = new();

public static async Task Main(string[] args)
{
string informationalVersion = Assembly.GetEntryAssembly()?.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion;
_container.RegisterInstance(new NetRunnerContext(RunnerType.Console, typeof(Program).Assembly.GetName().Name, informationalVersion));


Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
Expand All @@ -43,12 +38,11 @@ public static async Task Main(string[] args)
await host.WaitForShutdownAsync();
}
public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args)
.UseUnityServiceProvider()
.UseServiceProviderFactory(new DryIocServiceProviderFactory())
.ConfigureLogging(x => x.ClearProviders())
.ConfigureWebHostDefaults(webBuilder => webBuilder.UseStartup<Startup>())
.ConfigureContainer<IUnityContainer>((_, container) =>
{
_container = container; // This assignment is necessary, as configuration only affects the child container.
container.AddExtension(new LoggingExtension(new SerilogLoggerFactory()));
});
// .ConfigureContainer<Container>((_, container) => container
// .WithDependencyInjectionAdapter()
// )
;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using DSharpPlus.Entities;
using DSharpPlus;
using DSharpPlus;
using Microsoft.AspNetCore.Authentication;
using System.Security.Claims;
using Microsoft.Extensions.Configuration;
Expand Down
9 changes: 6 additions & 3 deletions src/YumeChan.NetRunner/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.IO;
using System.Reflection;
using AspNet.Security.OAuth.Discord;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
Expand All @@ -7,7 +7,6 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.HttpOverrides;
using Microsoft.AspNetCore.StaticFiles.Infrastructure;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
Expand All @@ -19,10 +18,11 @@
using YumeChan.NetRunner.Plugins.Infrastructure.Api;
using YumeChan.NetRunner.Plugins.Infrastructure.Filesystem;
using YumeChan.NetRunner.Services.Authentication;
using YumeChan.PluginBase.Tools;

namespace YumeChan.NetRunner;

public class Startup
public sealed class Startup
{
public IConfiguration Configuration { get; }

Expand All @@ -36,6 +36,9 @@ public Startup(IConfiguration configuration)
public void ConfigureServices(IServiceCollection services)
{
services.AddYumeCoreServices();

string informationalVersion = Assembly.GetEntryAssembly()?.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion;
services.AddSingleton(new NetRunnerContext(RunnerType.Console, typeof(Program).Assembly.GetName().Name, informationalVersion));

services.AddControllers(builder =>
{
Expand Down
Loading

0 comments on commit ac6376f

Please sign in to comment.