Skip to content

Commit 55e6038

Browse files
Merge branch 'master' into task/#13_loggerredesign
2 parents 77aa75e + aad2f59 commit 55e6038

17 files changed

Lines changed: 949 additions & 0 deletions
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
namespace Shared.EventStore.EventStore
2+
{
3+
using System;
4+
using DomainDrivenDesign.EventStore;
5+
6+
public class AggregateRepositoryManager : IAggregateRepositoryManager
7+
{
8+
#region Fields
9+
10+
/// <summary>
11+
/// The event store context manager
12+
/// </summary>
13+
private readonly IEventStoreContextManager EventStoreContextManager;
14+
15+
#endregion
16+
17+
#region Constructors
18+
19+
/// <summary>
20+
/// Initializes a new instance of the <see cref="AggregateRepositoryManager" /> class.
21+
/// </summary>
22+
/// <param name="eventStoreContextManager">The event store context manager.</param>
23+
public AggregateRepositoryManager(IEventStoreContextManager eventStoreContextManager)
24+
{
25+
this.EventStoreContextManager = eventStoreContextManager;
26+
}
27+
28+
#endregion
29+
30+
#region Methods
31+
32+
/// <summary>
33+
/// Gets the aggregate repository.
34+
/// </summary>
35+
/// <typeparam name="T"></typeparam>
36+
/// <param name="identifier">The identifier.</param>
37+
/// <returns></returns>
38+
public IAggregateRepository<T> GetAggregateRepository<T>(Guid identifier) where T : Aggregate, new()
39+
{
40+
IEventStoreContext context = this.EventStoreContextManager.GetEventStoreContext(identifier.ToString());
41+
42+
return new AggregateRepository<T>(context);
43+
}
44+
45+
#endregion
46+
}
47+
}
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: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace Shared.EventStore.EventStore
6+
{
7+
using DomainDrivenDesign.EventStore;
8+
9+
public interface IAggregateRepositoryManager
10+
{
11+
#region Methods
12+
13+
/// <summary>
14+
/// Gets the aggregate repository.
15+
/// </summary>
16+
/// <typeparam name="T"></typeparam>
17+
/// <param name="identifier">The identifier.</param>
18+
/// <returns></returns>
19+
IAggregateRepository<T> GetAggregateRepository<T>(Guid identifier) where T : Aggregate, new();
20+
21+
#endregion
22+
}
23+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
using System.Text;
3+
4+
namespace Shared.EventStore.EventStore
5+
{
6+
using System.Diagnostics;
7+
using DomainDrivenDesign.EventStore;
8+
9+
public interface IEventStoreContextManager
10+
{
11+
IEventStoreContext GetEventStoreContext(String connectionIdentifier);
12+
}
13+
}

Shared.EventStore/Shared.EventStore.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
<ItemGroup>
1414
<ProjectReference Include="..\Shared.DomainDrivenDesign\Shared.DomainDrivenDesign.csproj" />
15+
<ProjectReference Include="..\Shared\Shared.csproj" />
1516
</ItemGroup>
1617

1718
</Project>
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.ComponentModel.DataAnnotations;
4+
using System.Text;
5+
6+
namespace Shared.EntityFramework.ConnectionStringConfiguration
7+
{
8+
using System.ComponentModel.DataAnnotations.Schema;
9+
10+
public class ConnectionStringConfiguration
11+
{
12+
/// <summary>
13+
/// Gets or sets the identifier.
14+
/// </summary>
15+
/// <value>
16+
/// The identifier.
17+
/// </value>
18+
[Key]
19+
public Guid Id { get; set; }
20+
21+
/// <summary>
22+
/// Gets or sets the connection string identifier.
23+
/// </summary>
24+
/// <value>
25+
/// The connection string identifier.
26+
/// </value>
27+
[Required]
28+
[Column("externalIdentifier")]
29+
public String ExternalIdentifier { get; set; }
30+
31+
/// <summary>
32+
/// Gets or sets the connection string type identifier.
33+
/// </summary>
34+
/// <value>
35+
/// The connection string type identifier.
36+
/// </value>
37+
public Int32 ConnectionStringTypeId { get; set; }
38+
39+
/// <summary>
40+
/// Gets or sets the type of the connection string.
41+
/// </summary>
42+
/// <value>
43+
/// The type of the connection string.
44+
/// </value>
45+
[ForeignKey(nameof(ConnectionStringTypeId))]
46+
public virtual ConnectionStringType ConnectionStringType { get; set; }
47+
48+
49+
/// <summary>
50+
/// Gets or sets the connection string.
51+
/// </summary>
52+
/// <value>
53+
/// The connection string.
54+
/// </value>
55+
[Required]
56+
[Column("connectionString")]
57+
public String ConnectionString { get; set; }
58+
}
59+
}

0 commit comments

Comments
 (0)