Remotely/Server/Data/AppDbFactory.cs
2024-02-20 17:10:28 -08:00

40 lines
1.1 KiB
C#

using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Remotely.Server.Services;
using System;
namespace Remotely.Server.Data;
public interface IAppDbFactory
{
AppDb GetContext();
}
public class AppDbFactory : IAppDbFactory
{
private readonly IConfiguration _configuration;
private readonly IWebHostEnvironment _hostEnv;
public AppDbFactory(
IConfiguration configuration,
IWebHostEnvironment hostEnv)
{
_configuration = configuration;
_hostEnv = hostEnv;
}
public AppDb GetContext()
{
var dbProvider = _configuration["ApplicationOptions:DbProvider"]?.ToLower();
return dbProvider switch
{
"sqlite" => new SqliteDbContext(_configuration, _hostEnv),
"sqlserver" => new SqlServerDbContext(_configuration, _hostEnv),
"postgresql" => new PostgreSqlDbContext(_configuration, _hostEnv),
"inmemory" => new TestingDbContext(_hostEnv),
_ => throw new ArgumentException("Unknown DB provider."),
};
}
}