Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keyed Dictionary Support #15100

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/OrchardCore.Cms.Web/Program.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
using OrchardCore.DependencyInjection;
using OrchardCore.Logging;

var builder = WebApplication.CreateBuilder(args);

builder.Host.UseNLogHost();
builder.Host
.UseOrchardCoreHost()
.UseNLogHost();

builder.Services
.AddOrchardCms()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using OrchardCore.DependencyInjection;

namespace Microsoft.Extensions.DependencyInjection;

public static class ServiceProviderExtensions
{
public static IReadOnlyDictionary<object, TKeyedService> GetKeyedServiceDictionary<TKeyedService>(this IServiceProvider serviceProvider)
{
var keys = serviceProvider.GetRequiredService<Keys<TKeyedService>>();

var keyedDictionary = new Dictionary<object, TKeyedService>(keys.Count);
var keyedDictionaryWrapper = new ReadOnlyDictionary<object, TKeyedService>(keyedDictionary);

foreach (var key in keys)
{
var service = serviceProvider.GetKeyedService<TKeyedService>(key);

keyedDictionary.Add(key, service);
}

return keyedDictionaryWrapper;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Collections.Generic;

namespace OrchardCore.DependencyInjection;

public class Keys<T> : List<object>
{
public Keys(IEnumerable<object> collection) : base(collection)
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using Microsoft.Extensions.Hosting;

namespace OrchardCore.DependencyInjection;

public static class HostBuilderExtensions
{
public static IHostBuilder UseOrchardCoreHost(this IHostBuilder builder)
=> builder.UseServiceProviderFactory(new OrchardCoreServiceProviderFactory());
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;
using Microsoft.Extensions.DependencyInjection;

namespace OrchardCore.DependencyInjection;

public class OrchardCoreServiceProviderFactory : IServiceProviderFactory<IServiceCollection>
{
public IServiceCollection CreateBuilder(IServiceCollection services) => services;

public IServiceProvider CreateServiceProvider(IServiceCollection containerBuilder)
{
var keyedDictionary = new Dictionary<Type, List<object>>();

foreach (var service in containerBuilder)
{
if (service.ServiceKey != null)
{
if (!keyedDictionary.TryGetValue(service.ServiceType, out var keysList))
{
keysList = [];
keyedDictionary[service.ServiceType] = keysList;
}

keysList.Add(service.ServiceKey);
}
}

AddKeysService(containerBuilder, keyedDictionary);

containerBuilder.AddSingleton(typeof(Keys<>));

return containerBuilder.BuildServiceProvider();
}

private static void AddKeysService(IServiceCollection services, IDictionary<Type, List<object>> keyedDictionary)
{
foreach (var keyedDictionaryItem in keyedDictionary)
{
var serviceType = typeof(Keys<>).MakeGenericType(keyedDictionaryItem.Key);
var service = Activator.CreateInstance(serviceType, keyedDictionaryItem.Value);

services.AddSingleton(serviceType, service!);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
namespace OrchardCore.DependencyInjection.Tests;

public class ServiceProviderExtensionsTests
{
[Fact]
public void ResolveKeyedServiceDictionary()
{
// Arrange
var services = new ServiceCollection();
services.AddSingleton(new FooService());
services.AddKeyedSingleton("foo", new FooService());
services.AddKeyedSingleton("bar", new FooService());
services.AddKeyedSingleton("baz", new FooService());

var serviceProviderFactory = new OrchardCoreServiceProviderFactory();
var serviceProvider = serviceProviderFactory.CreateServiceProvider(services);

// Act
var keyedDictionary = serviceProvider.GetKeyedServiceDictionary<FooService>();

// Assert
Assert.Equal(3, keyedDictionary.Count);
Assert.Contains(keyedDictionary, item => item.Key.Equals("foo"));
Assert.Contains(keyedDictionary, item => item.Key.Equals("bar"));
Assert.Contains(keyedDictionary, item => item.Key.Equals("baz"));
Assert.IsType<FooService>(keyedDictionary["foo"]);
Assert.IsType<FooService>(keyedDictionary["bar"]);
Assert.IsType<FooService>(keyedDictionary["baz"]);
}
}
5 changes: 5 additions & 0 deletions test/OrchardCore.Tests/DependencyInjection/FooService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace OrchardCore.DependencyInjection.Tests;

internal class FooService
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
namespace OrchardCore.DependencyInjection.Tests;

public class OrchardCoreServiceProviderFactoryTests
{
[Fact]
public void ResolveKeys()
{
// Arrange
var services = new ServiceCollection();
services.AddSingleton(new FooService());
services.AddKeyedSingleton("foo", new FooService());
services.AddKeyedSingleton("bar", new FooService());
services.AddKeyedSingleton("baz", new FooService());

// Act
var serviceProviderFactory = new OrchardCoreServiceProviderFactory();
var serviceProvider = serviceProviderFactory.CreateServiceProvider(services);

// Assert
var keys = serviceProvider.GetRequiredService<Keys<FooService>>();
Assert.Equal(3, keys.Count);
Assert.Equal("foo", keys[0]);
Assert.Equal("bar", keys[1]);
Assert.Equal("baz", keys[2]);
}
}