コード例 #1
0
ファイル: YesSqlTest.cs プロジェクト: harry-wangx/Harry.Data
        private static IStore CreateStore()
        {
            IConfiguration storeConfiguration = new YesSql.Configuration();


            var databaseFile = Path.Combine("yessql.db");

            storeConfiguration
            .UseSqLite($"Data Source={databaseFile};Cache=Shared", IsolationLevel.ReadUncommitted)
            .UseDefaultIdGenerator();

            var store = StoreFactory.CreateAsync(storeConfiguration).GetAwaiter().GetResult();

            store.RegisterIndexes(new UserIndexProvider());

            return(store);
        }
コード例 #2
0
        private static IConfiguration GetStoreConfiguration(IServiceProvider sp)
        {
            var yesSqlOptions = sp.GetService <IOptions <YesSqlOptions> >().Value;

            var storeConfiguration = new YesSql.Configuration
            {
                CommandsPageSize   = yesSqlOptions.CommandsPageSize,
                QueryGatingEnabled = yesSqlOptions.QueryGatingEnabled,
                ContentSerializer  = new PoolingJsonContentSerializer(sp.GetService <ArrayPool <char> >()),
            };

            if (yesSqlOptions.IdGenerator != null)
            {
                storeConfiguration.IdGenerator = yesSqlOptions.IdGenerator;
            }

            if (yesSqlOptions.TableNameConvention != null)
            {
                storeConfiguration.TableNameConvention = yesSqlOptions.TableNameConvention;
            }

            if (yesSqlOptions.IdentifierAccessorFactory != null)
            {
                storeConfiguration.IdentifierAccessorFactory = yesSqlOptions.IdentifierAccessorFactory;
            }

            if (yesSqlOptions.VersionAccessorFactory != null)
            {
                storeConfiguration.VersionAccessorFactory = yesSqlOptions.VersionAccessorFactory;
            }

            if (yesSqlOptions.ContentSerializer != null)
            {
                storeConfiguration.ContentSerializer = yesSqlOptions.ContentSerializer;
            }

            return(storeConfiguration);
        }
コード例 #3
0
        /// <summary>
        /// Adds tenant level data access services.
        /// </summary>
        /// <param name="builder">The <see cref="OrchardCoreBuilder"/>.</param>
        public static OrchardCoreBuilder AddDataAccess(this OrchardCoreBuilder builder)
        {
            builder.ConfigureServices(services =>
            {
                services.AddScoped <IDataMigrationManager, DataMigrationManager>();
                services.AddScoped <IModularTenantEvents, AutomaticDataMigrations>();

                services.AddOptions <StoreCollectionOptions>();
                services.AddTransient <IConfigureOptions <SqliteOptions>, SqliteOptionsConfiguration>();

                // Adding supported databases
                services.TryAddDataProvider(name: "Sql Server", value: "SqlConnection", hasConnectionString: true, sampleConnectionString: "Server=localhost;Database=Orchard;User Id=username;Password=password", hasTablePrefix: true, isDefault: false);
                services.TryAddDataProvider(name: "Sqlite", value: "Sqlite", hasConnectionString: false, hasTablePrefix: false, isDefault: true);
                services.TryAddDataProvider(name: "MySql", value: "MySql", hasConnectionString: true, sampleConnectionString: "Server=localhost;Database=Orchard;Uid=username;Pwd=password", hasTablePrefix: true, isDefault: false);
                services.TryAddDataProvider(name: "Postgres", value: "Postgres", hasConnectionString: true, sampleConnectionString: "Server=localhost;Port=5432;Database=Orchard;User Id=username;Password=password", hasTablePrefix: true, isDefault: false);

                // Configuring data access

                services.AddSingleton(sp =>
                {
                    var shellSettings = sp.GetService <ShellSettings>();

                    // Before the setup a 'DatabaseProvider' may be configured without a required 'ConnectionString'.
                    if (shellSettings.State == TenantState.Uninitialized || shellSettings["DatabaseProvider"] == null)
                    {
                        return(null);
                    }

                    IConfiguration storeConfiguration = new YesSql.Configuration
                    {
                        ContentSerializer = new PoolingJsonContentSerializer(sp.GetService <ArrayPool <char> >()),
                    };

                    switch (shellSettings["DatabaseProvider"])
                    {
                    case "SqlConnection":
                        storeConfiguration
                        .UseSqlServer(shellSettings["ConnectionString"], IsolationLevel.ReadUncommitted)
                        .UseBlockIdGenerator();
                        break;

                    case "Sqlite":
                        var shellOptions            = sp.GetService <IOptions <ShellOptions> >();
                        var sqliteOptions           = sp.GetService <IOptions <SqliteOptions> >()?.Value ?? new SqliteOptions();
                        var option                  = shellOptions.Value;
                        var databaseFolder          = Path.Combine(option.ShellsApplicationDataPath, option.ShellsContainerName, shellSettings.Name);
                        var databaseFile            = Path.Combine(databaseFolder, "yessql.db");
                        var connectionStringBuilder = new SqliteConnectionStringBuilder
                        {
                            DataSource = databaseFile,
                            Cache      = SqliteCacheMode.Shared,
                            Pooling    = sqliteOptions.UseConnectionPooling
                        };

                        Directory.CreateDirectory(databaseFolder);
                        storeConfiguration
                        .UseSqLite(connectionStringBuilder.ToString(), IsolationLevel.ReadUncommitted)
                        .UseDefaultIdGenerator();
                        break;

                    case "MySql":
                        storeConfiguration
                        .UseMySql(shellSettings["ConnectionString"], IsolationLevel.ReadUncommitted)
                        .UseBlockIdGenerator();
                        break;

                    case "Postgres":
                        storeConfiguration
                        .UsePostgreSql(shellSettings["ConnectionString"], IsolationLevel.ReadUncommitted)
                        .UseBlockIdGenerator();
                        break;

                    default:
                        throw new ArgumentException("Unknown database provider: " + shellSettings["DatabaseProvider"]);
                    }

                    if (!string.IsNullOrWhiteSpace(shellSettings["TablePrefix"]))
                    {
                        storeConfiguration = storeConfiguration.SetTablePrefix(shellSettings["TablePrefix"] + "_");
                    }

                    var store   = StoreFactory.CreateAndInitializeAsync(storeConfiguration).GetAwaiter().GetResult();
                    var options = sp.GetService <IOptions <StoreCollectionOptions> >().Value;
                    foreach (var collection in options.Collections)
                    {
                        store.InitializeCollectionAsync(collection).GetAwaiter().GetResult();
                    }

                    var indexes = sp.GetServices <IIndexProvider>();

                    store.RegisterIndexes(indexes);

                    return(store);
                });

                services.AddScoped(sp =>
                {
                    var store = sp.GetService <IStore>();

                    if (store == null)
                    {
                        return(null);
                    }

                    var session = store.CreateSession();

                    var scopedServices = sp.GetServices <IScopedIndexProvider>();

                    session.RegisterIndexes(scopedServices.ToArray());

                    ShellScope.Current
                    .RegisterBeforeDispose(scope =>
                    {
                        return(scope.ServiceProvider
                               .GetRequiredService <IDocumentStore>()
                               .CommitAsync());
                    })
                    .AddExceptionHandler((scope, e) =>
                    {
                        return(scope.ServiceProvider
                               .GetRequiredService <IDocumentStore>()
                               .CancelAsync());
                    });

                    return(session);
                });

                services.AddScoped <IDocumentStore, DocumentStore>();
                services.AddSingleton <IFileDocumentStore, FileDocumentStore>();

                services.AddTransient <IDbConnectionAccessor, DbConnectionAccessor>();
            });

            return(builder);
        }
