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+ }
0 commit comments