C
C#13mo ago
Immutable

❔ Dapper Unit Of Work

So I'm setting up a simple UnitOfWork with dapper For that we need to have a scoped Transaction and Connection
public DapperContext(IConfiguration configuration)
{
var connectionString = configuration.GetConnectionString("DefaultConnectionString");
Connection = new NpgsqlConnection(connectionString);
Connection.Open();
Transaction = Connection.BeginTransaction();
}
public DapperContext(IConfiguration configuration)
{
var connectionString = configuration.GetConnectionString("DefaultConnectionString");
Connection = new NpgsqlConnection(connectionString);
Connection.Open();
Transaction = Connection.BeginTransaction();
}
we register this as
services.AddScoped<IDapperContext, DapperContext>();
services.AddScoped<IDapperContext, DapperContext>();
I have to open connection with Connection.Open(); before creating a transaction though My question is how good of an idea is this? Is there any better way of doing this without blocking?
4 Replies
Immutable
Immutable13mo ago
Stil interneted
n8ta
n8ta13mo ago
What's the problem exactly? Is openning the connection slow such that blocking on it is an issue?
Immutable
Immutable13mo ago
Yeah Maybe I can do Lazy connection and transaction such that it only opens when actually has to execute Dapper query
Accord
Accord13mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.