コード例 #4
0
        /// <summary>
        /// Adds tenant level data access services.
        /// </summary>
        public static OrchardCoreBuilder AddDataAccess(this OrchardCoreBuilder builder)
        {
            builder.ConfigureServices(services =>
            {
                services.AddScoped <IDataMigrationManager, DataMigrationManager>();
                services.AddScoped <IModularTenantEvents, AutomaticDataMigrations>();

                // Adding supported databases
                services.TryAddDataProvider(name: "Sql Server", value: "SqlConnection", hasConnectionString: true, hasTablePrefix: true, isDefault: false);
                services.TryAddDataProvider(name: "Sqlite", value: "Sqlite", hasConnectionString: false, hasTablePrefix: false, isDefault: true);
                services.TryAddDataProvider(name: "MySql", value: "MySql", hasConnectionString: true, hasTablePrefix: true, isDefault: false);
                services.TryAddDataProvider(name: "Postgres", value: "Postgres", hasConnectionString: true, hasTablePrefix: true, isDefault: false);

                // Configuring data access

                services.AddSingleton <IStore>(sp =>
                {
                    var shellSettings = sp.GetService <ShellSettings>();

                    // Before the setup a 'DatabaseProvider' may be configured without a required 'ConnectionString'.
                    if (shellSettings.State == TenantState.Uninitialized || shellSettings["DatabaseProvider"] == null)
                    {
                        return(null);
                    }

                    IConfiguration storeConfiguration = new YesSql.Configuration();

                    switch (shellSettings["DatabaseProvider"])
                    {
                    case "SqlConnection":
                        storeConfiguration
                        .UseSqlServer(shellSettings["ConnectionString"], IsolationLevel.ReadUncommitted)
                        .UseBlockIdGenerator();
                        break;

                    case "Sqlite":
                        var shellOptions   = sp.GetService <IOptions <ShellOptions> >();
                        var option         = shellOptions.Value;
                        var databaseFolder = Path.Combine(option.ShellsApplicationDataPath, option.ShellsContainerName, shellSettings.Name);
                        var databaseFile   = Path.Combine(databaseFolder, "yessql.db");
                        Directory.CreateDirectory(databaseFolder);
                        storeConfiguration
                        .UseSqLite($"Data Source={databaseFile};Cache=Shared", IsolationLevel.ReadUncommitted)
                        .UseDefaultIdGenerator();
                        break;

                    case "MySql":
                        storeConfiguration
                        .UseMySql(shellSettings["ConnectionString"], IsolationLevel.ReadUncommitted)
                        .UseBlockIdGenerator();
                        break;

                    case "Postgres":
                        storeConfiguration
                        .UsePostgreSql(shellSettings["ConnectionString"], IsolationLevel.ReadUncommitted)
                        .UseBlockIdGenerator();
                        break;

                    default:
                        throw new ArgumentException("Unknown database provider: " + shellSettings["DatabaseProvider"]);
                    }

                    if (!string.IsNullOrWhiteSpace(shellSettings["TablePrefix"]))
                    {
                        storeConfiguration = storeConfiguration.SetTablePrefix(shellSettings["TablePrefix"] + "_");
                    }

                    var store   = StoreFactory.CreateAsync(storeConfiguration).GetAwaiter().GetResult();
                    var indexes = sp.GetServices <IIndexProvider>();

                    store.RegisterIndexes(indexes);

                    return(store);
                });

                services.AddScoped(sp =>
                {
                    var store = sp.GetService <IStore>();

                    if (store == null)
                    {
                        return(null);
                    }

                    var session = store.CreateSession();

                    var scopedServices = sp.GetServices <IScopedIndexProvider>();

                    session.RegisterIndexes(scopedServices.ToArray());

                    var httpContext = sp.GetRequiredService <IHttpContextAccessor>()?.HttpContext;

                    if (httpContext != null)
                    {
                        httpContext.Items[typeof(YesSql.ISession)] = session;
                    }

                    return(session);
                });

                services.AddTransient <IDbConnectionAccessor, DbConnectionAccessor>();
            });

            return(builder);
        }
