using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; using Orleans; using Pole.Orleans.Provider.EntityframeworkCore.Conventions; using System; using System.Collections.Generic; using System.Text; namespace Pole.Orleans.Provider.EntityframeworkCore { public class GrainStoragePostConfigureOptions : IPostConfigureOptions> where TContext : DbContext where TGrain : Grain where TGrainState : new() where TEntity : class { public IGrainStorageConvention Convention { get; } public IGrainStorageConvention DefaultConvention { get; } public GrainStoragePostConfigureOptions(IServiceProvider serviceProvider) { DefaultConvention = (IGrainStorageConvention)serviceProvider.GetRequiredService(typeof(IGrainStorageConvention)); Convention = (IGrainStorageConvention) serviceProvider.GetService(typeof(IGrainStorageConvention)); } public void PostConfigure(string name, GrainStorageOptions options) { if (!string.Equals(name, typeof(TGrain).FullName)) throw new Exception("Post configure on wrong grain type."); if (options.IsPersistedFunc == null) options.IsPersistedFunc = DefaultConvention.CreateIsPersistedFunc(options); // Configure ETag if (options.ShouldUseETag) { if (!string.IsNullOrWhiteSpace(options.ETagPropertyName)) DefaultConvention.ConfigureETag(options.ETagPropertyName, options); } if (options.ReadStateAsync == null) { if (options.DbSetAccessor == null) options.DbSetAccessor = Convention?.CreateDefaultDbSetAccessorFunc() ?? DefaultConvention.CreateDefaultDbSetAccessorFunc(); if (Convention != null) Convention.SetDefaultKeySelector(options); else DefaultConvention.SetDefaultKeySelectors(options); if (options.PreCompileReadQuery) { options.ReadStateAsync = Convention?.CreatePreCompiledDefaultReadStateFunc(options) ?? DefaultConvention .CreatePreCompiledDefaultReadStateFunc(options); } else { options.ReadStateAsync = Convention?.CreateDefaultReadStateFunc() ?? DefaultConvention .CreateDefaultReadStateFunc(options); } } if (options.SetEntity == null) options.SetEntity = Convention?.GetSetterFunc() ?? DefaultConvention.GetSetterFunc(); if (options.GetEntity == null) options.GetEntity = Convention?.GetGetterFunc() ?? DefaultConvention.GetGetterFunc(); DefaultConvention.FindAndConfigureETag(options, options.ShouldUseETag); // todo: Validate options options.IsConfigured = true; } } }