diff --git a/Orleans.Persistence.Redis/Config/RedisSiloHostBuilderExtensions.cs b/Orleans.Persistence.Redis/Config/RedisSiloHostBuilderExtensions.cs index 9bc91c5..7114c01 100644 --- a/Orleans.Persistence.Redis/Config/RedisSiloHostBuilderExtensions.cs +++ b/Orleans.Persistence.Redis/Config/RedisSiloHostBuilderExtensions.cs @@ -13,209 +13,142 @@ using Orleans.Runtime; using Orleans.Serialization; using Orleans.Storage; -using System; using JsonSerializer = Orleans.Persistence.Redis.Serialization.JsonSerializer; using OrleansSerializer = Orleans.Persistence.Redis.Serialization.OrleansSerializer; // ReSharper disable once CheckNamespace -namespace Orleans.Hosting +namespace Orleans.Hosting; + +public static class RedisSiloBuilderExtensions { - public static class RedisSiloBuilderExtensions + 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> configureOptions = null + ) { - 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(name); - - internal static ISiloBuilder AddRedisDefaultHumanReadableSerializer(this ISiloBuilder builder, string name) - => builder.AddRedisHumanReadableSerializer( - name, - provider => new object[] { RedisDefaultJsonSerializerSettings.Get(provider) } - ); - - internal static ISiloBuilder AddCompression(this ISiloBuilder builder, string name) - where TCompression : ICompression - => builder.ConfigureServices(services => - services.AddSingletonNamedService(name, (provider, n) - => ActivatorUtilities.CreateInstance(provider))); - - internal static ISiloBuilder AddRedisSerializer( - this ISiloBuilder builder, - string name, - params object[] settings - ) - where TSerializer : ISerializer - => builder.ConfigureServices(services => - services.AddSingletonNamedService(name, (provider, n) - => ActivatorUtilities.CreateInstance(provider, settings)) - ); - - internal static ISiloBuilder AddRedisHumanReadableSerializer( - this ISiloBuilder builder, - string name, - params object[] settings - ) - where TSerializer : IHumanReadableSerializer - => builder.ConfigureServices(services => - services.AddSingletonNamedService(name, (provider, n) - => ActivatorUtilities.CreateInstance(provider, settings)) - ); - - internal static ISiloBuilder AddRedisHumanReadableSerializer( - this ISiloBuilder builder, - string name, - Func cfg - ) where TSerializer : IHumanReadableSerializer - => builder.ConfigureServices(services => - services.AddSingletonNamedService(name, (provider, n) - => ActivatorUtilities.CreateInstance(provider, cfg?.Invoke(provider))) - ); + configureOptions?.Invoke(services.AddOptions(name)); + // services.AddTransient(sp => new DynamoDBGrainStorageOptionsValidator(sp.GetService>().Get(name), name)); + services.AddSingletonNamedService(name, CreateStateStore); + services.ConfigureNamedOptionForLogging(name); + services.TryAddSingleton(sp => + sp.GetServiceByName(ProviderConstants.DEFAULT_STORAGE_PROVIDER_NAME)); + + return services + .AddSingletonNamedService(name, CreateDbConnection) + .AddSingletonNamedService(name, CreateRedisStorage) + .AddSingletonNamedService(name, (provider, n) + => (ILifecycleParticipant)provider.GetRequiredServiceByName(n)); } - public static class RedisSiloHostBuilderExtensions + internal static ISiloBuilder AddRedisDefaultSerializer(this ISiloBuilder builder, string name) + => builder.AddRedisSerializer(name); + + internal static ISiloBuilder AddRedisDefaultBrotliSerializer(this ISiloBuilder builder, string name) + => builder.AddRedisSerializer(name); + + internal static ISiloBuilder AddRedisDefaultHumanReadableSerializer(this ISiloBuilder builder, string name) + => builder.AddRedisHumanReadableSerializer( + name, + provider => new object[] { RedisDefaultJsonSerializerSettings.Get(provider) } + ); + + internal static ISiloBuilder AddCompression(this ISiloBuilder builder, string name) + where TCompression : ICompression + => builder.ConfigureServices(services => + services.AddSingletonNamedService(name, (provider, n) + => ActivatorUtilities.CreateInstance(provider))); + + internal static ISiloBuilder AddRedisSerializer( + this ISiloBuilder builder, + string name, + params object[] settings + ) + where TSerializer : ISerializer + => builder.ConfigureServices(services => + services.AddSingletonNamedService(name, (provider, n) + => ActivatorUtilities.CreateInstance(provider, settings)) + ); + + internal static ISiloBuilder AddRedisHumanReadableSerializer( + this ISiloBuilder builder, + string name, + params object[] settings + ) + where TSerializer : IHumanReadableSerializer + => builder.ConfigureServices(services => + services.AddSingletonNamedService(name, (provider, n) + => ActivatorUtilities.CreateInstance(provider, settings)) + ); + + internal static ISiloBuilder AddRedisHumanReadableSerializer( + this ISiloBuilder builder, + string name, + Func cfg + ) where TSerializer : IHumanReadableSerializer + => builder.ConfigureServices(services => + services.AddSingletonNamedService(name, (provider, n) + => ActivatorUtilities.CreateInstance(provider, cfg?.Invoke(provider))) + ); + + private static IGrainStorage CreateRedisStorage(IServiceProvider services, string name) { - 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> configureOptions = null - ) - { - configureOptions?.Invoke(services.AddOptions(name)); - // services.AddTransient(sp => new DynamoDBGrainStorageOptionsValidator(sp.GetService>().Get(name), name)); - services.AddSingletonNamedService(name, CreateStateStore); - services.ConfigureNamedOptionForLogging(name); - services.TryAddSingleton(sp => - sp.GetServiceByName(ProviderConstants.DEFAULT_STORAGE_PROVIDER_NAME)); - - return services - .AddSingletonNamedService(name, CreateDbConnection) - .AddSingletonNamedService(name, CreateRedisStorage) - .AddSingletonNamedService(name, (provider, n) - => (ILifecycleParticipant)provider.GetRequiredServiceByName(n)); - } - - internal static ISiloHostBuilder AddRedisDefaultSerializer(this ISiloHostBuilder builder, string name) - => builder.AddRedisSerializer(name); - - internal static ISiloHostBuilder AddRedisDefaultBrotliSerializer(this ISiloHostBuilder builder, string name) - => builder.AddRedisSerializer(name); - - internal static ISiloHostBuilder AddRedisDefaultHumanReadableSerializer(this ISiloHostBuilder builder, - string name) - => builder.AddRedisHumanReadableSerializer( - name, - provider => new object[] { RedisDefaultJsonSerializerSettings.Get(provider) }); - - internal static ISiloHostBuilder AddRedisSerializer(this ISiloHostBuilder builder, string name, - params object[] settings) - where TSerializer : ISerializer - => builder.ConfigureServices(services => - services.AddSingletonNamedService(name, (provider, n) - => ActivatorUtilities.CreateInstance(provider, settings)) - ); - - internal static ISiloHostBuilder AddRedisHumanReadableSerializer(this ISiloHostBuilder builder, - string name, params object[] settings) - where TSerializer : IHumanReadableSerializer - => builder.ConfigureServices(services => - services.AddSingletonNamedService(name, (provider, n) - => ActivatorUtilities.CreateInstance(provider, settings)) - ); - - internal static ISiloHostBuilder AddRedisHumanReadableSerializer( - this ISiloHostBuilder builder, - string name, - Func cfg - ) where TSerializer : IHumanReadableSerializer - => builder.ConfigureServices(services => - services.AddSingletonNamedService(name, (provider, n) - => ActivatorUtilities.CreateInstance(provider, cfg?.Invoke(provider))) - ); - - internal static ISiloHostBuilder AddCompression(this ISiloHostBuilder builder, string name) - where TCompression : ICompression - { - return builder.ConfigureServices(services => - services.AddSingletonNamedService(name, (provider, n) - => ActivatorUtilities.CreateInstance(provider))); - } - - private static IGrainStorage CreateRedisStorage(IServiceProvider services, string name) - { - var store = services.GetRequiredServiceByName(name); - var connection = services.GetRequiredServiceByName(name); - return ActivatorUtilities.CreateInstance(services, name, store, connection); - } + var store = services.GetRequiredServiceByName(name); + var connection = services.GetRequiredServiceByName(name); + return ActivatorUtilities.CreateInstance(services, name, store, connection); + } - private static IGrainStateStore CreateStateStore(IServiceProvider provider, string name) - { + private static IGrainStateStore CreateStateStore(IServiceProvider provider, string name) + { var connection = provider.GetRequiredServiceByName(name); var serializer = provider.GetRequiredServiceByName(name); var humanReadableSerializer = provider.GetServiceByName(name); var options = provider.GetRequiredService>(); - var compress = provider.GetServiceByName(name); - - if (compress != null) - return ActivatorUtilities.CreateInstance( - provider, - connection, - options.Get(name), - serializer, - humanReadableSerializer, - compress - ); - - return ActivatorUtilities.CreateInstance( - provider, + var logger = provider.GetRequiredService>(); + + return ActivatorUtilities.CreateInstance( + provider, + name, connection, options.Get(name), serializer, - humanReadableSerializer - ); - } + humanReadableSerializer, + logger, + provider + ); + } - private static DbConnection CreateDbConnection(IServiceProvider provider, string name) - { - var optionsSnapshot = provider.GetRequiredService>(); - var logger = provider.GetRequiredService>(); - return ActivatorUtilities.CreateInstance(provider, optionsSnapshot.Get(name), logger); - } + private static DbConnection CreateDbConnection(IServiceProvider provider, string name) + { + var optionsSnapshot = provider.GetRequiredService>(); + var logger = provider.GetRequiredService>(); + return ActivatorUtilities.CreateInstance(provider, optionsSnapshot.Get(name), logger); } +} - public static class RedisDefaultJsonSerializerSettings +public static class RedisDefaultJsonSerializerSettings +{ + public static JsonSerializerSettings Get(IServiceProvider provider) { - public static JsonSerializerSettings Get(IServiceProvider provider) - { - var settings = OrleansJsonSerializer.GetDefaultSerializerSettings( - provider.GetRequiredService(), - provider.GetRequiredService() - ); + var settings = OrleansJsonSerializerSettings.GetDefaultSerializerSettings(provider); - settings.ContractResolver = new DefaultContractResolver + settings.ContractResolver = new DefaultContractResolver + { + NamingStrategy = new DefaultNamingStrategy { - NamingStrategy = new DefaultNamingStrategy - { - ProcessDictionaryKeys = false - } - }; - - return settings; - } + ProcessDictionaryKeys = false + } + }; + + return settings; } } \ No newline at end of file diff --git a/Orleans.Persistence.Redis/Core/GrainStateStore.cs b/Orleans.Persistence.Redis/Core/GrainStateStore.cs index 6bd7241..92a957e 100644 --- a/Orleans.Persistence.Redis/Core/GrainStateStore.cs +++ b/Orleans.Persistence.Redis/Core/GrainStateStore.cs @@ -4,6 +4,7 @@ using Orleans.Persistence.Redis.Config; using Orleans.Persistence.Redis.Serialization; using Orleans.Persistence.Redis.Utils; +using Orleans.Runtime; using Orleans.Storage; using StackExchange.Redis; using System; @@ -30,22 +31,13 @@ internal class GrainStateStore : IGrainStateStore private readonly ICompression _compression; public GrainStateStore( - DbConnection connection, - RedisStorageOptions options, - ISerializer serializer, - IHumanReadableSerializer humanReadableSerializer, - ILogger logger - ) : this(connection, options, serializer, humanReadableSerializer, logger, null) - { - } - - public GrainStateStore( + string name, DbConnection connection, RedisStorageOptions options, ISerializer serializer, IHumanReadableSerializer humanReadableSerializer, ILogger logger, - ICompression compression + IServiceProvider services ) { _connection = connection; @@ -53,7 +45,7 @@ ICompression compression _humanReadableSerializer = humanReadableSerializer; _logger = logger; _options = options; - _compression = compression; + _compression = services.GetServiceByName(name); } public async Task GetGrainState(string grainId, Type stateType) diff --git a/package.json b/package.json index 3218354..99a02d7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "sucrose.orleans.persistence.redis", - "version": "1.5.0", + "version": "2.2.2", "description": "Orleans Redis Persistence Provider", "solution": "Orleans.Persistence.Redis-build.sln", "dependencies": {},