Skip to content

Commit aad2f59

Browse files
Merge pull request #10 from StuartFerguson/task/#8_multipersistence
minor crime fix
2 parents 01f3d04 + 1f781ad commit aad2f59

2 files changed

Lines changed: 151 additions & 124 deletions

File tree

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
namespace Shared.EventStore.EventStore
2+
{
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Threading;
6+
using DomainDrivenDesign.EventStore;
7+
using Microsoft.Extensions.Logging;
8+
using Repositories;
9+
10+
/// <summary>
11+
///
12+
/// </summary>
13+
/// <seealso cref="Shared.EventStore.EventStore.IEventStoreContextManager" />
14+
public class EventStoreContextManager : IEventStoreContextManager
15+
{
16+
#region Fields
17+
18+
/// <summary>
19+
/// The connection string configuration repository
20+
/// </summary>
21+
private readonly IConnectionStringConfigurationRepository ConnectionStringConfigurationRepository;
22+
23+
/// <summary>
24+
/// The context
25+
/// </summary>
26+
private readonly IEventStoreContext Context;
27+
28+
/// <summary>
29+
/// The event store context function
30+
/// </summary>
31+
private readonly Func<String, IEventStoreContext> EventStoreContextFunc;
32+
33+
/// <summary>
34+
/// The event store contexts
35+
/// </summary>
36+
private readonly Dictionary<String, IEventStoreContext> EventStoreContexts;
37+
38+
//TODO static?
39+
/// <summary>
40+
/// The padlock
41+
/// </summary>
42+
private readonly Object padlock = new Object();
43+
44+
#endregion
45+
46+
#region Constructors
47+
48+
/// <summary>
49+
/// Initializes a new instance of the <see cref="EventStoreContextManager" /> class.
50+
/// </summary>
51+
/// <param name="eventStoreContextFunc">The event store context function.</param>
52+
/// <param name="connectionStringConfigurationRepository">The connection string configuration repository.</param>
53+
public EventStoreContextManager(Func<String, IEventStoreContext> eventStoreContextFunc,
54+
IConnectionStringConfigurationRepository connectionStringConfigurationRepository)
55+
{
56+
this.EventStoreContexts = new Dictionary<String, IEventStoreContext>();
57+
this.EventStoreContextFunc = eventStoreContextFunc;
58+
this.ConnectionStringConfigurationRepository = connectionStringConfigurationRepository;
59+
}
60+
61+
/// <summary>
62+
/// Initializes a new instance of the <see cref="EventStoreContextManager" /> class.
63+
/// </summary>
64+
/// <param name="eventStoreContext">The event store context.</param>
65+
public EventStoreContextManager(IEventStoreContext eventStoreContext)
66+
{
67+
this.Context = eventStoreContext;
68+
}
69+
70+
#endregion
71+
72+
#region Events
73+
74+
/// <summary>
75+
/// Occurs when [trace generated].
76+
/// </summary>
77+
public event TraceHandler TraceGenerated;
78+
79+
#endregion
80+
81+
#region Methods
82+
83+
/// <summary>
84+
/// Gets the event store context.
85+
/// </summary>
86+
/// <param name="connectionIdentifier">The connection identifier.</param>
87+
/// <returns></returns>
88+
public IEventStoreContext GetEventStoreContext(String connectionIdentifier)
89+
{
90+
if (this.Context != null)
91+
{
92+
return this.Context;
93+
}
94+
95+
this.WriteTrace($"No resolved context found, about to resolve one using connectionIdentifier {connectionIdentifier}");
96+
97+
if (this.EventStoreContexts.ContainsKey(connectionIdentifier))
98+
{
99+
return this.EventStoreContexts[connectionIdentifier];
100+
}
101+
102+
this.WriteTrace($"Creating a new EventStoreContext for connectionIdentifier {connectionIdentifier}");
103+
104+
lock(this.padlock)
105+
{
106+
if (!this.EventStoreContexts.ContainsKey(connectionIdentifier))
107+
{
108+
// This will need to now look up the ES Connection string from persistence
109+
String connectionString = this.ConnectionStringConfigurationRepository
110+
.GetConnectionString(connectionIdentifier, ConnectionStringType.EventStore, CancellationToken.None).Result;
111+
112+
this.WriteTrace($"Connection String is {connectionString}");
113+
114+
IEventStoreContext eventStoreContext = this.EventStoreContextFunc(connectionString);
115+
116+
this.EventStoreContexts.Add(connectionIdentifier, eventStoreContext);
117+
}
118+
119+
return this.EventStoreContexts[connectionIdentifier];
120+
}
121+
}
122+
123+
/// <summary>
124+
/// Guards the against no connection identifier.
125+
/// </summary>
126+
/// <param name="connectionIdentifier">The connection identifier.</param>
127+
/// <exception cref="ArgumentException">Value cannot be empty. - connectionIdentifier</exception>
128+
private void GuardAgainstNoConnectionIdentifier(String connectionIdentifier)
129+
{
130+
//Check if the connectionStringIdentifier is present
131+
if (string.IsNullOrEmpty(connectionIdentifier))
132+
{
133+
throw new ArgumentException("Value cannot be empty.", nameof(connectionIdentifier));
134+
}
135+
}
136+
137+
/// <summary>
138+
/// Writes the trace.
139+
/// </summary>
140+
/// <param name="trace">The trace.</param>
141+
private void WriteTrace(String trace)
142+
{
143+
if (this.TraceGenerated != null)
144+
{
145+
this.TraceGenerated(trace, LogLevel.Information);
146+
}
147+
}
148+
149+
#endregion
150+
}
151+
}
Lines changed: 0 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -1,137 +1,13 @@
11
using System;
2-
using System.Collections.Generic;
32
using System.Text;
43

54
namespace Shared.EventStore.EventStore
65
{
76
using System.Diagnostics;
8-
using System.Threading;
97
using DomainDrivenDesign.EventStore;
10-
using Microsoft.Extensions.Logging;
11-
using Shared.Repositories;
128

139
public interface IEventStoreContextManager
1410
{
1511
IEventStoreContext GetEventStoreContext(String connectionIdentifier);
1612
}
17-
18-
public class EventStoreContextManager : IEventStoreContextManager
19-
{
20-
#region Fields
21-
22-
/// <summary>
23-
/// The event store context function
24-
/// </summary>
25-
private readonly Func<String, IEventStoreContext> EventStoreContextFunc;
26-
27-
private readonly IConnectionStringConfigurationRepository ConnectionStringConfigurationRepository;
28-
29-
/// <summary>
30-
/// The event store contexts
31-
/// </summary>
32-
private readonly Dictionary<String, IEventStoreContext> EventStoreContexts;
33-
34-
/// <summary>
35-
/// The context
36-
/// </summary>
37-
private readonly IEventStoreContext Context;
38-
39-
//TODO static?
40-
/// <summary>
41-
/// The padlock
42-
/// </summary>
43-
private readonly Object padlock = new Object();
44-
45-
/// <summary>
46-
/// Occurs when [trace generated].
47-
/// </summary>
48-
public event TraceHandler TraceGenerated;
49-
50-
#endregion
51-
52-
#region Constructors
53-
54-
/// <summary>
55-
/// Initializes a new instance of the <see cref="EventStoreContextManager" /> class.
56-
/// </summary>
57-
/// <param name="eventStoreContextFunc">The event store context function.</param>
58-
public EventStoreContextManager(Func<String, IEventStoreContext> eventStoreContextFunc,
59-
IConnectionStringConfigurationRepository connectionStringConfigurationRepository)
60-
{
61-
this.EventStoreContexts = new Dictionary<String, IEventStoreContext>();
62-
this.EventStoreContextFunc = eventStoreContextFunc;
63-
this.ConnectionStringConfigurationRepository = connectionStringConfigurationRepository;
64-
}
65-
66-
/// <summary>
67-
/// Initializes a new instance of the <see cref="EventStoreContextManager"/> class.
68-
/// </summary>
69-
/// <param name="eventStoreContext">The event store context.</param>
70-
public EventStoreContextManager(IEventStoreContext eventStoreContext)
71-
{
72-
this.Context = eventStoreContext;
73-
}
74-
75-
#endregion
76-
77-
#region Methods
78-
79-
public IEventStoreContext GetEventStoreContext(String connectionIdentifier)
80-
{
81-
if (this.Context != null)
82-
{
83-
return this.Context;
84-
}
85-
86-
this.WriteTrace($"No resolved context found, about to resolve one using connectionIdentifier {connectionIdentifier}");
87-
88-
if (this.EventStoreContexts.ContainsKey(connectionIdentifier))
89-
{
90-
return this.EventStoreContexts[connectionIdentifier.ToString()];
91-
}
92-
93-
this.WriteTrace($"Creating a new EventStoreContext for connectionIdentifier {connectionIdentifier}");
94-
95-
lock (this.padlock)
96-
{
97-
if (!this.EventStoreContexts.ContainsKey(connectionIdentifier))
98-
{
99-
// This will need to now look up the ES Connection string from persistence
100-
String connectionString = this.ConnectionStringConfigurationRepository.GetConnectionString(connectionIdentifier, ConnectionStringType.EventStore, CancellationToken.None).Result;
101-
102-
//this.WriteTrace($"Connection String is {connectionString}");
103-
104-
//IEventStoreContext eventStoreContext = this.EventStoreContextFunc(connectionString);
105-
106-
//this.EventStoreContexts.Add(connectionStringId, eventStoreContext);
107-
}
108-
109-
return this.EventStoreContexts[connectionIdentifier];
110-
}
111-
}
112-
113-
114-
/// <summary>
115-
/// Writes the trace.
116-
/// </summary>
117-
/// <param name="trace">The trace.</param>
118-
private void WriteTrace(String trace)
119-
{
120-
if (this.TraceGenerated != null)
121-
{
122-
this.TraceGenerated(trace, LogLevel.Information);
123-
}
124-
}
125-
126-
private void GuardAgainstNoConnectionIdentifier(String connectionIdentifier)
127-
{
128-
//Check if the connectionStringIdentifier is present
129-
if (String.IsNullOrEmpty(connectionIdentifier))
130-
{
131-
throw new ArgumentException("Value cannot be empty.", nameof(connectionIdentifier));
132-
}
133-
}
134-
135-
#endregion
136-
}
13713
}

0 commit comments

Comments
 (0)