Skip to content

Commit

Permalink
Ensuring the thread-safe in the AggregateRootWithEventSourcing implem…
Browse files Browse the repository at this point in the history
…entation. Adding the sdk projects.
  • Loading branch information
daxnet committed Mar 29, 2018
1 parent 7f9eec2 commit d2b7759
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 6 deletions.
21 changes: 20 additions & 1 deletion Apworks.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.27130.0
VisualStudioVersion = 15.0.27130.2027
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Apworks", "src\Apworks\Apworks.csproj", "{9D0B1FC5-57E6-4A60-8AFD-CDCAEC271A3A}"
EndProject
Expand Down Expand Up @@ -45,6 +45,14 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Apworks.Messaging.RabbitMQ"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Apworks.EventStore.SQLServer", "src\Apworks.EventStore.SQLServer\Apworks.EventStore.SQLServer.csproj", "{2AAF367E-2566-4FF1-B569-6CCA2438A1F6}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tools", "tools", "{A82F0A1A-EBE8-4D15-B688-BC5FCD7B05EF}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "sdk", "sdk", "{1D842584-A31F-4D70-9362-090C3B37D95F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Apworks.Sdk.Cli", "tools\sdk\Apworks.Sdk.Cli\Apworks.Sdk.Cli.csproj", "{742F06FA-B2E4-4ECF-A97D-7005FC51C93F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Apworks.Sdk.Framework", "tools\sdk\Apworks.Sdk.Framework\Apworks.Sdk.Framework.csproj", "{BCCB4E7A-3DDC-4A8D-ABEA-81589BDFC3EC}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -111,6 +119,14 @@ Global
{2AAF367E-2566-4FF1-B569-6CCA2438A1F6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2AAF367E-2566-4FF1-B569-6CCA2438A1F6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2AAF367E-2566-4FF1-B569-6CCA2438A1F6}.Release|Any CPU.Build.0 = Release|Any CPU
{742F06FA-B2E4-4ECF-A97D-7005FC51C93F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{742F06FA-B2E4-4ECF-A97D-7005FC51C93F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{742F06FA-B2E4-4ECF-A97D-7005FC51C93F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{742F06FA-B2E4-4ECF-A97D-7005FC51C93F}.Release|Any CPU.Build.0 = Release|Any CPU
{BCCB4E7A-3DDC-4A8D-ABEA-81589BDFC3EC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BCCB4E7A-3DDC-4A8D-ABEA-81589BDFC3EC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BCCB4E7A-3DDC-4A8D-ABEA-81589BDFC3EC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BCCB4E7A-3DDC-4A8D-ABEA-81589BDFC3EC}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -131,6 +147,9 @@ Global
{558BF7B3-71E9-40B5-87D1-D82DA22454B5} = {33C69FA4-CFEA-480D-924A-37F2190DC20D}
{BC0C9EDE-D0DE-4B51-8267-A59D53EC51CF} = {33C69FA4-CFEA-480D-924A-37F2190DC20D}
{2AAF367E-2566-4FF1-B569-6CCA2438A1F6} = {33C69FA4-CFEA-480D-924A-37F2190DC20D}
{1D842584-A31F-4D70-9362-090C3B37D95F} = {A82F0A1A-EBE8-4D15-B688-BC5FCD7B05EF}
{742F06FA-B2E4-4ECF-A97D-7005FC51C93F} = {1D842584-A31F-4D70-9362-090C3B37D95F}
{BCCB4E7A-3DDC-4A8D-ABEA-81589BDFC3EC} = {1D842584-A31F-4D70-9362-090C3B37D95F}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {4B26F4F2-C673-4523-AAF3-6FBC5A84C2D9}
Expand Down
17 changes: 12 additions & 5 deletions src/Apworks/AggregateRootWithEventSourcing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public abstract class AggregateRootWithEventSourcing<TKey> : IAggregateRootWithE
private TKey id;
private readonly Queue<IDomainEvent> uncommittedEvents = new Queue<IDomainEvent>();
private readonly Lazy<ConcurrentDictionary<string, IEnumerable<MethodInfo>>> eventHandlerRegistrations = new Lazy<ConcurrentDictionary<string, IEnumerable<MethodInfo>>>();
private readonly object syncLocker = new object();
private long persistedVersion;
#endregion

Expand Down Expand Up @@ -185,8 +186,11 @@ private void CopyFromEvent<TEvent>(TEvent @event)
protected void Raise<TEvent>(TEvent @event)
where TEvent : class, IDomainEvent
{
@event.Sequence = this.Version + 1;
this.uncommittedEvents.Enqueue(@event);
lock (this.syncLocker)
{
@event.Sequence = this.Version + 1;
this.uncommittedEvents.Enqueue(@event);
}
}

/// <summary>
Expand Down Expand Up @@ -300,10 +304,13 @@ public override int GetHashCode()
/// </summary>
void IPurgeable.Purge()
{
if (this.uncommittedEvents.Count > 0)
lock (this.syncLocker)
{
this.PersistedVersion = this.persistedVersion + this.uncommittedEvents.Count;
this.uncommittedEvents.Clear();
if (this.uncommittedEvents.Count > 0)
{
this.PersistedVersion = this.persistedVersion + this.uncommittedEvents.Count;
this.uncommittedEvents.Clear();
}
}
}

Expand Down
12 changes: 12 additions & 0 deletions tools/sdk/Apworks.Sdk.Cli/Apworks.Sdk.Cli.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Apworks.Sdk.Framework\Apworks.Sdk.Framework.csproj" />
</ItemGroup>

</Project>
12 changes: 12 additions & 0 deletions tools/sdk/Apworks.Sdk.Cli/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;

namespace Apworks.Sdk.Cli
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}
7 changes: 7 additions & 0 deletions tools/sdk/Apworks.Sdk.Framework/Apworks.Sdk.Framework.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>

</Project>
8 changes: 8 additions & 0 deletions tools/sdk/Apworks.Sdk.Framework/Class1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using System;

namespace Apworks.Sdk.Framework
{
public class Class1
{
}
}

0 comments on commit d2b7759

Please sign in to comment.