Skip to content

Commit 5674b42

Browse files
authored
Merge pull request #20 from nitin27may/fix/19-dapper-helper-async
Refactor DapperHelper to use SqlConnection and async methods for database operations
2 parents a36b608 + f804087 commit 5674b42

File tree

2 files changed

+30
-29
lines changed

2 files changed

+30
-29
lines changed

backend/src/Contact.Infrastructure/Persistence/Helper/DapperHelper.cs

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public DapperHelper(IOptions<AppSettings> myConfigValue, ILogger<DapperHelper> l
1717
_logger = logger;
1818
}
1919

20-
public IDbConnection GetConnection()
20+
public SqlConnection GetConnection()
2121
{
2222
_logger.LogInformation("Connection String: {connectionString}", myConfig.ConnectionStrings.DefaultConnection);
2323
return new SqlConnection(myConfig.ConnectionStrings.DefaultConnection);
@@ -30,115 +30,115 @@ public void Dispose()
3030

3131
public async Task<int> Execute(string sp, object parms, CommandType commandType = CommandType.Text, IDbTransaction? transaction = null)
3232
{
33-
var db = transaction?.Connection ?? GetConnection();
33+
var db = transaction?.Connection as SqlConnection ?? GetConnection();
3434
try
3535
{
3636
if (db.State == ConnectionState.Closed)
37-
db.Open();
37+
await db.OpenAsync();
3838

3939
var resultObj = await db.ExecuteAsync(sp, parms, commandType: commandType, transaction: transaction);
4040
if (transaction == null)
41-
db.Close();
41+
await db.CloseAsync();
4242
return resultObj;
4343
}
4444
catch (Exception exception)
4545
{
4646
if (transaction == null && db?.State == ConnectionState.Open)
47-
db.Close();
47+
await db.CloseAsync();
4848
_logger.LogInformation("SQL DB error exception: {error}", exception.Message);
49-
throw exception;
49+
throw;
5050
}
5151
}
5252

53-
public async Task<T> Get<T>(string sp, Object parms, CommandType commandType = CommandType.Text, IDbTransaction transaction = null)
53+
public async Task<T> Get<T>(string sp, Object parms, CommandType commandType = CommandType.Text, IDbTransaction? transaction = null)
5454
{
5555
T result;
56-
var db = transaction?.Connection ?? GetConnection();
56+
var db = transaction?.Connection as SqlConnection ?? GetConnection();
5757
try
5858
{
5959
if (db.State == ConnectionState.Closed)
60-
db.Open();
60+
await db.OpenAsync();
6161

6262
var resultObj = await db.QueryFirstOrDefaultAsync<T>(sp, parms, commandType: commandType, transaction: transaction);
6363
result = resultObj;
6464
if (transaction == null)
65-
db.Close();
65+
await db.CloseAsync();
6666
return result;
6767
}
6868
catch (Exception exception)
6969
{
7070
if (transaction == null && db?.State == ConnectionState.Open)
71-
db.Close();
71+
await db.CloseAsync();
7272
_logger.LogInformation("SQL DB error exception: {error}", exception.Message);
73-
throw exception;
73+
throw;
7474
}
7575
}
7676

7777
public async Task<IEnumerable<T>> GetAll<T>(string sp, Object parms, CommandType commandType = CommandType.Text)
7878
{
7979
try
8080
{
81-
using (IDbConnection db = GetConnection())
81+
using (var db = GetConnection())
8282
{
83+
await db.OpenAsync();
8384
var result = await db.QueryAsync<T>(sp, parms, commandType: commandType);
85+
await db.CloseAsync();
8486
return result.ToList();
8587
}
8688
}
8789
catch (Exception ex)
8890
{
8991
_logger.LogInformation("SQL DB error exception: {error}", ex.Message);
90-
91-
throw ex;
92+
throw;
9293
}
9394
}
9495

95-
public async Task<T> Insert<T>(string sp, Object parms, CommandType commandType = CommandType.Text, IDbTransaction transaction = null)
96+
public async Task<T> Insert<T>(string sp, Object parms, CommandType commandType = CommandType.Text, IDbTransaction? transaction = null)
9697
{
9798
T result;
98-
var db = transaction?.Connection ?? GetConnection();
99+
var db = transaction?.Connection as SqlConnection ?? GetConnection();
99100
try
100101
{
101102
if (db.State == ConnectionState.Closed)
102-
db.Open();
103+
await db.OpenAsync();
103104

104105
var resultObj = await db.QueryFirstOrDefaultAsync<T>(sp, parms, commandType: commandType, transaction: transaction);
105106
result = resultObj;
106107
if (transaction == null)
107-
db.Close();
108+
await db.CloseAsync();
108109
return result;
109110
}
110111
catch (Exception exception)
111112
{
112113
if (transaction == null && db?.State == ConnectionState.Open)
113-
db.Close();
114+
await db.CloseAsync();
114115
_logger.LogInformation("SQL DB error exception: {error}", exception.Message);
115-
throw exception;
116+
throw;
116117
}
117118
}
118119

119120
public async Task<T> Update<T>(string sp, object parms, CommandType commandType = CommandType.Text, IDbTransaction? transaction = null)
120121
{
121122
T result;
122-
var db = transaction?.Connection ?? GetConnection();
123+
var db = transaction?.Connection as SqlConnection ?? GetConnection();
123124
try
124125
{
125126
if (db.State == ConnectionState.Closed)
126-
db.Open();
127+
await db.OpenAsync();
127128

128129
var resultObj = await db.QueryFirstOrDefaultAsync<T>(sp, parms, commandType: commandType, transaction: transaction);
129130
result = resultObj;
130131
if (transaction == null)
131-
db.Close();
132+
await db.CloseAsync();
132133
return result;
133134
}
134135
catch (Exception ex)
135136
{
136137
if (transaction == null && db?.State == ConnectionState.Open)
137-
db.Close();
138+
await db.CloseAsync();
138139
_logger.LogInformation("SQL DB error exception: {error}", ex.Message);
139-
throw ex;
140+
throw;
140141
}
141-
142142
}
143143
}
144-
}
144+
}

backend/src/Contact.Infrastructure/Persistence/Helper/IDapperHelper.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
using System.Data;
2+
using Microsoft.Data.SqlClient;
23

34
namespace Contact.Infrastructure.Persistence.Helper;
45

56
public interface IDapperHelper : IDisposable
67
{
7-
IDbConnection GetConnection();
8+
SqlConnection GetConnection();
89
Task<T> Get<T>(string sp, Object parms, CommandType commandType = CommandType.Text, IDbTransaction? transaction = null);
910
Task<IEnumerable<T>> GetAll<T>(string sp, Object parms, CommandType commandType = CommandType.Text);
1011
Task<int> Execute(string sp, Object parms, CommandType commandType = CommandType.Text, IDbTransaction? transaction = null);

0 commit comments

Comments
 (0)