I am using Dapper ORM for database operations in my asp.net core web API project. Right now I am opening the new database connection every time and using it inside using block so they will get disposed when scope ends. But I was looking to handle all those connections without using using block and also want to dispose of them automatically. I was searching for a way to achieve this using dependency injection as they dispose of objects that implements IDisposable, automatically.
Here is how I am handling all the DB connections:
Created a GetConnection property in my base repository:
private IDbConnection _connection;
public IDbConnection GetConnection
{
get
{
_connection = new NpgsqlConnection("Connection String");
return _connection;
}
}
Accessing the property inside using block:
public async Task<IEnumerable<T>> GetAllAsync()
{
IEnumerable<T> records = null;
using (IDbConnection connection = GetConnection)
{
//db operations
}
return records;
}
So how can I achieve the same using dependency injection that will initialize the IDbconnection when required and disposes of at the end of the request without the need to encapsulate IDbconnection inside the using block?
In short, I want to avoid using GetConnection property every time to create a database object and eliminate using using blocks to dispose of the same.
Dapper is an open-source and micro ORM (object-relational mapping) product developed by the StackOverflow team. It is very lightweight and straightforward to use with a project. It supports PostgreSQL, MySQL, SQL Server, and other databases.
Dapper is a NuGet library, can be used with any . NET project. Quite lightweight, high performance. Drastically reduces the database access code.
Using Dapper Queries in ASP.NET Core Web API Then inside the using statement, we use our DapperContext object to create the SQLConnection object (or to be more precise an IDbConnection object) by calling the CreateConnection method. As you can see, as soon as we stop using our connection, we have to dispose of it.
Allow Dapper to manage it: Dapper automatically opens the connection (if it was not opened) and closes it (if it was opened by Dapper) for you. This is similar to DataAdapter. Fill() method.
I did it like this:
Adding Transient service in startup.cs file
services.AddTransient<IDbConnection>((sp) => new NpgsqlConnection("connectionString"));
Initializing the IDbconnection object in the base repository constructor like:
class RepositoryBase
{
protected IDbConnection _connection;
protected RepositoryBase(IDbConnection dbConnection)
{
_connection = dbConnection;
}
}
and performing the DB operation in my Repositories like
class XyzRepository : RepositoryBase
{
public async Task<IEnumerable<T>> GetAllAsync()
{
IEnumerable<T> records = null;
await _connection.ExecuteScalarAsync<object>("sqlQuery");
return records;
}
}
This will automatically dispose of IDbconnection object at the end of the request without using using blocks.
Reference from answer: How do I handle Database Connections with Dapper in .NET?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With