Skip to content

Commit

Permalink
fix(build): RedisSiloBuilderExtensions
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathansant committed Feb 2, 2024
1 parent 5ec6ed8 commit 81d6e1d
Show file tree
Hide file tree
Showing 2 changed files with 170 additions and 110 deletions.
278 changes: 169 additions & 109 deletions Orleans.Persistence.Redis/Config/RedisSiloHostBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.Extensions.DependencyInjection;
using System;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
Expand All @@ -17,138 +18,197 @@
using OrleansSerializer = Orleans.Persistence.Redis.Serialization.OrleansSerializer;

// ReSharper disable once CheckNamespace
namespace Orleans.Hosting;

public static class RedisSiloBuilderExtensions
namespace Orleans.Hosting
{
public static RedisStorageOptionsBuilder AddRedisGrainStorage(
this ISiloBuilder builder,
string name
) => new RedisStorageOptionsBuilder(builder, name);

public static RedisStorageOptionsBuilder AddRedisGrainStorageAsDefault(
this ISiloBuilder builder
) => builder.AddRedisGrainStorage("Default");

internal static IServiceCollection AddRedisGrainStorage(
this IServiceCollection services,
string name,
Action<OptionsBuilder<RedisStorageOptions>> configureOptions = null
)
public static class RedisSiloBuilderExtensions
{
configureOptions?.Invoke(services.AddOptions<RedisStorageOptions>(name));
// services.AddTransient<IConfigurationValidator>(sp => new DynamoDBGrainStorageOptionsValidator(sp.GetService<IOptionsSnapshot<RedisStorageOptions>>().Get(name), name));
services.AddSingletonNamedService(name, CreateStateStore);
services.ConfigureNamedOptionForLogging<RedisStorageOptions>(name);
services.TryAddSingleton(sp =>
sp.GetServiceByName<IGrainStorage>(ProviderConstants.DEFAULT_STORAGE_PROVIDER_NAME));

return services
.AddSingletonNamedService(name, CreateDbConnection)
.AddSingletonNamedService(name, CreateRedisStorage)
.AddSingletonNamedService(name, (provider, n)
=> (ILifecycleParticipant<ISiloLifecycle>)provider.GetRequiredServiceByName<IGrainStorage>(n));
public static RedisStorageOptionsBuilder AddRedisGrainStorage(
this ISiloBuilder builder,
string name
) => new RedisStorageOptionsBuilder(builder, name);

public static RedisStorageOptionsBuilder AddRedisGrainStorageAsDefault(
this ISiloBuilder builder
) => builder.AddRedisGrainStorage("Default");

internal static ISiloBuilder AddRedisDefaultSerializer(this ISiloBuilder builder, string name)
=> builder.AddRedisSerializer<OrleansSerializer>(name);

internal static ISiloBuilder AddRedisDefaultHumanReadableSerializer(this ISiloBuilder builder, string name)
=> builder.AddRedisHumanReadableSerializer<JsonSerializer>(
name,
provider => new object[] { RedisDefaultJsonSerializerSettings.Get(provider) }
);

internal static ISiloBuilder AddCompression<TCompression>(this ISiloBuilder builder, string name)
where TCompression : ICompression
=> builder.ConfigureServices(services =>
services.AddSingletonNamedService<ICompression>(name, (provider, n)
=> ActivatorUtilities.CreateInstance<TCompression>(provider)));

internal static ISiloBuilder AddRedisSerializer<TSerializer>(
this ISiloBuilder builder,
string name,
params object[] settings
)
where TSerializer : ISerializer
=> builder.ConfigureServices(services =>
services.AddSingletonNamedService<ISerializer>(name, (provider, n)
=> ActivatorUtilities.CreateInstance<TSerializer>(provider, settings))
);

internal static ISiloBuilder AddRedisHumanReadableSerializer<TSerializer>(
this ISiloBuilder builder,
string name,
params object[] settings
)
where TSerializer : IHumanReadableSerializer
=> builder.ConfigureServices(services =>
services.AddSingletonNamedService<IHumanReadableSerializer>(name, (provider, n)
=> ActivatorUtilities.CreateInstance<TSerializer>(provider, settings))
);

internal static ISiloBuilder AddRedisHumanReadableSerializer<TSerializer>(
this ISiloBuilder builder,
string name,
Func<IServiceProvider, object[]> cfg
) where TSerializer : IHumanReadableSerializer
=> builder.ConfigureServices(services =>
services.AddSingletonNamedService<IHumanReadableSerializer>(name, (provider, n)
=> ActivatorUtilities.CreateInstance<TSerializer>(provider, cfg?.Invoke(provider)))
);
}

internal static ISiloBuilder AddRedisDefaultSerializer(this ISiloBuilder builder, string name)
=> builder.AddRedisSerializer<OrleansSerializer>(name);

internal static ISiloBuilder AddRedisDefaultBrotliSerializer(this ISiloBuilder builder, string name)
=> builder.AddRedisSerializer<BrotliSerializer>(name);

internal static ISiloBuilder AddRedisDefaultHumanReadableSerializer(this ISiloBuilder builder, string name)
=> builder.AddRedisHumanReadableSerializer<JsonSerializer>(
name,
provider => new object[] { RedisDefaultJsonSerializerSettings.Get(provider) }
);

internal static ISiloBuilder AddCompression<TCompression>(this ISiloBuilder builder, string name)
where TCompression : ICompression
=> builder.ConfigureServices(services =>
services.AddSingletonNamedService<ICompression>(name, (provider, n)
=> ActivatorUtilities.CreateInstance<TCompression>(provider)));

internal static ISiloBuilder AddRedisSerializer<TSerializer>(
this ISiloBuilder builder,
string name,
params object[] settings
)
where TSerializer : ISerializer
=> builder.ConfigureServices(services =>
services.AddSingletonNamedService<ISerializer>(name, (provider, n)
=> ActivatorUtilities.CreateInstance<TSerializer>(provider, settings))
);

internal static ISiloBuilder AddRedisHumanReadableSerializer<TSerializer>(
this ISiloBuilder builder,
string name,
params object[] settings
)
where TSerializer : IHumanReadableSerializer
=> builder.ConfigureServices(services =>
services.AddSingletonNamedService<IHumanReadableSerializer>(name, (provider, n)
=> ActivatorUtilities.CreateInstance<TSerializer>(provider, settings))
);

internal static ISiloBuilder AddRedisHumanReadableSerializer<TSerializer>(
this ISiloBuilder builder,
string name,
Func<IServiceProvider, object[]> cfg
) where TSerializer : IHumanReadableSerializer
=> builder.ConfigureServices(services =>
services.AddSingletonNamedService<IHumanReadableSerializer>(name, (provider, n)
=> ActivatorUtilities.CreateInstance<TSerializer>(provider, cfg?.Invoke(provider)))
);

private static IGrainStorage CreateRedisStorage(IServiceProvider services, string name)
public static class RedisSiloHostBuilderExtensions
{
var store = services.GetRequiredServiceByName<IGrainStateStore>(name);
var connection = services.GetRequiredServiceByName<DbConnection>(name);
return ActivatorUtilities.CreateInstance<RedisGrainStorage>(services, name, store, connection);
}
public static RedisStorageSiloHostBuilderOptionsBuilder AddRedisGrainStorage(
this ISiloHostBuilder builder,
string name
) => new RedisStorageSiloHostBuilderOptionsBuilder(builder, name);

public static RedisStorageSiloHostBuilderOptionsBuilder AddRedisGrainStorageAsDefault(
this ISiloHostBuilder builder
) => builder.AddRedisGrainStorage("Default");

internal static IServiceCollection AddRedisGrainStorage(
this IServiceCollection services,
string name,
Action<OptionsBuilder<RedisStorageOptions>> configureOptions = null
)
{
configureOptions?.Invoke(services.AddOptions<RedisStorageOptions>(name));
// services.AddTransient<IConfigurationValidator>(sp => new DynamoDBGrainStorageOptionsValidator(sp.GetService<IOptionsSnapshot<RedisStorageOptions>>().Get(name), name));
services.AddSingletonNamedService(name, CreateStateStore);
services.ConfigureNamedOptionForLogging<RedisStorageOptions>(name);
services.TryAddSingleton(sp =>
sp.GetServiceByName<IGrainStorage>(ProviderConstants.DEFAULT_STORAGE_PROVIDER_NAME));

return services
.AddSingletonNamedService(name, CreateDbConnection)
.AddSingletonNamedService(name, CreateRedisStorage)
.AddSingletonNamedService(name, (provider, n)
=> (ILifecycleParticipant<ISiloLifecycle>) provider.GetRequiredServiceByName<IGrainStorage>(n));
}

internal static ISiloHostBuilder AddRedisDefaultSerializer(this ISiloHostBuilder builder, string name)
=> builder.AddRedisSerializer<OrleansSerializer>(name);

internal static ISiloHostBuilder AddRedisDefaultBrotliSerializer(this ISiloHostBuilder builder, string name)
=> builder.AddRedisSerializer<BrotliSerializer>(name);

internal static ISiloHostBuilder AddRedisDefaultHumanReadableSerializer(this ISiloHostBuilder builder,
string name)
=> builder.AddRedisHumanReadableSerializer<JsonSerializer>(
name,
provider => new object[] { RedisDefaultJsonSerializerSettings.Get(provider) });

internal static ISiloHostBuilder AddRedisSerializer<TSerializer>(this ISiloHostBuilder builder, string name,
params object[] settings)
where TSerializer : ISerializer
=> builder.ConfigureServices(services =>
services.AddSingletonNamedService<ISerializer>(name, (provider, n)
=> ActivatorUtilities.CreateInstance<TSerializer>(provider, settings))
);

internal static ISiloHostBuilder AddRedisHumanReadableSerializer<TSerializer>(this ISiloHostBuilder builder,
string name, params object[] settings)
where TSerializer : IHumanReadableSerializer
=> builder.ConfigureServices(services =>
services.AddSingletonNamedService<IHumanReadableSerializer>(name, (provider, n)
=> ActivatorUtilities.CreateInstance<TSerializer>(provider, settings))
);

internal static ISiloHostBuilder AddRedisHumanReadableSerializer<TSerializer>(
this ISiloHostBuilder builder,
string name,
Func<IServiceProvider, object[]> cfg
) where TSerializer : IHumanReadableSerializer
=> builder.ConfigureServices(services =>
services.AddSingletonNamedService<IHumanReadableSerializer>(name, (provider, n)
=> ActivatorUtilities.CreateInstance<TSerializer>(provider, cfg?.Invoke(provider)))
);

internal static ISiloHostBuilder AddCompression<TCompression>(this ISiloHostBuilder builder, string name)
where TCompression : ICompression
{
return builder.ConfigureServices(services =>
services.AddSingletonNamedService<ICompression>(name, (provider, n)
=> ActivatorUtilities.CreateInstance<TCompression>(provider)));
}

private static IGrainStateStore CreateStateStore(IServiceProvider provider, string name)
{
private static IGrainStorage CreateRedisStorage(IServiceProvider services, string name)
{
var store = services.GetRequiredServiceByName<IGrainStateStore>(name);
var connection = services.GetRequiredServiceByName<DbConnection>(name);
return ActivatorUtilities.CreateInstance<RedisGrainStorage>(services, name, store, connection);
}

private static IGrainStateStore CreateStateStore(IServiceProvider provider, string name)
{
var connection = provider.GetRequiredServiceByName<DbConnection>(name);
var serializer = provider.GetRequiredServiceByName<ISerializer>(name);
var humanReadableSerializer = provider.GetServiceByName<IHumanReadableSerializer>(name);
var options = provider.GetRequiredService<IOptionsSnapshot<RedisStorageOptions>>();
var logger = provider.GetRequiredService<ILogger<GrainStateStore>>();

return ActivatorUtilities.CreateInstance<GrainStateStore>(
provider,
return ActivatorUtilities.CreateInstance<GrainStateStore>(
provider,
name,
connection,
options.Get(name),
serializer,
humanReadableSerializer,
logger,
provider
);
}
);
}

private static DbConnection CreateDbConnection(IServiceProvider provider, string name)
{
var optionsSnapshot = provider.GetRequiredService<IOptionsSnapshot<RedisStorageOptions>>();
var logger = provider.GetRequiredService<ILogger<DbConnection>>();
return ActivatorUtilities.CreateInstance<DbConnection>(provider, optionsSnapshot.Get(name), logger);
private static DbConnection CreateDbConnection(IServiceProvider provider, string name)
{
var optionsSnapshot = provider.GetRequiredService<IOptionsSnapshot<RedisStorageOptions>>();
var logger = provider.GetRequiredService<ILogger<DbConnection>>();
return ActivatorUtilities.CreateInstance<DbConnection>(provider, optionsSnapshot.Get(name), logger);
}
}
}

public static class RedisDefaultJsonSerializerSettings
{
public static JsonSerializerSettings Get(IServiceProvider provider)
public static class RedisDefaultJsonSerializerSettings
{
var settings = OrleansJsonSerializerSettings.GetDefaultSerializerSettings(provider);

settings.ContractResolver = new DefaultContractResolver
public static JsonSerializerSettings Get(IServiceProvider provider)
{
NamingStrategy = new DefaultNamingStrategy
{
ProcessDictionaryKeys = false
}
};
var settings = OrleansJsonSerializer.GetDefaultSerializerSettings(
provider.GetRequiredService<ITypeResolver>(),
provider.GetRequiredService<IGrainFactory>()
);

return settings;
settings.ContractResolver = new DefaultContractResolver
{
NamingStrategy = new DefaultNamingStrategy
{
ProcessDictionaryKeys = false
}
};

return settings;
}
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sucrose.orleans.persistence.redis",
"version": "2.2.2",
"version": "1.6.0",
"description": "Orleans Redis Persistence Provider",
"solution": "Orleans.Persistence.Redis-build.sln",
"dependencies": {},
Expand Down

0 comments on commit 81d6e1d

Please sign in to comment.