Setting up a db factory with logging
Important so we can log all commands
This commit is contained in:
parent
ad4ed12f00
commit
1501ff442a
10 changed files with 412 additions and 177 deletions
|
|
@ -3,20 +3,55 @@ 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(c =>
|
||||
{
|
||||
IDbConnection connection = new NpgsqlConnection(ConnectionString);
|
||||
return connection;
|
||||
})
|
||||
.InstancePerLifetimeScope();
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue