Skip to content

Commit 908c4fe

Browse files
committed
Removed the usage of IConnectionFactory and cleaned up the connection handling
1 parent b10a0c4 commit 908c4fe

File tree

35 files changed

+315
-774
lines changed

35 files changed

+315
-774
lines changed

src/Server/Coderr.Server.App.Tests/Core/Accounts/CommandHandlers/RegisterAccountHandlerTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public async Task Should_inform_the_rest_of_the_system_about_the_new_account()
4545
await sut.HandleAsync(context, cmd);
4646

4747
await context.Received().SendAsync(Arg.Any<AccountRegistered>());
48-
context.Method("PublishAsync").Arg<AccountRegistered>().AccountId.Should().Be(3);
48+
context.Method("SendAsync").Arg<AccountRegistered>().AccountId.Should().Be(3);
4949
}
5050

5151
[Fact]

src/Server/Coderr.Server.App.Tests/Core/Applications/Commands/InviteUserHandlerTests.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,24 +66,24 @@ public void Regular_user_should_not_be_able_to_invite()
6666
var cmd = new InviteUser(1, "[email protected]") { UserId = 1 };
6767
var members = new[] { new ApplicationTeamMember(1, 3, "karl") };
6868
_applicationRepository.GetTeamMembersAsync(1).Returns(members);
69-
_context.Principal.Returns(CreateAdminPrincipal());
69+
_context.Principal.Returns(CreateUserPrincipal());
7070

7171
Func<Task> actual = async () => await _sut.HandleAsync(_context, cmd);
7272

7373
actual.ShouldThrow<SecurityException>();
7474
}
7575

7676
[Fact]
77-
public void Sysadmin_should_be_able_To_invite_users()
77+
public async Task Sysadmin_should_be_able_To_invite_users()
7878
{
7979
var cmd = new InviteUser(1, "[email protected]") { UserId = 1 };
8080
var members = new[] { new ApplicationTeamMember(3, 3, "karl") };
8181
_applicationRepository.GetTeamMembersAsync(1).Returns(members);
8282
_context.Principal.Returns(CreateAdminPrincipal());
8383

84-
Func<Task> actual = async () => await _sut.HandleAsync(_context, cmd);
84+
await _sut.HandleAsync(_context, cmd);
8585

86-
actual.ShouldThrow<SecurityException>();
86+
await _context.Received().SendAsync(Arg.Any<UserInvitedToApplication>());
8787
}
8888

8989
[Fact]
@@ -156,5 +156,14 @@ private ClaimsPrincipal CreateAdminPrincipal()
156156
var identity = new ClaimsIdentity(claims);
157157
return new ClaimsPrincipal(identity);
158158
}
159+
160+
private ClaimsPrincipal CreateUserPrincipal()
161+
{
162+
var claims = new List<Claim>
163+
{
164+
};
165+
var identity = new ClaimsIdentity(claims);
166+
return new ClaimsPrincipal(identity);
167+
}
159168
}
160169
}

