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

Cosmos DB: support custom partition key value providers #8694

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/Azure/Orleans.Persistence.Cosmos/CosmosGrainStorage.cs
Expand Up @@ -426,6 +426,16 @@ public static class CosmosStorageFactory
public static IGrainStorage Create(IServiceProvider services, string name)
{
var optionsMonitor = services.GetRequiredService<IOptionsMonitor<CosmosGrainStorageOptions>>();
return ActivatorUtilities.CreateInstance<CosmosGrainStorage>(services, name, optionsMonitor.Get(name));
var partitionKeyProvider = services.GetServiceByName<IPartitionKeyProvider>(name)
?? services.GetRequiredService<IPartitionKeyProvider>();
var loggerFactory = services.GetRequiredService<ILoggerFactory>();
var clusterOptions = services.GetRequiredService<IOptions<ClusterOptions>>();
return new CosmosGrainStorage(
name,
optionsMonitor.Get(name),
loggerFactory,
services,
clusterOptions,
partitionKeyProvider);
}
}
6 changes: 3 additions & 3 deletions src/Azure/Orleans.Persistence.Cosmos/HostingExtensions.cs
Expand Up @@ -36,7 +36,7 @@ public static class HostingExtensions
string name,
Action<CosmosGrainStorageOptions> configureOptions) where TPartitionKeyProvider : class, IPartitionKeyProvider
{
builder.Services.TryAddSingleton<IPartitionKeyProvider, TPartitionKeyProvider>();
builder.Services.AddSingletonNamedService<IPartitionKeyProvider, TPartitionKeyProvider>(name);
builder.Services.AddCosmosGrainStorage(name, configureOptions);
return builder;
}
Expand Down Expand Up @@ -129,7 +129,7 @@ public static class HostingExtensions
string name,
Action<OptionsBuilder<CosmosGrainStorageOptions>>? configureOptions = null) where TPartitionKeyProvider : class, IPartitionKeyProvider
{
builder.Services.TryAddSingleton<IPartitionKeyProvider, TPartitionKeyProvider>();
builder.Services.AddSingletonNamedService<IPartitionKeyProvider, TPartitionKeyProvider>(name);
builder.Services.AddCosmosGrainStorage(name, configureOptions);
return builder;
}
Expand Down Expand Up @@ -162,7 +162,7 @@ public static class HostingExtensions
{
if (customPartitionKeyProviderType != null)
{
builder.Services.TryAddSingleton(typeof(IPartitionKeyProvider), customPartitionKeyProviderType);
builder.Services.AddSingletonNamedService<IPartitionKeyProvider>(name, customPartitionKeyProviderType);
}

builder.Services.AddCosmosGrainStorage(name, configureOptions);
Expand Down
9 changes: 9 additions & 0 deletions src/Orleans.Core/Utils/KeyedService.cs
Expand Up @@ -205,6 +205,15 @@ public static IServiceCollection AddSingletonNamedService<TService>(this IServic
return collection.AddSingletonKeyedService<string, TService>(name, factory);
}

/// <summary>
/// Register a singleton named service
/// </summary>
public static IServiceCollection AddSingletonNamedService<TService>(this IServiceCollection collection, string name, Type implementationType)
where TService : class
{
return collection.AddSingletonKeyedService<string, TService>(name, (sp, name) => (TService)ActivatorUtilities.CreateInstance(sp, implementationType));
}

/// <summary>
/// Register a singleton named service
/// </summary>
Expand Down