Skip to content

Commit

Permalink
Simplify AddDataAccess (OrchardCMS#767)
Browse files Browse the repository at this point in the history
  • Loading branch information
hishamco authored and sebastienros committed May 25, 2017
1 parent 30d320e commit 995fd3a
Showing 1 changed file with 10 additions and 30 deletions.
40 changes: 10 additions & 30 deletions src/OrchardCore/Orchard.Data/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
using System;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Modules;
using Microsoft.Data.Sqlite;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using MySql.Data.MySqlClient;
using Npgsql;
using Orchard.Data.Migration;
using Orchard.Environment.Shell;
Expand Down Expand Up @@ -45,49 +42,32 @@ public static IServiceCollection AddDataAccess(this IServiceCollection services)
{
return null;
}

var shellOptions = sp.GetService<IOptions<ShellOptions>>();

IConnectionFactory connectionFactory = null;
var isolationLevel = IsolationLevel.Unspecified;

var storeConfiguration = new Configuration();

switch (shellSettings.DatabaseProvider)
{
case "SqlConnection":
connectionFactory = new DbConnectionFactory<SqlConnection>(shellSettings.ConnectionString);
isolationLevel = IsolationLevel.ReadUncommitted;
storeConfiguration.UseSqlServer(shellSettings.ConnectionString, IsolationLevel.ReadUncommitted);
break;
case "Sqlite":
var shellOptions = sp.GetService<IOptions<ShellOptions>>();
var option = shellOptions.Value;
var databaseFolder = Path.Combine(hostingEnvironment.ContentRootPath, option.ShellsRootContainerName, option.ShellsContainerName, shellSettings.Name);
var databaseFile = Path.Combine(databaseFolder, "yessql.db");
Directory.CreateDirectory(databaseFolder);
connectionFactory = new DbConnectionFactory<SqliteConnection>($"Data Source={databaseFile};Cache=Shared");
isolationLevel = IsolationLevel.ReadUncommitted;
break;
storeConfiguration.UseSqLite($"Data Source={databaseFile};Cache=Shared", IsolationLevel.ReadUncommitted);
break;
case "MySql":
connectionFactory = new DbConnectionFactory<MySqlConnection>(shellSettings.ConnectionString);
isolationLevel = IsolationLevel.ReadUncommitted;
storeConfiguration.UseMySql(shellSettings.ConnectionString, IsolationLevel.ReadUncommitted);
break;
case "Postgres":
connectionFactory = new DbConnectionFactory<NpgsqlConnection>(shellSettings.ConnectionString);
isolationLevel = IsolationLevel.ReadUncommitted;
break;
storeConfiguration.UsePostgreSql(shellSettings.ConnectionString, IsolationLevel.ReadUncommitted);
break;
default:
throw new ArgumentException("Unknown database provider: " + shellSettings.DatabaseProvider);
}

var storeConfiguration = new Configuration
{
ConnectionFactory = connectionFactory,
IsolationLevel = isolationLevel
};

storeConfiguration.RegisterMySql();
storeConfiguration.RegisterSqLite();
storeConfiguration.RegisterSqlServer();
storeConfiguration.RegisterPostgreSql();

if (!string.IsNullOrWhiteSpace(shellSettings.TablePrefix))
{
storeConfiguration.TablePrefix = shellSettings.TablePrefix + "_";
Expand All @@ -99,7 +79,7 @@ public static IServiceCollection AddDataAccess(this IServiceCollection services)
return store;
});

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

Expand Down

0 comments on commit 995fd3a

Please sign in to comment.