src/Server/Coderr.Server.Infrastructure/Configuration/Database/DatabaseStore.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ namespace codeRR.Server.Infrastructure.Configuration.Database
1616
public class DatabaseStore : ConfigurationStore
1717
{
1818
private readonly Dictionary<Type, Wrapper> _items = new Dictionary<Type, Wrapper>();
19-
private readonly IConnectionFactory _connectionFactory;
19+
private readonly Func<IDbConnection> _connectionFactory;
2020

21-
public DatabaseStore(IConnectionFactory connectionFactory)
21+
public DatabaseStore(Func<IDbConnection> connectionFactory)
2222
{
2323
_connectionFactory = connectionFactory ?? throw new ArgumentNullException(nameof(connectionFactory));
2424
}
@@ -105,7 +105,7 @@ public override void Store(IConfigurationSection section)
105105
/// <returns>open connection</returns>
106106
protected virtual IDbConnection OpenConnectionFor<T>()
107107
{
108-
return _connectionFactory.Open();
108+
return OpenConnectionFor(typeof(T));
109109
}
110110

111111
/// <summary>
@@ -116,7 +116,7 @@ protected virtual IDbConnection OpenConnectionFor<T>()
116116
protected virtual IDbConnection OpenConnectionFor(Type configClassType)
117117
{
118118
if (configClassType == null) throw new ArgumentNullException(nameof(configClassType));
119-
return _connectionFactory.Open();
119+
return _connectionFactory();
120120
}
121121

122122
private void SetCache(IConfigurationSection section)

src/Server/Coderr.Server.Infrastructure/ConnectionFactory.cs

Lines changed: 3 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -10,52 +10,14 @@ namespace codeRR.Server.Infrastructure
1010
/// <summary>
1111
/// Generates SQL connections
1212
/// </summary>
13-
public class Net452ConnectionFactory : IConnectionFactory
13+
public class DbConnectionFactory
1414
{
15-
private static Func<IDbConnection> _customFactory;
16-
17-
/// <inheritdoc />
18-
public IDbConnection Open()
19-
{
20-
return Create("Db");
21-
}
22-
23-
/// <inheritdoc />
24-
public IDbConnection Open(string connectionStringName)
25-
{
26-
return Create(connectionStringName);
27-
}
28-
29-
/// <inheritdoc />
30-
public IDbConnection TryOpen(string connectionStringName)
31-
{
32-
return Create(connectionStringName, false);
33-
}
34-
35-
3615
/// <summary>
37-
/// For integration tests etc.
38-
/// </summary>
39-
/// <param name="factoryMethod">factory method</param>
40-
/// <param name="caller"></param>
41-
public static void SetConnectionFactory(Func<IDbConnection> factoryMethod,
42-
[CallerMemberName] string caller = "")
43-
{
44-
_customFactory = factoryMethod;
45-
}
46-
47-
/// <summary>
48-
/// Creates a connection using the <c>web.config</c> connection string named <c>Db</c>.
16+
/// Opens a connection
4917
/// </summary>
5018
/// <returns>open connection</returns>
51-
private static IDbConnection Create(string connectionStringName, bool throwIfMissing = true)
19+
public static IDbConnection Open(string connectionStringName, bool throwIfMissing)
5220
{
53-
if (_customFactory != null)
54-
{
55-
var con = _customFactory();
56-
return con;
57-
}
58-
5921
var conStr = ConfigurationManager.ConnectionStrings[connectionStringName];
6022
if (conStr == null)
6123
{

src/Server/Coderr.Server.Infrastructure/IConnectionFactory.cs

Lines changed: 0 additions & 32 deletions
This file was deleted.

src/Server/Coderr.Server.ReportAnalyzer/AnalysisDbContext.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ namespace codeRR.Server.ReportAnalyzer
1010
/// </summary>
1111
public class AnalysisDbContext : IDisposable
1212
{
13-
private readonly IConnectionFactory _connectionFactory;
13+
private readonly Func<IDbConnection> _connectionFactory;
1414
private IDbConnection _con;
1515
private IAdoNetUnitOfWork _unitOfWork;
1616

17-
public AnalysisDbContext(IConnectionFactory connectionFactory)
17+
public AnalysisDbContext(Func<IDbConnection> connectionFactory)
1818
{
1919
_connectionFactory = connectionFactory ?? throw new ArgumentNullException(nameof(connectionFactory));
2020
}
@@ -35,7 +35,7 @@ public IDbConnection Connection
3535
if (_con != null)
3636
return _con;
3737

38-
_con = _connectionFactory.Open();
38+
_con = _connectionFactory();
3939
return _con;
4040
}
4141
}

src/Server/Coderr.Server.ReportAnalyzer/Domain/FailedReports/CustomerApplication.cs

Lines changed: 0 additions & 35 deletions
This file was deleted.

src/Server/Coderr.Server.ReportAnalyzer/Domain/FailedReports/SaveHandler.cs

Lines changed: 0 additions & 110 deletions
This file was deleted.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using codeRR.Server.Api.Core.Feedback.Commands;
4+
using codeRR.Server.ReportAnalyzer.LibContracts;
5+
using DotNetCqs;
6+
using Griffin.Container;
7+
using log4net;
8+
using Newtonsoft.Json;
9+
10+
namespace codeRR.Server.ReportAnalyzer.Handlers
11+
{
12+
[Component]
13+
public class ProcessFeedbackHandler : IMessageHandler<ProcessFeedback>
14+
{
15+
private readonly ILog _logger = LogManager.GetLogger(typeof(ProcessFeedbackHandler));
16+
17+
public async Task HandleAsync(IMessageContext context, ProcessFeedback message)
18+
{
19+
try
20+
{
21+
var submitCmd = new SubmitFeedback(message.ReportId, message.RemoteAddress)
22+
{
23+
CreatedAtUtc = message.ReceivedAtUtc,
24+
Email = message.EmailAddress,
25+
Feedback = message.Description
26+
};
27+
await context.SendAsync(submitCmd);
28+
}
29+
catch (Exception ex)
30+
{
31+
_logger.Error("Failed to process " + JsonConvert.SerializeObject(message), ex);
32+
}
33+
}
34+
}
35+
}

0 commit comments

Comments
 (0)