using Microsoft.EntityFrameworkCore; using Orleans.Runtime; using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Text; using System.Threading.Tasks; namespace Pole.Orleans.Provider.EntityframeworkCore { public static class GrainStorageOptionsExtensions { public static GrainStorageOptions UseQuery( this GrainStorageOptions options, Func>queryFunc) where TContext : DbContext where TGrainState : class { options.DbSetAccessor = queryFunc; return options; } public static GrainStorageOptions ConfigureIsPersisted( this GrainStorageOptions options, Func isPersistedFunc) where TContext : DbContext where TGrainState : class { options.IsPersistedFunc = isPersistedFunc; return options; } /// /// Instructs the storage provider to precompile read query. /// This will lead to better performance for complex queries. /// Default is to precompile. /// /// /// /// /// /// /// public static GrainStorageOptions PreCompileReadQuery( this GrainStorageOptions options, bool value = true) where TContext : DbContext where TGrainState : class { options.PreCompileReadQuery = value; return options; } /// /// Overrides the default implementation used to query grain state from database. /// /// /// /// /// /// /// public static GrainStorageOptions ConfigureReadState( this GrainStorageOptions options, Func> readStateAsyncFunc) where TContext : DbContext where TGrainState : class { if (options == null) throw new ArgumentNullException(nameof(options)); options.ReadStateAsync = readStateAsyncFunc ?? throw new ArgumentNullException(nameof(readStateAsyncFunc)); return options; } /// /// Instruct the storage that the current entity should use etags. /// If no valid properties were found on the entity and exception would be thrown. /// /// /// /// /// /// public static GrainStorageOptions UseETag( this GrainStorageOptions options) where TContext : DbContext where TGrainState : class { options.ShouldUseETag = true; return options; } public static GrainStorageOptions UseETag( this GrainStorageOptions options, Expression> expression) where TContext : DbContext where TGrainState : class { if (options == null) throw new ArgumentNullException(nameof(options)); if (expression == null) throw new ArgumentNullException(nameof(expression)); var memberExpression = expression.Body as MemberExpression ?? throw new ArgumentException( $"{nameof(expression)} must be a MemberExpression."); options.ETagPropertyName = memberExpression.Member.Name; options.ShouldUseETag = true; return options; } public static GrainStorageOptions UseETag( this GrainStorageOptions options, string propertyName) where TContext : DbContext where TGrainState : class { if (options == null) throw new ArgumentNullException(nameof(options)); if (propertyName == null) throw new ArgumentNullException(nameof(propertyName)); options.ETagPropertyName = propertyName; options.ShouldUseETag = true; return options; } public static GrainStorageOptions UseKey( this GrainStorageOptions options, Expression> expression) where TContext : DbContext where TGrainState : class { if (options == null) throw new ArgumentNullException(nameof(options)); if (expression == null) throw new ArgumentNullException(nameof(expression)); var memberExpression = expression.Body as MemberExpression ?? throw new ArgumentException( $"{nameof(expression)} must be a MemberExpression."); options.KeyPropertyName = memberExpression.Member.Name; return options; } public static GrainStorageOptions UseKey( this GrainStorageOptions options, Expression> expression) where TContext : DbContext where TGrainState : class { if (options == null) throw new ArgumentNullException(nameof(options)); if (expression == null) throw new ArgumentNullException(nameof(expression)); var memberExpression = expression.Body as MemberExpression ?? throw new GrainStorageConfigurationException( $"{nameof(expression)} must be a MemberExpression."); options.KeyPropertyName = memberExpression.Member.Name; return options; } public static GrainStorageOptions UseKey( this GrainStorageOptions options, Expression> expression) where TContext : DbContext where TGrainState : class { if (options == null) throw new ArgumentNullException(nameof(options)); if (expression == null) throw new ArgumentNullException(nameof(expression)); var memberExpression = expression.Body as MemberExpression ?? throw new ArgumentException( $"{nameof(expression)} must be a MemberExpression."); options.KeyPropertyName = memberExpression.Member.Name; return options; } public static GrainStorageOptions UseKey( this GrainStorageOptions options, string propertyName) where TContext : DbContext where TGrainState : class { if (options == null) throw new ArgumentNullException(nameof(options)); if (propertyName == null) throw new ArgumentNullException(nameof(propertyName)); options.KeyPropertyName = propertyName; return options; } public static GrainStorageOptions UseKeyExt( this GrainStorageOptions options, Expression> expression) where TContext : DbContext where TGrainState : class { if (options == null) throw new ArgumentNullException(nameof(options)); if (expression == null) throw new ArgumentNullException(nameof(expression)); var memberExpression = expression.Body as MemberExpression ?? throw new ArgumentException( $"{nameof(expression)} must be a MemberExpression."); options.KeyExtPropertyName = memberExpression.Member.Name; return options; } public static GrainStorageOptions UseKeyExt( this GrainStorageOptions options, string propertyName) where TContext : DbContext where TGrainState : class { if (options == null) throw new ArgumentNullException(nameof(options)); if (propertyName == null) throw new ArgumentNullException(nameof(propertyName)); options.KeyExtPropertyName = propertyName; return options; } public static GrainStorageOptions CheckPersistenceOn( this GrainStorageOptions options, Expression> expression) where TContext : DbContext where TGrainState : class { if (options == null) throw new ArgumentNullException(nameof(options)); if (expression == null) throw new ArgumentNullException(nameof(expression)); var memberExpression = expression.Body as MemberExpression ?? throw new ArgumentException( $"{nameof(expression)} must be a MemberExpression."); options.PersistenceCheckPropertyName = memberExpression.Member.Name; return options; } public static GrainStorageOptions CheckPersistenceOn( this GrainStorageOptions options, string propertyName) where TContext : DbContext where TGrainState : class { if (options == null) throw new ArgumentNullException(nameof(options)); if (propertyName == null) throw new ArgumentNullException(nameof(propertyName)); options.PersistenceCheckPropertyName = propertyName; return options; } } }