コード例 #5
0
        /// <summary>
        /// Adds tenant level data access services.
        /// </summary>
        public static OrchardCoreBuilder AddDataAccess(this OrchardCoreBuilder builder)
        {
            builder.ConfigureServices(services =>
            {
                services.AddScoped <IDataMigrationManager, DataMigrationManager>();
                services.AddScoped <IModularTenantEvents, AutomaticDataMigrations>();

                // Adding supported databases
                services.TryAddDataProvider(name: "Sql Server", value: "SqlConnection", hasConnectionString: true, hasTablePrefix: true, isDefault: false);
                services.TryAddDataProvider(name: "Sqlite", value: "Sqlite", hasConnectionString: false, hasTablePrefix: false, isDefault: true);
                services.TryAddDataProvider(name: "MySql", value: "MySql", hasConnectionString: true, hasTablePrefix: true, isDefault: false);
                services.TryAddDataProvider(name: "Postgres", value: "Postgres", hasConnectionString: true, hasTablePrefix: true, isDefault: false);

                // Configuring data access

                services.AddSingleton <IStore>(sp =>
                {
                    var shellSettings = sp.GetService <ShellSettings>();

                    if (shellSettings.DatabaseProvider == null)
                    {
                        return(null);
                    }

                    var storeConfiguration = new YesSql.Configuration();

                    // Disabling query gating as it's failing to improve performance right now
                    storeConfiguration.DisableQueryGating();

                    switch (shellSettings.DatabaseProvider)
                    {
                    case "SqlConnection":
                        storeConfiguration.UseSqlServer(shellSettings.ConnectionString, IsolationLevel.ReadUncommitted);
                        break;

                    case "Sqlite":
                        var shellOptions   = sp.GetService <IOptions <ShellOptions> >();
                        var option         = shellOptions.Value;
                        var databaseFolder = Path.Combine(option.ShellsApplicationDataPath, option.ShellsContainerName, shellSettings.Name);
                        var databaseFile   = Path.Combine(databaseFolder, "yessql.db");
                        Directory.CreateDirectory(databaseFolder);
                        storeConfiguration.UseSqLite($"Data Source={databaseFile};Cache=Shared", IsolationLevel.ReadUncommitted);
                        break;

                    case "MySql":
                        storeConfiguration.UseMySql(shellSettings.ConnectionString, IsolationLevel.ReadUncommitted);
                        break;

                    case "Postgres":
                        storeConfiguration.UsePostgreSql(shellSettings.ConnectionString, IsolationLevel.ReadUncommitted);
                        break;

                    default:
                        throw new ArgumentException("Unknown database provider: " + shellSettings.DatabaseProvider);
                    }

                    if (!string.IsNullOrWhiteSpace(shellSettings.TablePrefix))
                    {
                        storeConfiguration.TablePrefix = shellSettings.TablePrefix + "_";
                    }

                    var store   = new Store(storeConfiguration);
                    var indexes = sp.GetServices <IIndexProvider>();
                    store.RegisterIndexes(indexes.ToArray());
                    return(store);
                });

                services.AddScoped(sp =>
                {
                    var store = sp.GetService <IStore>();

                    if (store == null)
                    {
                        return(null);
                    }

                    var session = store.CreateSession();

                    return(session);
                });
            });

            return(builder);
        }