using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.Options; using Orleans; using Orleans.Providers; using Orleans.Runtime; using Orleans.Storage; using Pole.Orleans.Provider.EntityframeworkCore.Conventions; using System; using System.Collections.Generic; using System.Text; namespace Pole.Orleans.Provider.EntityframeworkCore { public static class GrainStorageServiceCollectionExtensions { public static IServiceCollection ConfigureGrainStorageOptions( this IServiceCollection services, Action> configureOptions = null) where TContext : DbContext where TGrain : Grain where TEntity : class, new() { return services .AddSingleton>, GrainStoragePostConfigureOptions>() .Configure>(typeof(TGrain).FullName, options => { configureOptions?.Invoke(options); }); } public static IServiceCollection ConfigureGrainStorageOptions( this IServiceCollection services, Action> configureOptions = null) where TContext : DbContext where TGrain : Grain where TGrainState : new() where TEntity : class { return services .AddSingleton>, GrainStoragePostConfigureOptions>() .Configure>(typeof(TGrain).FullName, options => { configureOptions?.Invoke(options); }); } public static IServiceCollection AddEfGrainStorage( this IServiceCollection services, string providerName = ProviderConstants.DEFAULT_STORAGE_PROVIDER_NAME) where TContext : DbContext { services.TryAddSingleton(typeof(IEntityTypeResolver), typeof(EntityTypeResolver)); services.TryAddSingleton(typeof(IGrainStorageConvention), typeof(GrainStorageConvention)); services.TryAddSingleton(typeof(IGrainStateEntryConfigurator<,,>), typeof(DefaultGrainStateEntryConfigurator<,,>)); services.AddSingleton(typeof(EntityFrameworkGrainStorage)); services.TryAddSingleton(sp => sp.GetServiceByName(ProviderConstants.DEFAULT_STORAGE_PROVIDER_NAME)); services.AddSingletonNamedService(providerName, (sp, name) => sp.GetRequiredService>()); return services; } } }