From 65ce4b5264df7fa1129666603f428c2119938aeb Mon Sep 17 00:00:00 2001 From: ElderJames Date: Tue, 22 Aug 2017 23:56:14 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E7=8E=B0EFCore=E4=BA=8B=E4=BB=B6?= =?UTF-8?q?=E5=AD=98=E5=82=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 +- ShriekFx.sln | 21 +++-- .../Handlers/DomainNotificationHandler.cs | 39 +++++++++ .../Handlers/TodoCommandHandler.cs | 30 +++++++ .../Handlers/TodoEventHandler.cs | 24 ++++++ .../Program.cs | 81 +++++++++++++++++++ .../Shriek.Sample.EventStorage.EFCore.csproj | 19 +++++ .../Class1.cs | 0 .../EventStorageEFCoreExtensions.cs | 21 +++++ .../EventStorageSQLContext.cs | 0 .../EventStorageSQLRepository.cs | 0 .../Shriek.EventStorage.EFCore.csproj} | 4 + src/Shriek.EventStorage.EFCore/StoredEvent.cs | 23 ++++++ .../StoredEventMap.cs | 6 +- src/Shriek/Events/Event.cs | 5 +- .../DomainNotificationHandler.cs | 36 +++++++++ src/Shriek/Shriek.csproj | 4 + src/Shriek/ShriekOption.cs | 10 +++ src/Shriek/Storage/IEventStorage.cs | 2 +- src/Shriek/Storage/InMemoryEventStorage.cs | 2 +- src/Shriek/Storage/SqlEventStorage.cs | 42 ++++++---- src/Shriek/Storage/StoredEvent.cs | 28 +++++++ .../{ObjectExt.cs => ObjectExtensions.cs} | 8 +- test/Shriek.Test/BootstrapperTest.cs | 49 +++-------- test/Shriek.Test/Shriek.Test.csproj | 2 - 25 files changed, 383 insertions(+), 76 deletions(-) create mode 100644 samples/Shriek.Sample.EventStorage.EFCore/Handlers/DomainNotificationHandler.cs create mode 100644 samples/Shriek.Sample.EventStorage.EFCore/Handlers/TodoCommandHandler.cs create mode 100644 samples/Shriek.Sample.EventStorage.EFCore/Handlers/TodoEventHandler.cs create mode 100644 samples/Shriek.Sample.EventStorage.EFCore/Program.cs create mode 100644 samples/Shriek.Sample.EventStorage.EFCore/Shriek.Sample.EventStorage.EFCore.csproj rename src/{Shriek.EventStorage.EF => Shriek.EventStorage.EFCore}/Class1.cs (100%) create mode 100644 src/Shriek.EventStorage.EFCore/EventStorageEFCoreExtensions.cs rename src/{Shriek.EventStorage.EF => Shriek.EventStorage.EFCore}/EventStorageSQLContext.cs (100%) rename src/{Shriek.EventStorage.EF => Shriek.EventStorage.EFCore}/EventStorageSQLRepository.cs (100%) rename src/{Shriek.EventStorage.EF/Shriek.EventStorage.EF.csproj => Shriek.EventStorage.EFCore/Shriek.EventStorage.EFCore.csproj} (88%) create mode 100644 src/Shriek.EventStorage.EFCore/StoredEvent.cs rename src/{Shriek.EventStorage.EF => Shriek.EventStorage.EFCore}/StoredEventMap.cs (81%) create mode 100644 src/Shriek/Notifications/DomainNotificationHandler.cs create mode 100644 src/Shriek/ShriekOption.cs create mode 100644 src/Shriek/Storage/StoredEvent.cs rename src/Shriek/Utils/{ObjectExt.cs => ObjectExtensions.cs} (89%) diff --git a/.gitignore b/.gitignore index 3c4efe2..b820766 100644 --- a/.gitignore +++ b/.gitignore @@ -258,4 +258,5 @@ paket-files/ # Python Tools for Visual Studio (PTVS) __pycache__/ -*.pyc \ No newline at end of file +*.pyc +*.db diff --git a/ShriekFx.sln b/ShriekFx.sln index a26b5cc..a170e80 100644 --- a/ShriekFx.sln +++ b/ShriekFx.sln @@ -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}" @@ -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 @@ -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 @@ -56,6 +54,14 @@ 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 @@ -63,10 +69,11 @@ Global 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} diff --git a/samples/Shriek.Sample.EventStorage.EFCore/Handlers/DomainNotificationHandler.cs b/samples/Shriek.Sample.EventStorage.EFCore/Handlers/DomainNotificationHandler.cs new file mode 100644 index 0000000..958aa8d --- /dev/null +++ b/samples/Shriek.Sample.EventStorage.EFCore/Handlers/DomainNotificationHandler.cs @@ -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 + { + private List _notifications; + + public DomainNotificationHandler() + { + _notifications = new List(); + } + + public void Handle(DomainNotification message) + { + Console.WriteLine("exception:" + message.Key + ":" + message.Value); + _notifications.Add(message); + } + + public List Notifications + { + get => _notifications; + } + + public bool NotEmpty + { + get => Notifications.Any(); + } + + public void Dispose() + { + _notifications = new List(); + } + } +} \ No newline at end of file diff --git a/samples/Shriek.Sample.EventStorage.EFCore/Handlers/TodoCommandHandler.cs b/samples/Shriek.Sample.EventStorage.EFCore/Handlers/TodoCommandHandler.cs new file mode 100644 index 0000000..81bf760 --- /dev/null +++ b/samples/Shriek.Sample.EventStorage.EFCore/Handlers/TodoCommandHandler.cs @@ -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, + ICommandHandler + { + 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(command.AggregateId); + if (root == null) return; + root.Change(command); + } + } +} \ No newline at end of file diff --git a/samples/Shriek.Sample.EventStorage.EFCore/Handlers/TodoEventHandler.cs b/samples/Shriek.Sample.EventStorage.EFCore/Handlers/TodoEventHandler.cs new file mode 100644 index 0000000..577b1c5 --- /dev/null +++ b/samples/Shriek.Sample.EventStorage.EFCore/Handlers/TodoEventHandler.cs @@ -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, IEventHandler + { + 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!"); + } + } +} \ No newline at end of file diff --git a/samples/Shriek.Sample.EventStorage.EFCore/Program.cs b/samples/Shriek.Sample.EventStorage.EFCore/Program.cs new file mode 100644 index 0000000..2064320 --- /dev/null +++ b/samples/Shriek.Sample.EventStorage.EFCore/Program.cs @@ -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(); + + 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(); + } + } +} \ No newline at end of file diff --git a/samples/Shriek.Sample.EventStorage.EFCore/Shriek.Sample.EventStorage.EFCore.csproj b/samples/Shriek.Sample.EventStorage.EFCore/Shriek.Sample.EventStorage.EFCore.csproj new file mode 100644 index 0000000..bd498aa --- /dev/null +++ b/samples/Shriek.Sample.EventStorage.EFCore/Shriek.Sample.EventStorage.EFCore.csproj @@ -0,0 +1,19 @@ + + + + Exe + netcoreapp2.0 + + + + + + + + + + + + + + diff --git a/src/Shriek.EventStorage.EF/Class1.cs b/src/Shriek.EventStorage.EFCore/Class1.cs similarity index 100% rename from src/Shriek.EventStorage.EF/Class1.cs rename to src/Shriek.EventStorage.EFCore/Class1.cs diff --git a/src/Shriek.EventStorage.EFCore/EventStorageEFCoreExtensions.cs b/src/Shriek.EventStorage.EFCore/EventStorageEFCoreExtensions.cs new file mode 100644 index 0000000..dc2e0ba --- /dev/null +++ b/src/Shriek.EventStorage.EFCore/EventStorageEFCoreExtensions.cs @@ -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 optionsAction = null) + { + services.AddDbContext(optionsAction); + services.AddScoped(); + services.AddScoped(); + } + } +} \ No newline at end of file diff --git a/src/Shriek.EventStorage.EF/EventStorageSQLContext.cs b/src/Shriek.EventStorage.EFCore/EventStorageSQLContext.cs similarity index 100% rename from src/Shriek.EventStorage.EF/EventStorageSQLContext.cs rename to src/Shriek.EventStorage.EFCore/EventStorageSQLContext.cs diff --git a/src/Shriek.EventStorage.EF/EventStorageSQLRepository.cs b/src/Shriek.EventStorage.EFCore/EventStorageSQLRepository.cs similarity index 100% rename from src/Shriek.EventStorage.EF/EventStorageSQLRepository.cs rename to src/Shriek.EventStorage.EFCore/EventStorageSQLRepository.cs diff --git a/src/Shriek.EventStorage.EF/Shriek.EventStorage.EF.csproj b/src/Shriek.EventStorage.EFCore/Shriek.EventStorage.EFCore.csproj similarity index 88% rename from src/Shriek.EventStorage.EF/Shriek.EventStorage.EF.csproj rename to src/Shriek.EventStorage.EFCore/Shriek.EventStorage.EFCore.csproj index 961138a..54e36bd 100644 --- a/src/Shriek.EventStorage.EF/Shriek.EventStorage.EF.csproj +++ b/src/Shriek.EventStorage.EFCore/Shriek.EventStorage.EFCore.csproj @@ -4,6 +4,10 @@ netstandard2.0 + + + + diff --git a/src/Shriek.EventStorage.EFCore/StoredEvent.cs b/src/Shriek.EventStorage.EFCore/StoredEvent.cs new file mode 100644 index 0000000..d106822 --- /dev/null +++ b/src/Shriek.EventStorage.EFCore/StoredEvent.cs @@ -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; } + } +} \ No newline at end of file diff --git a/src/Shriek.EventStorage.EF/StoredEventMap.cs b/src/Shriek.EventStorage.EFCore/StoredEventMap.cs similarity index 81% rename from src/Shriek.EventStorage.EF/StoredEventMap.cs rename to src/Shriek.EventStorage.EFCore/StoredEventMap.cs index 92e6435..3554adb 100644 --- a/src/Shriek.EventStorage.EF/StoredEventMap.cs +++ b/src/Shriek.EventStorage.EFCore/StoredEventMap.cs @@ -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 { diff --git a/src/Shriek/Events/Event.cs b/src/Shriek/Events/Event.cs index 4a1da7c..37d0e38 100644 --- a/src/Shriek/Events/Event.cs +++ b/src/Shriek/Events/Event.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; using System.Text; using Shriek.Messages; @@ -9,7 +10,7 @@ public class Event : Message, IEvent { public Event() { - this.EventType = GetType().Name; + this.EventType = GetType().FullName; this.Timestamp = DateTime.Now; } @@ -19,6 +20,6 @@ public Event() public DateTime Timestamp { get; private set; } - public string EventType { get; private set; } + public string EventType { get; protected set; } } } \ No newline at end of file diff --git a/src/Shriek/Notifications/DomainNotificationHandler.cs b/src/Shriek/Notifications/DomainNotificationHandler.cs new file mode 100644 index 0000000..479d437 --- /dev/null +++ b/src/Shriek/Notifications/DomainNotificationHandler.cs @@ -0,0 +1,36 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace Shriek.Notifications +{ + public class DomainNotificationHandler : IDomainNotificationHandler + { + private List _notifications; + + public DomainNotificationHandler() + { + _notifications = new List(); + } + + public void Handle(DomainNotification message) + { + _notifications.Add(message); + } + + public List Notifications + { + get => _notifications; + } + + public bool NotEmpty + { + get => Notifications.Any(); + } + + public void Dispose() + { + _notifications = new List(); + } + } +} \ No newline at end of file diff --git a/src/Shriek/Shriek.csproj b/src/Shriek/Shriek.csproj index 91c9cfd..e328f4b 100644 --- a/src/Shriek/Shriek.csproj +++ b/src/Shriek/Shriek.csproj @@ -2,9 +2,13 @@ netstandard2.0 + + + + \ No newline at end of file diff --git a/src/Shriek/ShriekOption.cs b/src/Shriek/ShriekOption.cs new file mode 100644 index 0000000..e2ce643 --- /dev/null +++ b/src/Shriek/ShriekOption.cs @@ -0,0 +1,10 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Shriek +{ + public class ShriekOption + { + } +} \ No newline at end of file diff --git a/src/Shriek/Storage/IEventStorage.cs b/src/Shriek/Storage/IEventStorage.cs index 3fa9f2f..33d5630 100644 --- a/src/Shriek/Storage/IEventStorage.cs +++ b/src/Shriek/Storage/IEventStorage.cs @@ -11,7 +11,7 @@ public interface IEventStorage { IEnumerable GetEvents(Guid aggregateId); - void SaveAggregateRoot(TAggregateRoot aggreagate) where TAggregateRoot : IAggregateRoot, IEventProvider; + void SaveAggregateRoot(TAggregateRoot aggregate) where TAggregateRoot : IAggregateRoot, IEventProvider; void Save(T @event) where T : Event; } diff --git a/src/Shriek/Storage/InMemoryEventStorage.cs b/src/Shriek/Storage/InMemoryEventStorage.cs index 3435051..f8cacf4 100644 --- a/src/Shriek/Storage/InMemoryEventStorage.cs +++ b/src/Shriek/Storage/InMemoryEventStorage.cs @@ -47,7 +47,7 @@ public void SaveAggregateRoot(TAggregateRoot aggregate) where TA } } @event.Version = version; - _events.Add(@event); + Save(@event); } } diff --git a/src/Shriek/Storage/SqlEventStorage.cs b/src/Shriek/Storage/SqlEventStorage.cs index 674d7f3..672e67c 100644 --- a/src/Shriek/Storage/SqlEventStorage.cs +++ b/src/Shriek/Storage/SqlEventStorage.cs @@ -1,11 +1,10 @@ -using System; +using System.Reflection; +using System; using System.Collections.Generic; -using System.Text; using Newtonsoft.Json; using Shriek.Domains; using Shriek.Events; using Shriek.EventSourcing; -using Shriek.Storage.Mementos; namespace Shriek.Storage { @@ -20,12 +19,12 @@ public SqlEventStorage(IEventStorageRepository eventStoreRepository) public IEnumerable GetEvents(Guid aggregateId) { - throw new NotImplementedException(); - } - - public T GetMemento(Guid aggregateId) where T : Memento - { - throw new NotImplementedException(); + var storeEvents = _eventStoreRepository.All(aggregateId); + foreach (var e in storeEvents) + { + var eventType = Type.GetType(e.EventType); + yield return JsonConvert.DeserializeObject(e.Data, eventType) as Event; + } } public void Save(T theEvent) where T : Event @@ -41,14 +40,27 @@ public void Save(T theEvent) where T : Event _eventStoreRepository.Store(storedEvent); } - public void SaveAggregateRoot(TAggregateRoot aggreagate) where TAggregateRoot : IAggregateRoot, IEventProvider + public void SaveAggregateRoot(TAggregateRoot aggregate) where TAggregateRoot : IAggregateRoot, IEventProvider { - throw new NotImplementedException(); - } + var uncommittedChanges = aggregate.GetUncommittedChanges(); + var version = aggregate.Version; - public void SaveMemento(Memento memento) - { - throw new NotImplementedException(); + foreach (var @event in uncommittedChanges) + { + version++; + //if (version > 2) + //{ + // if (version % 3 == 0) + // { + // var originator = (IOriginator)aggregate; + // var memento = originator.GetMemento(); + // memento.Version = version; + // SaveMemento(memento); + // } + //} + @event.Version = version; + Save(@event); + } } } } \ No newline at end of file diff --git a/src/Shriek/Storage/StoredEvent.cs b/src/Shriek/Storage/StoredEvent.cs new file mode 100644 index 0000000..d32647f --- /dev/null +++ b/src/Shriek/Storage/StoredEvent.cs @@ -0,0 +1,28 @@ +using System.ComponentModel.DataAnnotations; +using System; +using System.Collections.Generic; +using System.Text; + +namespace Shriek.Events +{ + public class StoredEvent : Event + { + public StoredEvent(Event @event, string data, string user) + { + AggregateId = @event.AggregateId; + EventType = @event.GetType().AssemblyQualifiedName; + Data = data; + User = user; + } + + // EF Constructor + protected StoredEvent() { } + + [Key] + public int Id { get; set; } + + public string Data { get; private set; } + + public string User { get; private set; } + } +} \ No newline at end of file diff --git a/src/Shriek/Utils/ObjectExt.cs b/src/Shriek/Utils/ObjectExtensions.cs similarity index 89% rename from src/Shriek/Utils/ObjectExt.cs rename to src/Shriek/Utils/ObjectExtensions.cs index a0f5de9..c7c4754 100644 --- a/src/Shriek/Utils/ObjectExt.cs +++ b/src/Shriek/Utils/ObjectExtensions.cs @@ -41,16 +41,16 @@ public static Dictionary ToMap(this object o) foreach (var d in dic) { - md.GetType().GetProperty(d.Key).SetValue(md, d.Value); + var prop = md.GetType().GetProperty(d.Key); + if (prop.CanWrite) + prop.SetValue(md, d.Value); } return md; } public static object ToObject(this IDictionary dic, Type type) { - var assembly = Assembly.GetAssembly(type); - - var md = assembly.CreateInstance(type.FullName); + var md = Activator.CreateInstance(type); foreach (var d in dic) { diff --git a/test/Shriek.Test/BootstrapperTest.cs b/test/Shriek.Test/BootstrapperTest.cs index a4976d7..0b650d8 100644 --- a/test/Shriek.Test/BootstrapperTest.cs +++ b/test/Shriek.Test/BootstrapperTest.cs @@ -1,10 +1,9 @@ -using Shriek.Samples.Aggregates; -using System; -using Shriek.Samples.Commands; +using Shriek.Messages; +using Shriek.Storage; +using Shriek.Events; using Shriek.Commands; using Microsoft.Extensions.DependencyInjection; using Microsoft.VisualStudio.TestTools.UnitTesting; -using Shriek.Storage; namespace Shriek.Test { @@ -20,41 +19,15 @@ public void BootstrapperTest() var container = services.BuildServiceProvider(); - var bus = container.GetService(); - - Assert.IsNotNull(bus); - - var id = Guid.NewGuid(); - - var command = new CreateTodoCommand(id) - { - Name = "go to bed", - Desception = "very good", - FinishTime = DateTime.Now.AddHours(1) - }; - bus.Send(command); - - var repository = container.GetService>(); - - var root = repository.GetById(id); - - Assert.IsNotNull(root); - Assert.AreEqual(0, root.Version); - Assert.AreEqual(command.Name, root.Name); - Assert.AreEqual(command.Desception, root.Desception); - - bus.Send(new ChangeTodoCommand(id) - { - Name = "Cho", - Desception = "Beautiful!" - }); - - root = repository.GetById(id); + var commandBus = container.GetService(); + var eventBus = container.GetService(); + var eventStorage = container.GetService(); + var messageProcessor = container.GetService(); - Assert.IsNotNull(root); - Assert.AreEqual(1, root.Version); - Assert.AreEqual("Cho", root.Name); - Assert.AreEqual("Beautiful!", root.Desception); + Assert.IsNotNull(commandBus); + Assert.IsNotNull(eventBus); + Assert.IsNotNull(eventStorage); + Assert.IsNotNull(messageProcessor); } } } \ No newline at end of file diff --git a/test/Shriek.Test/Shriek.Test.csproj b/test/Shriek.Test/Shriek.Test.csproj index 5fb2702..e186a63 100644 --- a/test/Shriek.Test/Shriek.Test.csproj +++ b/test/Shriek.Test/Shriek.Test.csproj @@ -11,9 +11,7 @@ - -