57 lines
1.6 KiB
C#
57 lines
1.6 KiB
C#
using Autofac;
|
|
using Npgsql;
|
|
using System.Data;
|
|
namespace PlanTempus.Database.ModuleRegistry
|
|
{
|
|
public interface IDbConnectionFactory
|
|
{
|
|
IDbConnection Create();
|
|
IDbConnection Create(string username, string password);
|
|
}
|
|
public class DbPostgreSqlModule : Module
|
|
{
|
|
public required string ConnectionString { get; set; }
|
|
|
|
protected override void Load(ContainerBuilder builder)
|
|
{
|
|
Insight.Database.Providers.PostgreSQL.PostgreSQLInsightDbProvider.RegisterProvider();
|
|
|
|
builder.Register<IDbConnectionFactory>(c =>
|
|
new Core.ConnectionFactory.PostgresConnectionFactory(ConnectionString))
|
|
.SingleInstance();
|
|
|
|
builder.RegisterType<Core.Sql.SqlOperations>()
|
|
.As<Core.Sql.IDatabaseOperations>();
|
|
}
|
|
}
|
|
public class PostgresConnectionFactory1 //: IDbConnectionFactory
|
|
{
|
|
private readonly string _baseConnectionString;
|
|
|
|
public PostgresConnectionFactory1(string connectionString)
|
|
{
|
|
_baseConnectionString = connectionString;
|
|
}
|
|
|
|
public IDbConnection Create()
|
|
{
|
|
return new NpgsqlConnection(_baseConnectionString);
|
|
}
|
|
|
|
public IDbConnection Create(string username, string password)
|
|
{
|
|
var builder = new NpgsqlConnectionStringBuilder(_baseConnectionString)
|
|
{
|
|
Username = username,
|
|
Password = password
|
|
};
|
|
|
|
return new NpgsqlConnection(builder.ToString());
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|