Esempio n. 1
0
 public GrainStorage(
     GrainStorageOptions <TContext, TGrain, TGrainState> options,
     IServiceScopeFactory serviceScopeFactory,
     ILogger <GrainStorage <TContext, TGrain, TGrainState> > logger)
 {
     _options      = options ?? throw new ArgumentNullException(nameof(options));
     _scopeFactory = serviceScopeFactory ?? throw new ArgumentNullException(nameof(serviceScopeFactory));
     _logger       = logger ?? throw new ArgumentNullException(nameof(logger));
 }
        public GrainStorage(string grainType, IServiceProvider serviceProvider)
        {
            if (grainType == null)
            {
                throw new ArgumentNullException(nameof(grainType));
            }

            _serviceProvider = serviceProvider
                               ?? throw new ArgumentNullException(nameof(serviceProvider));

            var loggerFactory = _serviceProvider.GetService <ILoggerFactory>();

            _logger = loggerFactory?.CreateLogger <GrainStorage <TContext, TGrain, TGrainState> >()
                      ?? NullLogger <GrainStorage <TContext, TGrain, TGrainState> > .Instance;

            _scopeFactory = serviceProvider.GetRequiredService <IServiceScopeFactory>();
            _options      = GetOrCreateDefaultOptions(grainType);
        }
        public void PostConfigure(string name, GrainStorageOptions <TContext, TGrain, TGrainState> options)
        {
            if (!string.Equals(name, typeof(TGrain).FullName))
            {
                throw new Exception("Post configure on wrong grain type.");
            }

            if (options.ReadQuery == null)
            {
                options.ReadQuery = Convention?.CreateDefaultQueryFunc()
                                    ?? DefaultConvention.CreateDefaultQueryFunc <TContext, TGrainState>();
            }

            if (options.QueryExpressionGeneratorFunc == null)
            {
                options.QueryExpressionGeneratorFunc
                    = Convention?.CreateGrainStateQueryExpressionGeneratorFunc()
                      ?? DefaultConvention
                      .CreateDefaultGrainStateQueryExpressionGeneratorFunc <TGrain, TGrainState>(options);
            }

            if (options.IsPersistedFunc == null)
            {
                options.IsPersistedFunc =
                    DefaultConvention.CreateIsPersistedFunc <TGrainState>(options);
            }

            // Configure ETag
            if (options.ShouldUseETag)
            {
                if (!string.IsNullOrWhiteSpace(options.ETagPropertyName))
                {
                    DefaultConvention.ConfigureETag(options.ETagPropertyName, options);
                }
            }

            DefaultConvention.FindAndConfigureETag(options, options.ShouldUseETag);

            // todo: Validate options

            options.IsConfigured = true;
        }
Esempio n. 4
0
        public void PostConfigure(string name, GrainStorageOptions <TContext, TGrain, TEntity> 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 <TEntity>(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 <TContext, TEntity>();
                }

                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 <TGrainState, TEntity>();
            }

            if (options.GetEntity == null)
            {
                options.GetEntity =
                    Convention?.GetGetterFunc()
                    ?? DefaultConvention.GetGetterFunc <TGrainState, TEntity>();
            }

            DefaultConvention.FindAndConfigureETag(options, options.ShouldUseETag);

            // todo: Validate options

            options.IsConfigured = true;
        }