Skip to content
This repository has been archived by the owner on Dec 8, 2022. It is now read-only.

Commit

Permalink
实现EFCore事件存储
Browse files Browse the repository at this point in the history
  • Loading branch information
ElderJames committed Aug 22, 2017
1 parent 1597b9b commit 65ce4b5
Show file tree
Hide file tree
Showing 25 changed files with 383 additions and 76 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -258,4 +258,5 @@ paket-files/

# Python Tools for Visual Studio (PTVS)
__pycache__/
*.pyc
*.pyc
*.db
21 changes: 14 additions & 7 deletions ShriekFx.sln
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Shriek", "src\Shriek\Shriek
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Shriek.EntityFrameworkCore", "src\Shriek.EntityFrameworkCore\Shriek.EntityFrameworkCore.csproj", "{6ABF879A-04C4-4701-84C7-5FCEC2986F30}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Shriek.EventStorage.EF", "src\Shriek.EventStorage.EF\Shriek.EventStorage.EF.csproj", "{157CF3F6-25BC-4D60-B76F-38CB7BAD480F}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Shriek.Test", "test\Shriek.Test\Shriek.Test.csproj", "{5895FBBA-5229-4CDE-A17B-4CE9A4F70A64}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Shriek.Samples.InProcess", "samples\Shriek.Samples.InProcess\Shriek.Samples.InProcess.csproj", "{2A5692B8-9F85-41A4-87BE-715F9607A94B}"
Expand All @@ -26,6 +24,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
README.md = README.md
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Shriek.EventStorage.EFCore", "src\Shriek.EventStorage.EFCore\Shriek.EventStorage.EFCore.csproj", "{60117DDA-35FA-49A5-8BCC-7A404B67D37D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Shriek.Sample.EventStorage.EFCore", "samples\Shriek.Sample.EventStorage.EFCore\Shriek.Sample.EventStorage.EFCore.csproj", "{A00F88AB-51EB-4FF4-A926-A4A56B5653F8}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -40,10 +42,6 @@ Global
{6ABF879A-04C4-4701-84C7-5FCEC2986F30}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6ABF879A-04C4-4701-84C7-5FCEC2986F30}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6ABF879A-04C4-4701-84C7-5FCEC2986F30}.Release|Any CPU.Build.0 = Release|Any CPU
{157CF3F6-25BC-4D60-B76F-38CB7BAD480F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{157CF3F6-25BC-4D60-B76F-38CB7BAD480F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{157CF3F6-25BC-4D60-B76F-38CB7BAD480F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{157CF3F6-25BC-4D60-B76F-38CB7BAD480F}.Release|Any CPU.Build.0 = Release|Any CPU
{5895FBBA-5229-4CDE-A17B-4CE9A4F70A64}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5895FBBA-5229-4CDE-A17B-4CE9A4F70A64}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5895FBBA-5229-4CDE-A17B-4CE9A4F70A64}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand All @@ -56,17 +54,26 @@ Global
{92E69FD4-B29F-45AB-A525-A2A1248926A9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{92E69FD4-B29F-45AB-A525-A2A1248926A9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{92E69FD4-B29F-45AB-A525-A2A1248926A9}.Release|Any CPU.Build.0 = Release|Any CPU
{60117DDA-35FA-49A5-8BCC-7A404B67D37D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{60117DDA-35FA-49A5-8BCC-7A404B67D37D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{60117DDA-35FA-49A5-8BCC-7A404B67D37D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{60117DDA-35FA-49A5-8BCC-7A404B67D37D}.Release|Any CPU.Build.0 = Release|Any CPU
{A00F88AB-51EB-4FF4-A926-A4A56B5653F8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A00F88AB-51EB-4FF4-A926-A4A56B5653F8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A00F88AB-51EB-4FF4-A926-A4A56B5653F8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A00F88AB-51EB-4FF4-A926-A4A56B5653F8}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{3A472EE4-71FA-4FFF-903A-8AE145CCFAD3} = {462AFBD1-F8B2-475E-988F-13E968C4EA15}
{6ABF879A-04C4-4701-84C7-5FCEC2986F30} = {462AFBD1-F8B2-475E-988F-13E968C4EA15}
{157CF3F6-25BC-4D60-B76F-38CB7BAD480F} = {462AFBD1-F8B2-475E-988F-13E968C4EA15}
{5895FBBA-5229-4CDE-A17B-4CE9A4F70A64} = {D3955132-9211-478D-A295-983180F3894C}
{2A5692B8-9F85-41A4-87BE-715F9607A94B} = {D89488AA-4409-4E15-9945-A918008FBA0A}
{92E69FD4-B29F-45AB-A525-A2A1248926A9} = {D89488AA-4409-4E15-9945-A918008FBA0A}
{60117DDA-35FA-49A5-8BCC-7A404B67D37D} = {462AFBD1-F8B2-475E-988F-13E968C4EA15}
{A00F88AB-51EB-4FF4-A926-A4A56B5653F8} = {D89488AA-4409-4E15-9945-A918008FBA0A}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {9B7121CB-4CDF-4D0B-82C7-736ABF500F14}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Shriek.Notifications;

namespace Shriek.Sample.EventStorage.EFCore.Notifications
{
public class DomainNotificationHandler : IDomainNotificationHandler<DomainNotification>
{
private List<DomainNotification> _notifications;

public DomainNotificationHandler()
{
_notifications = new List<DomainNotification>();
}

public void Handle(DomainNotification message)
{
Console.WriteLine("exception:" + message.Key + ":" + message.Value);
_notifications.Add(message);
}

public List<DomainNotification> Notifications
{
get => _notifications;
}

public bool NotEmpty
{
get => Notifications.Any();
}

public void Dispose()
{
_notifications = new List<DomainNotification>();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using Shriek.Storage;
using Shriek.Samples.Aggregates;
using Shriek.Samples.Commands;
using Shriek.Commands;
using System;
using System.Collections.Generic;
using System.Text;

namespace Shriek.Sample.EventStorage.EFCore.Handlers
{
public class TodoCommandHandler : ICommandHandler<CreateTodoCommand>,
ICommandHandler<ChangeTodoCommand>
{
public TodoCommandHandler()
{
}

public void Execute(ICommandContext context, CreateTodoCommand command)
{
var root = context.GetAggregateRoot(command.AggregateId, () => TodoAggregateRoot.Register(command));
}

public void Execute(ICommandContext context, ChangeTodoCommand command)
{
var root = context.GetAggregateRoot<TodoAggregateRoot>(command.AggregateId);
if (root == null) return;
root.Change(command);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System.Diagnostics;
using System.Threading;
using Shriek.Samples.Events;
using Shriek.Events;

namespace Shriek.Sample.EventStorage.EFCore.Handlers
{
public class TodoEventHandler : IEventHandler<TodoCreatedEvent>, IEventHandler<TodoChangedEvent>
{
public void Handle(TodoCreatedEvent e)
{
System.Console.WriteLine($"here is {nameof(TodoCreatedEvent)}:" + e.Name);
Thread.Sleep(5000);
System.Console.WriteLine($"{e.Name} finished!");
}

public void Handle(TodoChangedEvent e)
{
System.Console.WriteLine($"here is {nameof(TodoCreatedEvent)}:" + e.Name);
Thread.Sleep(5000);
System.Console.WriteLine($"{e.Name} finished!");
}
}
}
81 changes: 81 additions & 0 deletions samples/Shriek.Sample.EventStorage.EFCore/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using System;
using Microsoft.Extensions.DependencyInjection;
using Shriek.Commands;
using Shriek.Samples.Commands;
using Shriek.EventStorage.EFCore;
using Microsoft.EntityFrameworkCore;

namespace Shriek.Sample.EventStorage.EFCore
{
internal class Program
{
private static void Main(string[] args)
{
var services = new ServiceCollection();
var connectionStringBuilder = new Microsoft.Data.Sqlite.SqliteConnectionStringBuilder { DataSource = "shriek.sample.db" };
var connectionString = connectionStringBuilder.ToString();

services.AddShriek();
services.AddEFCoreEventStorage(options =>
options.UseSqlite(connectionString));

var container = services.BuildServiceProvider();

var bus = container.GetService<ICommandBus>();

var id = Guid.NewGuid();

bus.Send(new CreateTodoCommand(id)
{
Name = "get up",
Desception = "good day",
FinishTime = DateTime.Now.AddDays(1)
});

Console.WriteLine($"{nameof(CreateTodoCommand)} sended!");

bus.Send(new ChangeTodoCommand(id)
{
Name = "eat breakfast",
Desception = "yummy!",
FinishTime = DateTime.Now.AddDays(1)
});

Console.WriteLine($"{nameof(ChangeTodoCommand)} sended!");

bus.Send(new ChangeTodoCommand(id)
{
Name = "go to work",
Desception = "fighting!",
FinishTime = DateTime.Now.AddDays(1)
});
Console.WriteLine($"{nameof(ChangeTodoCommand)} sended!");

bus.Send(new ChangeTodoCommand(id)
{
Name = "call boss",
Desception = "haha!",
FinishTime = DateTime.Now.AddDays(1)
});
Console.WriteLine($"{nameof(ChangeTodoCommand)} sended!");

bus.Send(new ChangeTodoCommand(id)
{
Name = "coding",
Desception = "great!",
FinishTime = DateTime.Now.AddDays(1)
});
Console.WriteLine($"{nameof(ChangeTodoCommand)} sended!");

bus.Send(new ChangeTodoCommand(id)
{
Name = "drive car",
Desception = "be careful!",
FinishTime = DateTime.Now.AddDays(-1)
});
Console.WriteLine($"{nameof(ChangeTodoCommand)} sended!");

Console.ReadKey();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

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

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Shriek.EventStorage.EFCore\Shriek.EventStorage.EFCore.csproj" />
<ProjectReference Include="..\..\src\Shriek\Shriek.csproj" />
<ProjectReference Include="..\Shriek.Samples\Shriek.Samples.csproj" />
</ItemGroup>

</Project>
21 changes: 21 additions & 0 deletions src/Shriek.EventStorage.EFCore/EventStorageEFCoreExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Shriek.EventSourcing;
using Shriek.Storage;
using Shriek.EventSourcing.Sql.EFCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Text;

namespace Shriek.EventStorage.EFCore
{
public static class EventStorageEFCoreExtensions
{
public static void AddEFCoreEventStorage(this IServiceCollection services, Action<DbContextOptionsBuilder> optionsAction = null)
{
services.AddDbContext<EventStorageSQLContext>(optionsAction);
services.AddScoped<IEventStorageRepository, EventStorageSQLRepository>();
services.AddScoped<IEventStorage, SqlEventStorage>();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Remove="StoredEvent.cs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="2.0.0" />
Expand Down
23 changes: 23 additions & 0 deletions src/Shriek.EventStorage.EFCore/StoredEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Shriek.Events
{
public class StoredEvent : Event
{
public StoredEvent(Guid aggregateId, string data, string user)
{
AggregateId = aggregateId;
Data = data;
User = user;
}

// EF Constructor
protected StoredEvent() { }

public string Data { get; private set; }

public string User { get; private set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
using System;
using System.Collections.Generic;
using System.Text;
using Shriek.Events;
using Shriek.Events;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Shriek.EntityFrameworkCore;

namespace Shriek.EventStorage.EF
{
Expand Down
5 changes: 3 additions & 2 deletions src/Shriek/Events/Event.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Text;
using Shriek.Messages;

Expand All @@ -9,7 +10,7 @@ public class Event : Message, IEvent<Guid>
{
public Event()
{
this.EventType = GetType().Name;
this.EventType = GetType().FullName;
this.Timestamp = DateTime.Now;
}

Expand All @@ -19,6 +20,6 @@ public Event()

public DateTime Timestamp { get; private set; }

public string EventType { get; private set; }
public string EventType { get; protected set; }
}
}
Loading

0 comments on commit 65ce4b5

Please sign in to comment.