Skip to content

Commit f53bfd2

Browse files
Fix SQL Connection (#976)
1 parent 4d57576 commit f53bfd2

9 files changed

Lines changed: 11 additions & 37 deletions

File tree

src/Application/Common/Interfaces/ISqlConnectionFactory.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ namespace Cfo.Cats.Application.Common.Interfaces;
22

33
public interface ISqlConnectionFactory
44
{
5-
IDbConnection GetOpenConnection();
6-
7-
IDbConnection CreateNewConnection();
5+
IDbConnection CreateOpenConnection();
86

97
string GetConnectionString();
108
}

src/Application/Features/Labels/LabelCounter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public class LabelCounter(ISqlConnectionFactory sqlConnectionFactory) : ILabelCo
77
{
88
public int CountParticipants(LabelId labelId)
99
{
10-
var connection = sqlConnectionFactory.GetOpenConnection();
10+
using var connection = sqlConnectionFactory.CreateOpenConnection();
1111

1212
const string sql = """
1313
SELECT Count(*)
@@ -24,7 +24,7 @@ FROM [Participant].[Label] as [Label]
2424

2525
public int CountVisibleLabels(string name, string? contractId)
2626
{
27-
var connection = sqlConnectionFactory.GetOpenConnection();
27+
using var connection = sqlConnectionFactory.CreateOpenConnection();
2828

2929
const string sql = """
3030
SELECT Count(*)

src/Application/Features/Labels/Queries/GetLabelById.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class Handler(ISqlConnectionFactory sqlConnectionFactory) : IRequestHandl
1818
{
1919
public async Task<Result<LabelDto>> Handle(Query request, CancellationToken cancellationToken)
2020
{
21-
var connection = sqlConnectionFactory.GetOpenConnection();
21+
using var connection = sqlConnectionFactory.CreateOpenConnection();
2222

2323
const string sql = $"""
2424
SELECT

src/Application/Features/Labels/Queries/GetVisibleLabels.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class Handler(ISqlConnectionFactory sqlConnectionFactory) : IRequestHandl
1717
{
1818
public async Task<Result<LabelDto[]>> Handle(Query request, CancellationToken cancellationToken)
1919
{
20-
var connection = sqlConnectionFactory.GetOpenConnection();
20+
using var connection = sqlConnectionFactory.CreateOpenConnection();
2121

2222
const string sql = $"""
2323
SELECT

src/Application/Features/ParticipantLabels/GetParticipantLabels/GetParticipantLabelsQueryHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public class GetParticipantLabelsQueryHandler(ISqlConnectionFactory sqlConnectio
88
{
99
public async Task<Result<GetParticipantLabelsDto>> Handle(GetParticipantLabelsQuery request, CancellationToken cancellationToken)
1010
{
11-
var connection = sqlConnectionFactory.GetOpenConnection();
11+
using var connection = sqlConnectionFactory.CreateOpenConnection();
1212

1313
var sql = """
1414
select

src/Application/Features/ParticipantLabels/ParticipantLabelsCounter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public class ParticipantLabelsCounter(ISqlConnectionFactory sqlConnectionFactory
99
{
1010
public int CountOpenLabels(ParticipantId participantId, LabelId labelId)
1111
{
12-
var connection = sqlConnectionFactory.GetOpenConnection();
12+
using var connection = sqlConnectionFactory.CreateOpenConnection();
1313

1414
const string sql = """
1515
SELECT COUNT(*)

src/Infrastructure/Persistence/SqlConnectionFactory.cs

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,9 @@
33

44
namespace Cfo.Cats.Infrastructure.Persistence;
55

6-
public class SqlConnectionFactory(string connectionString) : ISqlConnectionFactory, IDisposable
6+
public class SqlConnectionFactory(string connectionString) : ISqlConnectionFactory
77
{
8-
private IDbConnection? _connection;
9-
10-
public IDbConnection GetOpenConnection()
11-
{
12-
if (this._connection is { State: ConnectionState.Open })
13-
{
14-
return _connection;
15-
}
16-
17-
_connection = new SqlConnection(connectionString);
18-
_connection.Open();
19-
20-
return _connection;
21-
}
22-
23-
public IDbConnection CreateNewConnection()
8+
public IDbConnection CreateOpenConnection()
249
{
2510
var connection = new SqlConnection(connectionString);
2611
connection.Open();
@@ -29,12 +14,4 @@ public IDbConnection CreateNewConnection()
2914
}
3015

3116
public string GetConnectionString() => connectionString;
32-
33-
public void Dispose()
34-
{
35-
if (this._connection != null && this._connection.State == ConnectionState.Open)
36-
{
37-
this._connection.Dispose();
38-
}
39-
}
4017
}

test/Application.UnitTests/Labels/LabelCounterTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public void CountParticipants_ShouldReturnExpectedCount(int expectedCount)
2929
.Returns(expectedCount);
3030

3131
var sqlFactory = new Mock<ISqlConnectionFactory>();
32-
sqlFactory.Setup(x => x.GetOpenConnection()).Returns(connection.Object);
32+
sqlFactory.Setup(x => x.CreateOpenConnection()).Returns(connection.Object);
3333

3434
var counter = new LabelCounter(sqlFactory.Object);
3535
var labelId = new LabelId(Guid.NewGuid());

test/Application.UnitTests/ParticipantLabels/ParticipantLabelsCounterTests.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ public void Constructor_WithValidFactory_ShouldCreateInstance()
2323

2424
private class TestSqlConnectionFactory : ISqlConnectionFactory
2525
{
26-
public IDbConnection GetOpenConnection() => null!;
27-
public IDbConnection CreateNewConnection() => null!;
26+
public IDbConnection CreateOpenConnection() => null!;
2827
public string GetConnectionString() => "";
2928
}
3029
}

0 commit comments

Comments
 (0)