© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
5 replies
Byte

❔ 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();
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?
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Dapper - Unit of work between 2 repositories
C#CC# / help
11mo ago
How does Dapper work
C#CC# / help
2y ago
❔ Dapper
C#CC# / help
3y ago
Confusion about Repository Pattern and Unit of Work.
C#CC# / help
3y ago