Skip to content

Commit

Permalink
Use keyed DI
Browse files Browse the repository at this point in the history
  • Loading branch information
benjaminpetit committed Nov 3, 2023
1 parent 5013a37 commit dff4ad9
Show file tree
Hide file tree
Showing 115 changed files with 411 additions and 865 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Microsoft.Extensions.Options;
using Orleans.Configuration;
using Orleans.Providers;
using Orleans.Runtime.Hosting;

namespace Orleans.Hosting
{
Expand Down Expand Up @@ -48,13 +49,7 @@ public static IServiceCollection AddDynamoDBGrainStorage(this IServiceCollection
services.AddTransient<IConfigurationValidator>(sp => new DynamoDBGrainStorageOptionsValidator(sp.GetRequiredService<IOptionsMonitor<DynamoDBStorageOptions>>().Get(name), name));
services.ConfigureNamedOptionForLogging<DynamoDBStorageOptions>(name);
services.AddTransient<IPostConfigureOptions<DynamoDBStorageOptions>, DefaultStorageProviderSerializerOptionsConfigurator<DynamoDBStorageOptions>>();
if (string.Equals(name, ProviderConstants.DEFAULT_STORAGE_PROVIDER_NAME, StringComparison.Ordinal))
{
services.TryAddSingleton(sp => sp.GetServiceByName<IGrainStorage>(ProviderConstants.DEFAULT_STORAGE_PROVIDER_NAME));
}

return services.AddSingletonNamedService(name, DynamoDBGrainStorageFactory.Create)
.AddSingletonNamedService(name, (s, n) => (ILifecycleParticipant<ISiloLifecycle>)s.GetRequiredServiceByName<IGrainStorage>(n));
return services.AddGrainStorage(name, DynamoDBGrainStorageFactory.Create);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Orleans.Hosting;
using Orleans.Providers;
using Orleans.Runtime;
using Orleans.Runtime.Hosting;
using Orleans.Storage;

namespace Orleans.Hosting
Expand Down Expand Up @@ -59,14 +60,9 @@ public static IServiceCollection AddAdoNetGrainStorage(this IServiceCollection s
{
configureOptions?.Invoke(services.AddOptions<AdoNetGrainStorageOptions>(name));
services.ConfigureNamedOptionForLogging<AdoNetGrainStorageOptions>(name);
if (string.Equals(name, ProviderConstants.DEFAULT_STORAGE_PROVIDER_NAME, StringComparison.Ordinal))
{
services.TryAddSingleton(sp => sp.GetServiceByName<IGrainStorage>(ProviderConstants.DEFAULT_STORAGE_PROVIDER_NAME));
}
services.AddTransient<IPostConfigureOptions<AdoNetGrainStorageOptions>, DefaultStorageProviderSerializerOptionsConfigurator<AdoNetGrainStorageOptions>>();
services.AddTransient<IConfigurationValidator>(sp => new AdoNetGrainStorageOptionsValidator(sp.GetRequiredService<IOptionsMonitor<AdoNetGrainStorageOptions>>().Get(name), name));
return services.AddSingletonNamedService<IGrainStorage>(name, AdoNetGrainStorageFactory.Create)
.AddSingletonNamedService<ILifecycleParticipant<ISiloLifecycle>>(name, (s, n) => (ILifecycleParticipant<ISiloLifecycle>)s.GetRequiredServiceByName<IGrainStorage>(n));
return services.AddGrainStorage(name, AdoNetGrainStorageFactory.Create);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Orleans.GrainDirectory;
using Orleans.GrainDirectory.AzureStorage;
using Orleans.Runtime;
using Orleans.Runtime.Hosting;

namespace Orleans.Hosting
{
Expand All @@ -22,8 +23,7 @@ internal static IServiceCollection AddAzureTableGrainDirectory(
services
.AddTransient<IConfigurationValidator>(sp => new AzureTableGrainDirectoryOptionsValidator(sp.GetRequiredService<IOptionsMonitor<AzureTableGrainDirectoryOptions>>().Get(name), name))
.ConfigureNamedOptionForLogging<AzureTableGrainDirectoryOptions>(name)
.AddSingletonNamedService<IGrainDirectory>(name, (sp, name) => ActivatorUtilities.CreateInstance<AzureTableGrainDirectory>(sp, sp.GetOptionsByName<AzureTableGrainDirectoryOptions>(name)))
.AddSingletonNamedService<ILifecycleParticipant<ISiloLifecycle>>(name, (s, n) => (ILifecycleParticipant<ISiloLifecycle>)s.GetRequiredServiceByName<IGrainDirectory>(n));
.AddGrainDirectory(name, (sp, name) => ActivatorUtilities.CreateInstance<AzureTableGrainDirectory>(sp, sp.GetOptionsByName<AzureTableGrainDirectoryOptions>(name)));

return services;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Orleans.Hosting;
using Orleans.Providers;
using Orleans.Runtime;
using Orleans.Runtime.Hosting;
using Orleans.Storage;

namespace Orleans.Hosting
Expand Down Expand Up @@ -49,12 +50,7 @@ public static IServiceCollection AddAzureBlobGrainStorage(this IServiceCollectio
services.AddTransient<IConfigurationValidator>(sp => new AzureBlobStorageOptionsValidator(sp.GetRequiredService<IOptionsMonitor<AzureBlobStorageOptions>>().Get(name), name));
services.AddTransient<IPostConfigureOptions<AzureBlobStorageOptions>, DefaultStorageProviderSerializerOptionsConfigurator<AzureBlobStorageOptions>>();
services.ConfigureNamedOptionForLogging<AzureBlobStorageOptions>(name);
if (string.Equals(name, ProviderConstants.DEFAULT_STORAGE_PROVIDER_NAME, StringComparison.Ordinal))
{
services.TryAddSingleton(sp => sp.GetServiceByName<IGrainStorage>(ProviderConstants.DEFAULT_STORAGE_PROVIDER_NAME));
}
return services.AddSingletonNamedService<IGrainStorage>(name, AzureBlobGrainStorageFactory.Create)
.AddSingletonNamedService<ILifecycleParticipant<ISiloLifecycle>>(name, (s, n) => (ILifecycleParticipant<ISiloLifecycle>)s.GetRequiredServiceByName<IGrainStorage>(n));
return services.AddGrainStorage(name, AzureBlobGrainStorageFactory.Create);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Orleans.Configuration;
using Orleans.Providers;
using Orleans.Runtime;
using Orleans.Runtime.Hosting;
using Orleans.Storage;

namespace Orleans.Hosting
Expand Down Expand Up @@ -52,12 +53,8 @@ internal static IServiceCollection AddAzureTableGrainStorage(
services.AddTransient<IConfigurationValidator>(sp => new AzureTableGrainStorageOptionsValidator(sp.GetRequiredService<IOptionsMonitor<AzureTableStorageOptions>>().Get(name), name));
services.AddTransient<IPostConfigureOptions<AzureTableStorageOptions>, DefaultStorageProviderSerializerOptionsConfigurator<AzureTableStorageOptions>>();
services.ConfigureNamedOptionForLogging<AzureTableStorageOptions>(name);
if (string.Equals(name, ProviderConstants.DEFAULT_STORAGE_PROVIDER_NAME, StringComparison.Ordinal))
{
services.TryAddSingleton(sp => sp.GetServiceByName<IGrainStorage>(ProviderConstants.DEFAULT_STORAGE_PROVIDER_NAME));
}
return services.AddSingletonNamedService<IGrainStorage>(name, AzureTableGrainStorageFactory.Create)
.AddSingletonNamedService<ILifecycleParticipant<ISiloLifecycle>>(name, (s, n) => (ILifecycleParticipant<ISiloLifecycle>)s.GetRequiredServiceByName<IGrainStorage>(n));
return services.AddGrainStorage(name, AzureTableGrainStorageFactory.Create);

}
}
}
2 changes: 1 addition & 1 deletion src/Azure/Orleans.Persistence.Cosmos/CosmosGrainStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ public static class CosmosStorageFactory
public static IGrainStorage Create(IServiceProvider services, string name)
{
var optionsMonitor = services.GetRequiredService<IOptionsMonitor<CosmosGrainStorageOptions>>();
var partitionKeyProvider = services.GetServiceByName<IPartitionKeyProvider>(name)
var partitionKeyProvider = services.GetKeyedService<IPartitionKeyProvider>(name)
?? services.GetRequiredService<IPartitionKeyProvider>();
var loggerFactory = services.GetRequiredService<ILoggerFactory>();
var clusterOptions = services.GetRequiredService<IOptions<ClusterOptions>>();
Expand Down
12 changes: 6 additions & 6 deletions src/Azure/Orleans.Persistence.Cosmos/HostingExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Orleans.Storage;
using Orleans.Providers;
using Orleans.Persistence.Cosmos;
using Orleans.Runtime.Hosting;

namespace Orleans.Hosting;

Expand Down Expand Up @@ -36,7 +37,7 @@ public static ISiloBuilder AddCosmosGrainStorage<TPartitionKeyProvider>(
string name,
Action<CosmosGrainStorageOptions> configureOptions) where TPartitionKeyProvider : class, IPartitionKeyProvider
{
builder.Services.AddSingletonNamedService<IPartitionKeyProvider, TPartitionKeyProvider>(name);
builder.Services.AddKeyedSingleton<IPartitionKeyProvider, TPartitionKeyProvider>(name);
builder.Services.AddCosmosGrainStorage(name, configureOptions);
return builder;
}
Expand Down Expand Up @@ -129,7 +130,7 @@ public static ISiloBuilder AddCosmosGrainStorage<TPartitionKeyProvider>(
string name,
Action<OptionsBuilder<CosmosGrainStorageOptions>>? configureOptions = null) where TPartitionKeyProvider : class, IPartitionKeyProvider
{
builder.Services.AddSingletonNamedService<IPartitionKeyProvider, TPartitionKeyProvider>(name);
builder.Services.AddKeyedSingleton<IPartitionKeyProvider, TPartitionKeyProvider>(name);
builder.Services.AddCosmosGrainStorage(name, configureOptions);
return builder;
}
Expand Down Expand Up @@ -162,7 +163,7 @@ public static ISiloBuilder AddCosmosGrainStorage(
{
if (customPartitionKeyProviderType != null)
{
builder.Services.AddSingletonNamedService<IPartitionKeyProvider>(name, customPartitionKeyProviderType);
builder.Services.AddKeyedSingleton(typeof(IPartitionKeyProvider), name, customPartitionKeyProviderType);
}

builder.Services.AddCosmosGrainStorage(name, configureOptions);
Expand Down Expand Up @@ -251,9 +252,8 @@ public static IServiceCollection AddCosmosGrainStorage(
sp.GetService<IOptionsMonitor<CosmosGrainStorageOptions>>()!.Get(name),
name));
services.ConfigureNamedOptionForLogging<CosmosGrainStorageOptions>(name);
services.TryAddSingleton(sp => sp.GetServiceByName<IGrainStorage>(ProviderConstants.DEFAULT_STORAGE_PROVIDER_NAME));
services.TryAddSingleton(sp => sp.GetRequiredKeyedService<IGrainStorage>(ProviderConstants.DEFAULT_STORAGE_PROVIDER_NAME));
services.TryAddSingleton<IPartitionKeyProvider, DefaultPartitionKeyProvider>();
return services.AddSingletonNamedService(name, CosmosStorageFactory.Create)
.AddSingletonNamedService(name, (s, n) => (ILifecycleParticipant<ISiloLifecycle>)s.GetRequiredServiceByName<IGrainStorage>(n));
return services.AddGrainStorage(name, CosmosStorageFactory.Create);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public static AzureQueueAdapterFactory Create(IServiceProvider services, string
{
var azureQueueOptions = services.GetOptionsByName<AzureQueueOptions>(name);
var cacheOptions = services.GetOptionsByName<SimpleQueueCacheOptions>(name);
var dataAdapter = services.GetServiceByName<IQueueDataAdapter<string, IBatchContainer>>(name)
var dataAdapter = services.GetKeyedService<IQueueDataAdapter<string, IBatchContainer>>(name)
?? services.GetService<IQueueDataAdapter<string, IBatchContainer>>();
var factory = ActivatorUtilities.CreateInstance<AzureQueueAdapterFactory>(services, name, azureQueueOptions, cacheOptions, dataAdapter);
factory.Init();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ protected override Task<string[]> GetPartitionIdsAsync()

private IEventHubReceiver EHGeneratorReceiverFactory(EventHubPartitionSettings settings, string offset, ILogger logger)
{
var streamGeneratorFactory = this.serviceProvider.GetServiceByName<Func<StreamId, IStreamDataGenerator<EventData>>>(this.Name)
var streamGeneratorFactory = this.serviceProvider.GetKeyedService<Func<StreamId, IStreamDataGenerator<EventData>>>(this.Name)
?? SimpleStreamEventDataGenerator.CreateFactory(this.serviceProvider);
var generator = new EventHubPartitionDataGenerator(this.ehGeneratorOptions, streamGeneratorFactory, logger);
return new EventHubPartitionGeneratorReceiver(generator);
Expand Down Expand Up @@ -183,7 +183,7 @@ public virtual Task<object> ExecuteCommand(int command, object arg)
var cacheOptions = services.GetOptionsByName<EventHubStreamCachePressureOptions>(name);
var statisticOptions = services.GetOptionsByName<StreamStatisticOptions>(name);
var evictionOptions = services.GetOptionsByName<StreamCacheEvictionOptions>(name);
IEventHubDataAdapter dataAdapter = services.GetServiceByName<IEventHubDataAdapter>(name)
IEventHubDataAdapter dataAdapter = services.GetKeyedService<IEventHubDataAdapter>(name)
?? services.GetService<IEventHubDataAdapter>()
?? ActivatorUtilities.CreateInstance<EventHubDataAdapter>(services);
var factory = ActivatorUtilities.CreateInstance<EventDataGeneratorAdapterFactory>(services, name, generatorOptions, ehOptions, receiverOptions, cacheOptions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public virtual void Init()
//should only need checkpointer on silo side, so move its init logic when it is used
private void InitCheckpointerFactory()
{
this.checkpointerFactory = this.serviceProvider.GetRequiredServiceByName<IStreamQueueCheckpointerFactory>(this.Name);
this.checkpointerFactory = this.serviceProvider.GetRequiredKeyedService<IStreamQueueCheckpointerFactory>(this.Name);
}
/// <summary>
/// Create queue adapter.
Expand Down Expand Up @@ -308,7 +308,7 @@ public static EventHubAdapterFactory Create(IServiceProvider services, string na
var cacheOptions = services.GetOptionsByName<EventHubStreamCachePressureOptions>(name);
var statisticOptions = services.GetOptionsByName<StreamStatisticOptions>(name);
var evictionOptions = services.GetOptionsByName<StreamCacheEvictionOptions>(name);
IEventHubDataAdapter dataAdapter = services.GetServiceByName<IEventHubDataAdapter>(name)
IEventHubDataAdapter dataAdapter = services.GetKeyedService<IEventHubDataAdapter>(name)
?? services.GetService<IEventHubDataAdapter>()
?? ActivatorUtilities.CreateInstance<EventHubDataAdapter>(services);
var factory = ActivatorUtilities.CreateInstance<EventHubAdapterFactory>(services, name, ehOptions, receiverOptions, cacheOptions, evictionOptions, statisticOptions, dataAdapter);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Azure;
using Azure.Core;
using Azure.Messaging.EventHubs;
using Microsoft.Extensions.DependencyInjection;
using Orleans.Runtime;
using Orleans.Streams;
using System;
Expand Down Expand Up @@ -200,7 +201,7 @@ public StreamCheckpointerConfigurationValidator(IServiceProvider services, strin
}
public void ValidateConfiguration()
{
var checkpointerFactory = services.GetServiceByName<IStreamQueueCheckpointerFactory>(this.name);
var checkpointerFactory = services.GetKeyedService<IStreamQueueCheckpointerFactory>(this.name);
if (checkpointerFactory == null)
throw new OrleansConfigurationException($"No IStreamQueueCheckpointer is configured with PersistentStreamProvider {this.name}. Please configure one.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ internal static IServiceCollection AddAzureTableTransactionalStateStorage(this I
configureOptions?.Invoke(services.AddOptions<AzureTableTransactionalStateOptions>(name));
services.AddTransient<IConfigurationValidator>(sp => new AzureTableTransactionalStateOptionsValidator(sp.GetRequiredService<IOptionsMonitor<AzureTableTransactionalStateOptions>>().Get(name), name));

services.TryAddSingleton<ITransactionalStateStorageFactory>(sp => sp.GetServiceByName<ITransactionalStateStorageFactory>(ProviderConstants.DEFAULT_STORAGE_PROVIDER_NAME));
services.AddSingletonNamedService<ITransactionalStateStorageFactory>(name, AzureTableTransactionalStateStorageFactory.Create);
services.AddSingletonNamedService<ILifecycleParticipant<ISiloLifecycle>>(name, (s, n) => (ILifecycleParticipant<ISiloLifecycle>)s.GetRequiredServiceByName<ITransactionalStateStorageFactory>(n));
services.TryAddSingleton<ITransactionalStateStorageFactory>(sp => sp.GetKeyedService<ITransactionalStateStorageFactory>(ProviderConstants.DEFAULT_STORAGE_PROVIDER_NAME));
services.AddKeyedSingleton<ITransactionalStateStorageFactory>(name, (sp, key) => AzureTableTransactionalStateStorageFactory.Create(sp, key as string));
services.AddKeyedSingleton<ILifecycleParticipant<ISiloLifecycle>>(name, (s, n) => (ILifecycleParticipant<ISiloLifecycle>)s.GetRequiredKeyedService<ITransactionalStateStorageFactory>(n));

return services;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public static IClientBuilder AddBroadcastChannel(this IClientBuilder @this, stri
/// <param name="this">The client.</param>
/// <param name="name">The name of the provider</param>
public static IBroadcastChannelProvider GetBroadcastChannelProvider(this IClusterClient @this, string name)
=> @this.ServiceProvider.GetRequiredServiceByName<IBroadcastChannelProvider>(name);
=> @this.ServiceProvider.GetRequiredKeyedService<IBroadcastChannelProvider>(name);

private static void AddBroadcastChannel(this IServiceCollection services, string name, Action<OptionsBuilder<BroadcastChannelOptions>> configureOptions)
{
Expand All @@ -77,8 +77,8 @@ private static void AddBroadcastChannel(this IServiceCollection services, string
.AddSingleton<ImplicitChannelSubscriberTable>()
.AddSingleton<IChannelNamespacePredicateProvider, DefaultChannelNamespacePredicateProvider>()
.AddSingleton<IChannelNamespacePredicateProvider, ConstructorChannelNamespacePredicateProvider>()
.AddSingletonKeyedService<string, IChannelIdMapper, DefaultChannelIdMapper>(DefaultChannelIdMapper.Name)
.AddSingletonNamedService(name, BroadcastChannelProvider.Create);
.AddKeyedSingleton<IChannelIdMapper, DefaultChannelIdMapper>(DefaultChannelIdMapper.Name)
.AddKeyedSingleton(name, (sp, key) => BroadcastChannelProvider.Create(sp, key as string));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections.Immutable;
using System.Linq;
using System.Runtime.InteropServices;
using Microsoft.Extensions.DependencyInjection;
using Orleans.Metadata;
using Orleans.Runtime;

Expand Down Expand Up @@ -90,7 +91,7 @@ private Cache BuildCache(MajorMinorVersion version, ImmutableDictionary<GrainTyp
$"Channel binding for grain type {binding.GrainType} is missing a \"{WellKnownGrainTypeProperties.ChannelIdMapperKey}\" value");
}

var channelIdMapper = _serviceProvider.GetServiceByName<IChannelIdMapper>(string.IsNullOrWhiteSpace(mapperName) ? DefaultChannelIdMapper.Name : mapperName);
var channelIdMapper = _serviceProvider.GetKeyedService<IChannelIdMapper>(string.IsNullOrWhiteSpace(mapperName) ? DefaultChannelIdMapper.Name : mapperName);
var subscriber = new BroadcastChannelSubscriber(binding, channelIdMapper);
newPredicates.Add(new BroadcastChannelSubscriberPredicate(subscriber, predicate));
}
Expand Down
Loading

0 comments on commit dff4ad9

Please sign in to comment.