├── .gitignore ├── LICENSE ├── README.md ├── build.bat ├── paket.dependencies ├── scripts └── build-solution.fsx ├── src ├── Fluent-CQRS.Sample.Contracts │ ├── Fluent-CQRS.Sample.Contracts.csproj │ ├── Properties │ │ └── AssemblyInfo.cs │ ├── SampleDomainCommand.cs │ └── SampleEventRaised.cs ├── Fluent-CQRS.Sample.Domain │ ├── App.config │ ├── Fluent-CQRS.Sample.Domain.csproj │ ├── Program.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ ├── SampleAggregate.cs │ ├── SampleCommandHandler.cs │ └── SampleEventHandler.cs ├── Fluent-CQRS.Tests │ ├── AggregateTests.cs │ ├── AggregateUnderTest.cs │ ├── AggregatesRepositoryTests.cs │ ├── Fluent-CQRS.Tests.csproj │ ├── Infrastructure │ │ ├── AlternativeTestAggregate.cs │ │ ├── AlternativeTestEventHandler.cs │ │ ├── BusinessFault.cs │ │ ├── EmptyTestAggregate.cs │ │ ├── InstanceCreated.cs │ │ ├── NeverUsed.cs │ │ ├── SomeThingElseHappened.cs │ │ ├── SomethingHappend.cs │ │ ├── SomethingHappendOnce.cs │ │ ├── SomethingSpecialHappend.cs │ │ ├── TestAggregate.cs │ │ ├── TestCommand.cs │ │ ├── TestEventHandler.cs │ │ ├── ValueChanged.cs │ │ └── ValueInitialized.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ ├── StateDehydrationTests.cs │ ├── With_AggregateUnderTest.cs │ └── With_Aggregates_Repository.cs ├── Fluent-CQRS.sln ├── Fluent-CQRS │ ├── Aggregate.cs │ ├── AggregateId.cs │ ├── AggregateLifeCycle.cs │ ├── Aggregates.cs │ ├── EventBag.cs │ ├── EventHandlers.cs │ ├── ExecutionResult.cs │ ├── Extensions │ │ ├── EventExtension.cs │ │ └── ExecutionResultExtension.cs │ ├── Fault.cs │ ├── Fluent-CQRS.csproj │ ├── Fluentation │ │ ├── IInvokeActionsOnAggregates.cs │ │ └── IProvideAnAggregate.cs │ ├── Fold.cs │ ├── IAggregateMessages.cs │ ├── IAmACommandMessage.cs │ ├── IAmAnEventMessage.cs │ ├── ICollectEvents.cs │ ├── IConcatenateEventHandler.cs │ ├── IHandleEvents.cs │ ├── IReplayEvents.cs │ ├── IStoreAndRetrieveEvents.cs │ ├── InMemoryEventStore.cs │ ├── Mappings.cs │ ├── MissingEventsPublishingTarget.cs │ ├── MultipleHandlerMethodesDetected.cs │ ├── PlaybackEvents.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ └── ResultSet.cs └── package.nuspec └── tools ├── NuGet └── nuget.exe └── Paket ├── license.html └── paket.exe /.gitignore: -------------------------------------------------------------------------------- 1 | # Created by https://www.gitignore.io 2 | 3 | ### Windows ### 4 | # Windows image file caches 5 | Thumbs.db 6 | ehthumbs.db 7 | 8 | # Folder config file 9 | Desktop.ini 10 | 11 | # Recycle Bin used on file shares 12 | $RECYCLE.BIN/ 13 | 14 | # Windows Installer files 15 | *.cab 16 | *.msi 17 | *.msm 18 | *.msp 19 | 20 | # Windows shortcuts 21 | *.lnk 22 | 23 | 24 | ### OSX ### 25 | .DS_Store 26 | .AppleDouble 27 | .LSOverride 28 | 29 | # Icon must end with two \r 30 | Icon 31 | 32 | 33 | # Thumbnails 34 | ._* 35 | 36 | # Files that might appear on external disk 37 | .Spotlight-V100 38 | .Trashes 39 | 40 | # Directories potentially created on remote AFP share 41 | .AppleDB 42 | .AppleDesktop 43 | Network Trash Folder 44 | Temporary Items 45 | .apdisk 46 | 47 | 48 | ### VisualStudio ### 49 | ## Ignore Visual Studio temporary files, build results, and 50 | ## files generated by popular Visual Studio add-ons. 51 | 52 | # User-specific files 53 | *.suo 54 | *.user 55 | *.userosscache 56 | *.sln.docstates 57 | 58 | # User-specific files (MonoDevelop/Xamarin Studio) 59 | *.userprefs 60 | 61 | # Build results 62 | [Dd]ebug/ 63 | [Dd]ebugPublic/ 64 | [Rr]elease/ 65 | [Rr]eleases/ 66 | x64/ 67 | x86/ 68 | build/ 69 | bld/ 70 | [Bb]in/ 71 | [Oo]bj/ 72 | 73 | # Visual Studo 2015 cache/options directory 74 | .vs/ 75 | 76 | # MSTest test Results 77 | [Tt]est[Rr]esult*/ 78 | [Bb]uild[Ll]og.* 79 | 80 | # NUNIT 81 | *.VisualState.xml 82 | TestResult.xml 83 | 84 | # Build Results of an ATL Project 85 | [Dd]ebugPS/ 86 | [Rr]eleasePS/ 87 | dlldata.c 88 | 89 | *_i.c 90 | *_p.c 91 | *_i.h 92 | *.ilk 93 | *.meta 94 | *.obj 95 | *.pch 96 | *.pdb 97 | *.pgc 98 | *.pgd 99 | *.rsp 100 | *.sbr 101 | *.tlb 102 | *.tli 103 | *.tlh 104 | *.tmp 105 | *.tmp_proj 106 | *.log 107 | *.vspscc 108 | *.vssscc 109 | .builds 110 | *.pidb 111 | *.svclog 112 | *.scc 113 | 114 | # Chutzpah Test files 115 | _Chutzpah* 116 | 117 | # Visual C++ cache files 118 | ipch/ 119 | *.aps 120 | *.ncb 121 | *.opensdf 122 | *.sdf 123 | *.cachefile 124 | 125 | # Visual Studio profiler 126 | *.psess 127 | *.vsp 128 | *.vspx 129 | 130 | # TFS 2012 Local Workspace 131 | $tf/ 132 | 133 | # Guidance Automation Toolkit 134 | *.gpState 135 | 136 | # ReSharper is a .NET coding add-in 137 | _ReSharper*/ 138 | *.[Rr]e[Ss]harper 139 | *.DotSettings.user 140 | 141 | # JustCode is a .NET coding addin-in 142 | .JustCode 143 | 144 | # TeamCity is a build add-in 145 | _TeamCity* 146 | 147 | # DotCover is a Code Coverage Tool 148 | *.dotCover 149 | 150 | # NCrunch 151 | _NCrunch_* 152 | .*crunch*.local.xml 153 | 154 | # MightyMoose 155 | *.mm.* 156 | AutoTest.Net/ 157 | 158 | # Web workbench (sass) 159 | .sass-cache/ 160 | 161 | # Installshield output folder 162 | [Ee]xpress/ 163 | 164 | # DocProject is a documentation generator add-in 165 | DocProject/buildhelp/ 166 | DocProject/Help/*.HxT 167 | DocProject/Help/*.HxC 168 | DocProject/Help/*.hhc 169 | DocProject/Help/*.hhk 170 | DocProject/Help/*.hhp 171 | DocProject/Help/Html2 172 | DocProject/Help/html 173 | 174 | # Click-Once directory 175 | publish/ 176 | 177 | # Publish Web Output 178 | *.[Pp]ublish.xml 179 | *.azurePubxml 180 | # TODO: Comment the next line if you want to checkin your web deploy settings 181 | # but database connection strings (with potential passwords) will be unencrypted 182 | *.pubxml 183 | *.publishproj 184 | 185 | # NuGet Packages 186 | *.nupkg 187 | # The packages folder can be ignored because of Package Restore 188 | **/packages/* 189 | # except build/, which is used as an MSBuild target. 190 | !**/packages/build/ 191 | # Uncomment if necessary however generally it will be regenerated when needed 192 | #!**/packages/repositories.config 193 | 194 | # Windows Azure Build Output 195 | csx/ 196 | *.build.csdef 197 | 198 | # Windows Store app package directory 199 | AppPackages/ 200 | 201 | # Others 202 | *.[Cc]ache 203 | ClientBin/ 204 | [Ss]tyle[Cc]op.* 205 | ~$* 206 | *~ 207 | *.dbmdl 208 | *.dbproj.schemaview 209 | *.pfx 210 | *.publishsettings 211 | node_modules/ 212 | bower_components/ 213 | 214 | # RIA/Silverlight projects 215 | Generated_Code/ 216 | 217 | # Backup & report files from converting an old project file 218 | # to a newer Visual Studio version. Backup files are not needed, 219 | # because we have git ;-) 220 | _UpgradeReport_Files/ 221 | Backup*/ 222 | UpgradeLog*.XML 223 | UpgradeLog*.htm 224 | 225 | # SQL Server files 226 | *.mdf 227 | *.ldf 228 | 229 | # Business Intelligence projects 230 | *.rdl.data 231 | *.bim.layout 232 | *.bim_*.settings 233 | 234 | # Microsoft Fakes 235 | FakesAssemblies/ 236 | 237 | # Node.js Tools for Visual Studio 238 | .ntvs_analysis.dat 239 | 240 | # Visual Studio 6 build log 241 | *.plg 242 | 243 | # Visual Studio 6 workspace options file 244 | *.opt 245 | 246 | 247 | ### MonoDevelop ### 248 | #User Specific 249 | *.userprefs 250 | *.usertasks 251 | 252 | #Mono Project Files 253 | *.pidb 254 | *.resources 255 | test-results/ 256 | 257 | 258 | ### XamarinStudio ### 259 | bin/ 260 | obj/ 261 | *.userprefs 262 | 263 | ##Paket 264 | paket.lock 265 | 266 | ##FAKE 267 | .fake/ 268 | 269 | ##Expected Files 270 | !tools/NuGet/nuget.exe 271 | !tools/Paket/paket.exe 272 | 273 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Janek 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # fluent-cqrs 2 | "The Next Generation" CQRS Framework for .Net applications 3 | 4 | ![TeamCity Build Status](https://img.shields.io/teamcity/http/ci.devcrowd.de/s/OpenSource_FluentCqrs_NugetBuildAndPublish.svg) 5 | 6 | [![Nuget Version](https://img.shields.io/nuget/v/fluent-cqrs.svg)](https://www.nuget.org/packages/Fluent-CQRS/) 7 | 8 | ### Attenzione Attenzione: 9 | There is an **Api break**. The method `OnError` was splitted into `CatchException` 10 | (raised by unexpected System Exceptions) and `CatchFault` (raised by Business Faults). 11 | By default 'Do' throws all exceptions directly. Use 'Try' for catching errors. 12 | 13 | ####2.0.3.3 14 | Now you can't use `Changes`, `History` or `MessagesOfType` outside of an aggregate. 15 | Someone has done it in the past and it feels like a pinch. 16 | 17 | ####2.0.3.4 18 | The `With(command)` method gets a sister, `With(new AggregateId("cool Id"))`. You can use it for providing Aggregates in 19 | CommandHandlers by commands without Id for the Aggregate. May be you can retrieve the Id by an other way like Read Model. 20 | 21 | --- 22 | 23 | Why fluent? Just look at this: 24 | 25 | ```csharp 26 | 27 | public class AwsomeCommandHandler 28 | { 29 | Aggregates _aggregates; 30 | 31 | public AwsomeCommandHandler(Aggregates aggregates) 32 | { 33 | _aggregates = aggregates; 34 | } 35 | 36 | public void Handle(SuperDuperCommand command) 37 | { 38 | _aggregates 39 | .Provide<[AnAggregateYouLike]> 40 | .With(command) 41 | // or you can use .With(new AggregateId("cool id")) 42 | .Do(yourAggregate => yourAggregate.DoSomethingWith(command.Data)); 43 | 44 | // You want it with exception handling? 45 | // Lets do it 46 | 47 | _aggregates 48 | .Provide<[AnAggregateYouLike]> 49 | .With(command) 50 | // or you can use .With(new AggregateId("cool id") 51 | .Try(yourAggregate => yourAggregate.DoSomethingWith(command.Data)) 52 | .CatchException(exception=> handleThis(exception)); 53 | 54 | // And here a very simple way to catch business faults which might be thrown within the Aggregate 55 | 56 | _aggregates 57 | .Provide<[AnAggregateYouLike]> 58 | .With(command) 59 | // or you can use .With(new AggregateId("cool id") 60 | .Try(yourAggregate => yourAggregate.DoSomethingWith(command.Data)) 61 | .CatchFault(fault=> handleThis(fault)) 62 | .CatchException(exception => handleThis(exception)); 63 | } 64 | } 65 | ``` 66 | Uhhh... this is the **complete handling** of a Domain Command. 67 | 68 | --- 69 | 70 | Ok, but what do I have to do to **publish** the new state, aka **Domain Events**? 71 | 72 | This is simple. You assign any Event Handler you like by chaining it by the `And` method after `PublishNewStateTo`. 73 | For example you have three Event Handlers: 74 | 75 | ``` 76 | 77 | var _aggregates = Aggregates.CreateWith(yourExtremeGoodEventStoreInstance); 78 | var firstEventHandler = new SampleEventHandler(); 79 | var secondEventHandler = new ReportingHandler(); 80 | var thirdEventHandler = new LoggingHandler(); 81 | 82 | _aggregates 83 | .PublishNewStateTo(firstEventHandler) 84 | .And(secondEventHandler) 85 | .And(thirdEventHandler); 86 | ``` 87 | now all your cool Event Handlers receiving all changes of an aggregate. 88 | 89 | --- 90 | 91 | All right... Now let's have a look inside the **Aggregate**. 92 | 93 | ```csharp 94 | 95 | class CoolAggregate() : Aggregate 96 | { 97 | public CoolAggregate(string id, IEnumerable history) 98 | : base(id, history) { } 99 | ... 100 | public void DoSomethingHelpful() 101 | { 102 | Changes.Add(new SomethingHappend()); 103 | } 104 | ... 105 | } 106 | ``` 107 | 108 | The very cool thing of an aggregate is that you can make decisions based on the event history. There are two different ways doing that. 109 | 110 | The simplest way is using `MessagesOfType()`. 111 | 112 | ```csharp 113 | 114 | // Do not wonder, it is the nes C# 6 Style 115 | class CoolAggregate() : Aggregate 116 | { 117 | ... 118 | bool ItHappened => MessagesOfType().Any(); 119 | int HappeningCounter => MessagesOfType().Count(); 120 | AType CoolStuff => MessagesOfType().Last().CoolStuff; 121 | ... 122 | } 123 | ``` 124 | This might be enough for a lot of use cases, though sometimes it is not that easy and multiple events are playing together and their order gets important. Now it's time for the Swiss knife of **Event Sourcing**, as the current state is just a left fold/ aggregate of all preceding events. 125 | 126 | In that case you set up the rules that should happen for any event you like. Use it to restore a complex entity or a simple value, it is up to you. 127 | 128 | ```csharp 129 | 130 | // Do not wonder, it is the nes C# 6 Style 131 | class CoolAggregate() : Aggregate 132 | { 133 | 134 | OtherType BetterStuff => 135 | // or InitializeWith(new OtherType()) 136 | InitialzedAs() 137 | // former state can be ignored 138 | .ApplyForAny(message => message.BetterStuff) 139 | 140 | // the former state can be used for calculations 141 | .ApplyForAny(state, message => f(state, message.OtherStuff)) 142 | 143 | // well, or set to a constant 144 | .ApplyForAny(SomeConstantState) 145 | 146 | // if none of the Events can be found, 147 | // Otherwise will be executed 148 | .Otherwise(() => OtherConstantState ) 149 | // or => throw new BusinesFault() 150 | // Please note that Otherwise is optional 151 | 152 | // the actual fold over all domain events 153 | .AggregateAllMessages(); 154 | } 155 | 156 | ``` 157 | 158 | In some cases you want to save an event only once, but if you add the event into the list of Changes like above, 159 | the event will save every time. 160 | 161 | Bad, very bad. Here comes the hero `Replay` ... 162 | 163 | ```csharp 164 | 165 | class CoolAggregate() : Aggregate 166 | { 167 | ... 168 | public void DoSomethingHelpful() 169 | { 170 | if(MessagesOfType().Any) 171 | { 172 | Replay(new SomethingHappend()); 173 | } 174 | else 175 | { 176 | Changes.Add(new SomethingHappend()); 177 | } 178 | } 179 | ... 180 | } 181 | ``` 182 | 183 | The `Replay` method prevents you for multiple equals events. It fires to the event handlers, though it will not be recorded as event. 184 | 185 | --- 186 | 187 | Nice, really nice... But what if you want to **replay all Events** of an Aggregate? Yes... you couldn't... till now. 188 | 189 | To replay all Events of an Aggregate code this: 190 | 191 | ```csharp 192 | 193 | _aggregates 194 | .ReplayFor<[AnAggregateYouLike]>() 195 | .EventsWithAggregateId(aggrId) 196 | .ToAllEventHandlers(); 197 | ``` 198 | 199 | This published all Events of the Aggregate with the given ID to all registered Event Handler. 200 | If you want to publish to only one special Event Handler change your Code to: 201 | 202 | ```csharp 203 | 204 | _aggregates 205 | .ReplayFor<[AnAggregateYouLike]>() 206 | .EventsWithAggregateId(aggrId) 207 | .To([OneOfYourEventHandler]); 208 | ``` 209 | 210 | This is simple, as well. 211 | 212 | You can also replay events of an aggregate type: 213 | 214 | ```csharp 215 | 216 | _aggregates 217 | .ReplayFor<[AnAggregateYouLike]>() 218 | .AllEvents() 219 | .ToAllEventHandlers(); 220 | ``` 221 | 222 | You can also filter for certain event messages: 223 | 224 | ```csharp 225 | 226 | _aggregates 227 | .ReplayFor<[AnAggregateYouLike]>() 228 | .AllEvents() 229 | .OfType<[AnEvent]>() 230 | .ToAllEventHandlers(); 231 | ``` 232 | 233 | --- 234 | 235 | ~tbc 236 | -------------------------------------------------------------------------------- /build.bat: -------------------------------------------------------------------------------- 1 | cls 2 | 3 | set toolPath=packages\FAKE\tools\Fake.exe 4 | 5 | %toolPath% scripts/build-solution.fsx -------------------------------------------------------------------------------- /paket.dependencies: -------------------------------------------------------------------------------- 1 | source https://nuget.org/api/v2 2 | 3 | nuget FAKE 4 | nuget Paket 5 | nuget FluentAssertions = 3.5.0 6 | nuget Nunit = 2.6.4 7 | nuget NUnit.Runners = 2.6.4 -------------------------------------------------------------------------------- /scripts/build-solution.fsx: -------------------------------------------------------------------------------- 1 | // Referenz zu FAKE herstellen 2 | #r @"../packages/FAKE/tools/FakeLib.dll" 3 | 4 | open Fake 5 | open Fake.AssemblyInfoFile 6 | 7 | let name = "Fluent-CQRS" 8 | let title = "Fluent CQRS Framework" 9 | let description = "The next generation of CQRS Framework." 10 | let id = "41246c8a-9b02-4be4-83ec-c88a26ee6054" 11 | let authors = ["Jan Fellien"; "Carsten Koenig"] 12 | let tags = "CQRS, Event Sourcing, Event Sore, DDD, Domain Driven Design" 13 | 14 | let buildOutput = "./build/" 15 | let testBuildOutput = "./build/for_tests/" 16 | let publishDir = "./build/publish/" 17 | let sourceProjectDir = "src/Fluent-CQRS/" 18 | let testsProjectDir = "src/Fluent-CQRS.Tests/" 19 | let builtAssembly = name + ".dll" 20 | let toolsPath = "tools/" 21 | let nugetPath = toolsPath + "NuGet/NuGet.exe" 22 | let nunitPath = @".\packages\NUnit.Runners\tools\" 23 | let nunitExecutable = "nunit-console.exe" 24 | 25 | let version = 26 | match buildServer with 27 | | TeamCity -> buildVersion 28 | | _ -> "0.0.1" 29 | 30 | RestorePackages 31 | 32 | // Targets 33 | Target "Clean" (fun _ -> 34 | CleanDir buildOutput 35 | CleanDir testBuildOutput 36 | ) 37 | 38 | Target "Build" (fun _ -> 39 | 40 | CreateCSharpAssemblyInfo ("./src/" + name + "/Properties/AssemblyInfo.cs") 41 | [Attribute.Title title 42 | Attribute.Description description 43 | Attribute.Company "" 44 | Attribute.Copyright "Copyright © Jan Fellien" 45 | Attribute.Guid id 46 | Attribute.Product name 47 | Attribute.Version version 48 | Attribute.FileVersion version] 49 | 50 | !! (sourceProjectDir + "*.csproj") 51 | |> MSBuildRelease buildOutput "Build" 52 | |> Log "AppBuild-Output: " 53 | 54 | !! (testsProjectDir + "*.csproj") 55 | |> MSBuildRelease testBuildOutput "Build" 56 | |> Log "TestBuild-Output: " 57 | ) 58 | 59 | Target "Clean BuildResults" (fun _ -> 60 | !!(buildOutput + "*.pdb") 61 | |> DeleteFiles 62 | 63 | !!(testBuildOutput + "*.pdb") 64 | |> DeleteFiles 65 | ) 66 | 67 | Target "Run Tests" (fun _ -> 68 | !! (testBuildOutput + "/*.Tests.dll") 69 | |> NUnit (fun p -> 70 | {p with 71 | ToolPath = nunitPath 72 | ToolName = nunitExecutable 73 | DisableShadowCopy = true; 74 | OutputFile = testBuildOutput + "/TestResults.xml"}) 75 | 76 | 77 | ) 78 | 79 | Target "Package" (fun _ -> 80 | CopyFiles publishDir !! (buildOutput @@ builtAssembly) 81 | 82 | NuGet (fun p -> 83 | {p with 84 | ToolPath = nugetPath 85 | Project = name 86 | Authors = authors 87 | Description = description 88 | Summary = description 89 | Tags = tags 90 | Version = version 91 | OutputPath = publishDir 92 | WorkingDir = publishDir 93 | Files = [builtAssembly, Some "lib/portable-net45+netcore45+wpa81", None] }) 94 | "src/package.nuspec" 95 | ) 96 | 97 | // Dependencies 98 | "Clean" 99 | ==> "Build" 100 | ==> "Clean BuildResults" 101 | ==> "Run Tests" 102 | ==> "Package" 103 | 104 | RunTargetOrDefault "Package" -------------------------------------------------------------------------------- /src/Fluent-CQRS.Sample.Contracts/Fluent-CQRS.Sample.Contracts.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {00396ADD-73D9-46CF-B7A0-A74CAE5F92D2} 8 | Library 9 | Properties 10 | Fluent_CQRS.Sample.Contracts 11 | Fluent-CQRS.Sample.Contracts 12 | v4.5 13 | 512 14 | 15 | 16 | true 17 | full 18 | false 19 | bin\Debug\ 20 | DEBUG;TRACE 21 | prompt 22 | 4 23 | 24 | 25 | pdbonly 26 | true 27 | bin\Release\ 28 | TRACE 29 | prompt 30 | 4 31 | 32 | 33 | true 34 | bin\Debug-Net40\ 35 | DEBUG;TRACE 36 | full 37 | AnyCPU 38 | prompt 39 | MinimumRecommendedRules.ruleset 40 | 41 | 42 | bin\Release-Net40\ 43 | TRACE 44 | true 45 | pdbonly 46 | AnyCPU 47 | prompt 48 | MinimumRecommendedRules.ruleset 49 | 50 | 51 | true 52 | bin\Debug-Net35\ 53 | DEBUG;TRACE 54 | full 55 | AnyCPU 56 | prompt 57 | MinimumRecommendedRules.ruleset 58 | 59 | 60 | bin\Release-Net35\ 61 | TRACE 62 | true 63 | pdbonly 64 | AnyCPU 65 | prompt 66 | MinimumRecommendedRules.ruleset 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | {413E358A-9009-418A-B9ED-0969F3C325F0} 85 | Fluent-CQRS 86 | 87 | 88 | 89 | 96 | -------------------------------------------------------------------------------- /src/Fluent-CQRS.Sample.Contracts/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // Allgemeine Informationen über eine Assembly werden über die folgenden 6 | // Attribute gesteuert. Ändern Sie diese Attributwerte, um die Informationen zu ändern, 7 | // die mit einer Assembly verknüpft sind. 8 | [assembly: AssemblyTitle("Fluent-CQRS.Sample.Contracts")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("devCrowd GmbH")] 12 | [assembly: AssemblyProduct("Fluent-CQRS.Sample.Contracts")] 13 | [assembly: AssemblyCopyright("Copyright © devCrowd GmbH")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Durch Festlegen von ComVisible auf "false" werden die Typen in dieser Assembly unsichtbar 18 | // für COM-Komponenten. Wenn Sie auf einen Typ in dieser Assembly von 19 | // COM zugreifen müssen, legen Sie das ComVisible-Attribut für diesen Typ auf "true" fest. 20 | [assembly: ComVisible(false)] 21 | 22 | // Die folgende GUID bestimmt die ID der Typbibliothek, wenn dieses Projekt für COM verfügbar gemacht wird 23 | [assembly: Guid("02c1ebb3-9660-4ac5-99b8-4ad9f7bcdcc8")] 24 | 25 | // Versionsinformationen für eine Assembly bestehen aus den folgenden vier Werten: 26 | // 27 | // Hauptversion 28 | // Nebenversion 29 | // Buildnummer 30 | // Revision 31 | // 32 | // Sie können alle Werte angeben oder die standardmäßigen Build- und Revisionsnummern 33 | // übernehmen, indem Sie "*" eingeben: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /src/Fluent-CQRS.Sample.Contracts/SampleDomainCommand.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Fluent_CQRS.Sample.Contracts 4 | { 5 | public class SampleDomainCommand : IAmACommandMessage 6 | { 7 | public string Id 8 | { 9 | get; 10 | set; 11 | } 12 | 13 | public string MyValue { get; set; } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/Fluent-CQRS.Sample.Contracts/SampleEventRaised.cs: -------------------------------------------------------------------------------- 1 | namespace Fluent_CQRS.Sample.Contracts 2 | { 3 | public class SampleEventRaised : IAmAnEventMessage 4 | { 5 | public string MyValue { get; set; } 6 | } 7 | } -------------------------------------------------------------------------------- /src/Fluent-CQRS.Sample.Domain/App.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /src/Fluent-CQRS.Sample.Domain/Fluent-CQRS.Sample.Domain.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {D7F28833-BC97-4F70-866B-195AB16C0296} 8 | Exe 9 | Properties 10 | Fluent_CQRS.Sample.Domain 11 | Fluent-CQRS.Sample.Domain 12 | v4.5 13 | 512 14 | 15 | 16 | AnyCPU 17 | true 18 | full 19 | false 20 | bin\Debug\ 21 | DEBUG;TRACE 22 | prompt 23 | 4 24 | 25 | 26 | AnyCPU 27 | pdbonly 28 | true 29 | bin\Release\ 30 | TRACE 31 | prompt 32 | 4 33 | 34 | 35 | true 36 | bin\Debug-Net40\ 37 | DEBUG;TRACE 38 | full 39 | AnyCPU 40 | prompt 41 | MinimumRecommendedRules.ruleset 42 | true 43 | 44 | 45 | bin\Release-Net40\ 46 | TRACE 47 | true 48 | pdbonly 49 | AnyCPU 50 | prompt 51 | MinimumRecommendedRules.ruleset 52 | true 53 | 54 | 55 | true 56 | bin\Debug-Net35\ 57 | DEBUG;TRACE 58 | full 59 | AnyCPU 60 | prompt 61 | MinimumRecommendedRules.ruleset 62 | true 63 | 64 | 65 | bin\Release-Net35\ 66 | TRACE 67 | true 68 | pdbonly 69 | AnyCPU 70 | prompt 71 | MinimumRecommendedRules.ruleset 72 | true 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | {00396add-73d9-46cf-b7a0-a74cae5f92d2} 96 | Fluent-CQRS.Sample.Contracts 97 | 98 | 99 | {413E358A-9009-418A-B9ED-0969F3C325F0} 100 | Fluent-CQRS 101 | 102 | 103 | 104 | 111 | -------------------------------------------------------------------------------- /src/Fluent-CQRS.Sample.Domain/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Fluent_CQRS.Sample.Contracts; 3 | 4 | namespace Fluent_CQRS.Sample.Domain 5 | { 6 | class Program 7 | { 8 | static void Main() 9 | { 10 | 11 | var aggregates = Aggregates.CreateWith(new InMemoryEventStore()); 12 | 13 | var firstEventHandler = new SampleEventHandler(); 14 | var secondEventHandler = new SampleEventHandler(); 15 | var thirdEventHandler = new SampleEventHandler(); 16 | var sampleCommandHandler = new SampleCommandHandler(aggregates); 17 | 18 | aggregates 19 | .PublishNewStateTo(firstEventHandler) 20 | .And(secondEventHandler) 21 | .And(thirdEventHandler); 22 | 23 | var aggregateId = Guid.NewGuid().ToString(); 24 | 25 | sampleCommandHandler.Handle( 26 | new SampleDomainCommand 27 | { 28 | Id = aggregateId, 29 | MyValue = "Hi There" 30 | }); 31 | 32 | sampleCommandHandler.Handle( 33 | new SampleDomainCommand 34 | { 35 | Id = aggregateId, 36 | MyValue = "Hello Kitty" 37 | }); 38 | 39 | sampleCommandHandler.Handle( 40 | new SampleDomainCommand 41 | { 42 | Id = aggregateId, 43 | MyValue = "Hey Dude" 44 | }); 45 | 46 | 47 | Console.ReadLine(); 48 | 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/Fluent-CQRS.Sample.Domain/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // Allgemeine Informationen über eine Assembly werden über die folgenden 6 | // Attribute gesteuert. Ändern Sie diese Attributwerte, um die Informationen zu ändern, 7 | // die mit einer Assembly verknüpft sind. 8 | [assembly: AssemblyTitle("Fluent-CQRS.Sample.Domain")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("devCrowd GmbH")] 12 | [assembly: AssemblyProduct("Fluent-CQRS.Sample.Domain")] 13 | [assembly: AssemblyCopyright("Copyright © devCrowd GmbH")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Durch Festlegen von ComVisible auf "false" werden die Typen in dieser Assembly unsichtbar 18 | // für COM-Komponenten. Wenn Sie auf einen Typ in dieser Assembly von 19 | // COM zugreifen müssen, legen Sie das ComVisible-Attribut für diesen Typ auf "true" fest. 20 | [assembly: ComVisible(false)] 21 | 22 | // Die folgende GUID bestimmt die ID der Typbibliothek, wenn dieses Projekt für COM verfügbar gemacht wird 23 | [assembly: Guid("dfabea72-9a19-4a0c-b33e-31dd655b6773")] 24 | 25 | // Versionsinformationen für eine Assembly bestehen aus den folgenden vier Werten: 26 | // 27 | // Hauptversion 28 | // Nebenversion 29 | // Buildnummer 30 | // Revision 31 | // 32 | // Sie können alle Werte angeben oder die standardmäßigen Build- und Revisionsnummern 33 | // übernehmen, indem Sie "*" eingeben: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /src/Fluent-CQRS.Sample.Domain/SampleAggregate.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using Fluent_CQRS.Sample.Contracts; 3 | 4 | namespace Fluent_CQRS.Sample.Domain 5 | { 6 | internal class SampleAggregate : Aggregate 7 | { 8 | public SampleAggregate(string id, IEnumerable history) 9 | : base(id, history) { } 10 | 11 | public void SampleAggregateMethod(string myValue) 12 | { 13 | Changes.Add(new SampleEventRaised{MyValue = myValue}); 14 | } 15 | } 16 | } -------------------------------------------------------------------------------- /src/Fluent-CQRS.Sample.Domain/SampleCommandHandler.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Fluent_CQRS.Sample.Contracts; 3 | using Fluent_CQRS.Extensions; 4 | 5 | namespace Fluent_CQRS.Sample.Domain 6 | { 7 | public class SampleCommandHandler 8 | { 9 | readonly Aggregates _aggregates; 10 | 11 | public SampleCommandHandler(Aggregates aggregates) 12 | { 13 | _aggregates = aggregates; 14 | } 15 | 16 | public void Handle(SampleDomainCommand command) 17 | { 18 | Console.WriteLine("Command {0} recieved: {1}", command.Id, command.MyValue); 19 | 20 | _aggregates.Provide().With(command) 21 | .Try(aggregate => aggregate.SampleAggregateMethod(command.MyValue)) 22 | .CatchException(Console.WriteLine); 23 | } 24 | } 25 | } -------------------------------------------------------------------------------- /src/Fluent-CQRS.Sample.Domain/SampleEventHandler.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using Fluent_CQRS.Sample.Contracts; 5 | using Fluent_CQRS.Extensions; 6 | 7 | namespace Fluent_CQRS.Sample.Domain 8 | { 9 | public class SampleEventHandler : IHandleEvents 10 | { 11 | public void Receive (IEnumerable eventMessages) 12 | { 13 | eventMessages.ToList ().ForEach (message => message.HandleMeWith (this)); 14 | } 15 | 16 | public void HandleMessage (SampleEventRaised message) 17 | { 18 | Console.WriteLine ("Event {0} empfangen: {1}", message.GetType (), message.MyValue); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/Fluent-CQRS.Tests/AggregateTests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using FluentAssertions; 7 | using Fluent_CQRS.Extensions; 8 | using Fluent_CQRS.Tests.Infrastructure; 9 | using NUnit.Framework; 10 | 11 | namespace Fluent_CQRS.Tests 12 | { 13 | [TestFixture] 14 | public class AggregateTests : With_Aggregates_Repository 15 | { 16 | [Test] 17 | public void When_execute_DoSomething_it_should_saved_one_SomethingHappend_event_in_the_eventstore() 18 | { 19 | var testCommand = new TestCommand 20 | { 21 | Id = "TestAggr" 22 | }; 23 | 24 | _aggregates.Provide().With(testCommand) 25 | .Do(aggregate => aggregate.DoSomething()); 26 | 27 | var aggregateEvents = _eventStore.RetrieveFor(testCommand.Id); 28 | 29 | aggregateEvents.OfType().Count().Should().Be(1); 30 | } 31 | 32 | [Test] 33 | public void When_execute_DoSomethingOnce_twice_it_should_saved_one_SomethingHappend_event_in_the_eventstore() 34 | { 35 | var testCommand = new TestCommand 36 | { 37 | Id = "TestAggr" 38 | }; 39 | 40 | _aggregates.Provide().With(testCommand) 41 | .Do(aggregate => aggregate.DoSomethingOnce()); 42 | 43 | _aggregates.Provide().With(testCommand) 44 | .Do(aggregate => aggregate.DoSomethingOnce()); 45 | 46 | var aggrigateEvents = _eventStore.RetrieveFor(testCommand.Id); 47 | 48 | aggrigateEvents.OfType().Count().Should().Be(1); 49 | } 50 | 51 | [Test] 52 | public void When_execute_DoSomethingOnce_twice_it_should_saved_one_SomethingHappend_event_in_the_eventstore_an_publish_twice() 53 | { 54 | var testCommand = new TestCommand 55 | { 56 | Id = "TestAggr" 57 | }; 58 | 59 | _aggregates.PublishNewStateTo(_eventHandler); 60 | 61 | _aggregates.Provide().With(testCommand) 62 | .Do(aggregate => aggregate.DoSomethingOnce()); 63 | 64 | _aggregates.Provide().With(testCommand) 65 | .Do(aggregate => aggregate.DoSomethingOnce()); 66 | 67 | var aggregateEvents = _eventStore.RetrieveFor(testCommand.Id); 68 | 69 | aggregateEvents.OfType().Count().Should().Be(1); 70 | 71 | _eventHandler.RecievedEvents.OfType().Count().Should().Be(2); 72 | } 73 | 74 | [Test] 75 | [ExpectedException( 76 | ExpectedException = typeof(ApplicationException), 77 | ExpectedMessage = "This is a intentionally Exception")] 78 | public void When_throw_an_Exception_within_the_aggregate_it_should_catched_in_CatchException_method() 79 | { 80 | var testCommand = new TestCommand 81 | { 82 | Id = "TestAggr" 83 | }; 84 | 85 | _aggregates.Provide().With(testCommand) 86 | .Try(aggregate => aggregate.ThrowException()) 87 | .CatchException(exception => 88 | { 89 | throw exception; 90 | }); 91 | } 92 | 93 | [Test] 94 | [ExpectedException( 95 | ExpectedException = typeof(BusinessFault), 96 | ExpectedMessage = "My BusinessFault")] 97 | public void When_throw_a_Fault_within_the_aggregate_it_should_catched_in_CatchFault_method() 98 | { 99 | var testCommand = new TestCommand 100 | { 101 | Id = "TestAggr" 102 | }; 103 | 104 | _aggregates.Provide().With(testCommand) 105 | .Try(aggregate => aggregate.ThrowFault()) 106 | .CatchFault(fault => 107 | { 108 | throw fault; 109 | }); 110 | } 111 | 112 | [Test] 113 | [ExpectedException( 114 | ExpectedException = typeof(ApplicationException), 115 | ExpectedMessage = "This is a intentionally Exception")] 116 | public void When_throw_an_Exception_within_the_aggregate_it_should_not_catched_by_default() 117 | { 118 | var testCommand = new TestCommand 119 | { 120 | Id = "TestAggr" 121 | }; 122 | 123 | _aggregates.Provide().With(testCommand) 124 | .Do(aggregate => aggregate.ThrowException()); 125 | } 126 | 127 | [Test] 128 | [ExpectedException( 129 | ExpectedException = typeof(BusinessFault), 130 | ExpectedMessage = "My BusinessFault")] 131 | public void When_throw_a_Fault_within_the_aggregate_it_should_not_catched_by_default() 132 | { 133 | var testCommand = new TestCommand 134 | { 135 | Id = "TestAggr" 136 | }; 137 | 138 | _aggregates.Provide().With(testCommand) 139 | .Do(aggregate => aggregate.ThrowFault()); 140 | } 141 | 142 | [Test] 143 | public void When_throw_an_Exception_within_the_aggregate_it_should_returned_by_execution_result() 144 | { 145 | var testCommand = new TestCommand 146 | { 147 | Id = "TestAggr" 148 | }; 149 | 150 | var executionResult = _aggregates.Provide().With(testCommand) 151 | .Try(aggregate => aggregate.ThrowException()); 152 | 153 | executionResult.Exception.Should().BeOfType(); 154 | executionResult.Exception.Message.Should().Be("This is a intentionally Exception"); 155 | 156 | } 157 | 158 | [Test] 159 | public void When_throw_a_Fault_within_the_aggregate_it_should_returnes_by_execution_result() 160 | { 161 | var testCommand = new TestCommand 162 | { 163 | Id = "TestAggr" 164 | }; 165 | 166 | var executionResult = _aggregates.Provide().With(testCommand) 167 | .Try(aggregate => aggregate.ThrowFault()); 168 | 169 | executionResult.Fault.Should().BeOfType(); 170 | executionResult.Fault.Message.Should().Be("My BusinessFault"); 171 | 172 | } 173 | 174 | [Test] 175 | public void When_throw_an_Exception_within_the_aggregate_it_should_not_saved_any_changes() 176 | { 177 | var testCommand = new TestCommand 178 | { 179 | Id = "TestAggr" 180 | }; 181 | 182 | _aggregates.Provide().With(testCommand) 183 | .Try(aggregate => aggregate.ThrowException()); 184 | 185 | _eventStore 186 | .RetrieveFor(testCommand.Id) 187 | .OfType() 188 | .Count().Should().Be(0); 189 | 190 | } 191 | 192 | [Test] 193 | public void When_throw_an_Fault_within_the_aggregate_it_should_not_saved_any_changes() 194 | { 195 | var testCommand = new TestCommand 196 | { 197 | Id = "TestAggr" 198 | }; 199 | 200 | _aggregates.Provide().With(testCommand) 201 | .Try(aggregate => aggregate.ThrowException()); 202 | 203 | _eventStore 204 | .RetrieveFor(testCommand.Id) 205 | .OfType() 206 | .Count().Should().Be(0); 207 | 208 | } 209 | 210 | [Test] 211 | public void When_throw_an_Exception_within_the_aggregate_it_should_not_published_any_event() 212 | { 213 | var testCommand = new TestCommand 214 | { 215 | Id = "TestAggr" 216 | }; 217 | 218 | _aggregates.PublishNewStateTo(_eventHandler); 219 | 220 | _aggregates.Provide().With(testCommand) 221 | .Try(aggregate => aggregate.ThrowException()); 222 | 223 | _eventHandler.RecievedEvents.Count.Should().Be(0); 224 | 225 | } 226 | 227 | [Test] 228 | public void When_throw_an_Fault_within_the_aggregate_it_should_not_published_any_event() 229 | { 230 | var testCommand = new TestCommand 231 | { 232 | Id = "TestAggr" 233 | }; 234 | 235 | _aggregates.PublishNewStateTo(_eventHandler); 236 | 237 | _aggregates.Provide().With(testCommand) 238 | .Try(aggregate => aggregate.ThrowException()); 239 | 240 | _eventHandler.RecievedEvents.Count.Should().Be(0); 241 | } 242 | 243 | [Test] 244 | public void When_instantiate_an_Aggregate_it_should_do_only_once() 245 | { 246 | var testCommand = new TestCommand 247 | { 248 | Id = "TestAggr" 249 | }; 250 | 251 | _aggregates.Provide().With(testCommand) 252 | .Try(aggregate => aggregate.DoNothing()); 253 | 254 | _aggregates.Provide().With(testCommand) 255 | .Try(aggregate => aggregate.DoNothing()); 256 | 257 | _eventStore.RetrieveFor(testCommand.Id).OfType().Count().Should().Be(1); 258 | } 259 | } 260 | } 261 | -------------------------------------------------------------------------------- /src/Fluent-CQRS.Tests/AggregateUnderTest.cs: -------------------------------------------------------------------------------- 1 | using Fluent_CQRS.Tests.Infrastructure; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | 6 | namespace Fluent_CQRS.Tests 7 | { 8 | class AggregateUnderTest : Aggregate 9 | { 10 | public AggregateUnderTest(String id, IEnumerable history) : base(id, history) 11 | { 12 | } 13 | //you SHOULD NEVER set properties to public in production for aggregates 14 | public int NumberOfValueChanges 15 | { 16 | get{ return MessagesOfType().Count();} 17 | } 18 | 19 | public int SumOfValueChanges 20 | { 21 | get { return MessagesOfType().Sum(message => message.Value); } 22 | } 23 | 24 | public int SumOfAnyValues 25 | { 26 | get 27 | { 28 | return MessagesOfType().Sum(message => message.Value) 29 | + MessagesOfType().Sum(message => message.Value); 30 | } 31 | } 32 | 33 | public int SumOfAnyValuesAggregated 34 | { 35 | get 36 | { 37 | return InitializedAs() 38 | .ApplyForAny((message) => message.Value) 39 | .ApplyForAny((state, message) => state + message.Value) 40 | .AggregateAllMessages(); 41 | } 42 | } 43 | 44 | public int SumOfAllValuesAfterSomeThingHappened 45 | { 46 | get 47 | { 48 | return InitializedWith(0) 49 | .ApplyForAny((state, message) => state + message.Value) 50 | .ApplyForAny((state, message) => state + message.Value) 51 | .ApplyForAny(0) 52 | .AggregateAllMessages(); 53 | } 54 | } 55 | 56 | public string PassInitialState 57 | { 58 | get 59 | { 60 | return InitializedWith("CQRS") 61 | .AggregateAllMessages(); 62 | } 63 | } 64 | 65 | public string ReturnDefaultValue 66 | { 67 | get 68 | { 69 | return InitializedAs() 70 | .AggregateAllMessages(); 71 | } 72 | } 73 | 74 | public string ElseCase 75 | { 76 | get 77 | { 78 | return InitializedAs() 79 | .ApplyForAny(message => "Hello World") 80 | .Otherwise("Else") 81 | .AggregateAllMessages(); 82 | } 83 | } 84 | 85 | public string ElseCaseNotNeeded 86 | { 87 | get 88 | { 89 | return InitializedAs() 90 | .ApplyForAny(message => "Else Not Needed") 91 | .Otherwise("Else") 92 | .AggregateAllMessages(); 93 | } 94 | } 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /src/Fluent-CQRS.Tests/AggregatesRepositoryTests.cs: -------------------------------------------------------------------------------- 1 | using Fluent_CQRS.Tests.Infrastructure; 2 | using NUnit.Framework; 3 | using System.Linq; 4 | using FluentAssertions; 5 | 6 | namespace Fluent_CQRS.Tests 7 | { 8 | [TestFixture] 9 | public class AggregatesRepositoryTests : With_Aggregates_Repository 10 | { 11 | [Test] 12 | public void When_an_aggregate_fires_4_events_after_creating_then_should_the_repository_replay_only_this_4_events() 13 | { 14 | var aggrId = "TestAggr"; 15 | 16 | _aggregates 17 | .PublishNewStateTo(_eventHandler); 18 | 19 | _aggregates 20 | .Provide() 21 | .With(new TestCommand { Id = aggrId }) 22 | .Do(aggr => aggr.DoFourActions()); 23 | 24 | _eventStore.RetrieveFor(aggrId).Count().Should().Be(4); 25 | 26 | _eventHandler.RecievedEvents.Clear(); 27 | 28 | _aggregates 29 | .ReplayFor() 30 | .EventsWithAggregateId(aggrId) 31 | .ToAllEventHandlers(); 32 | 33 | _eventHandler.RecievedEvents.Count.Should().Be(4); 34 | } 35 | 36 | [Test] 37 | public void When_replay_from_an_Aggregate_into_alternative_Event_Handler_it_should_have_the_same_events_like_the_default_Event_Handler() 38 | { 39 | var aggrId = "TestAggr"; 40 | 41 | _aggregates 42 | .PublishNewStateTo(_eventHandler); 43 | 44 | _aggregates 45 | .Provide() 46 | .With(new TestCommand { Id = aggrId }) 47 | .Do(aggr => aggr.DoFourActions()); 48 | 49 | var alternativeEventHandler = new AlternativeTestEventHandler(); 50 | 51 | _aggregates 52 | .ReplayFor() 53 | .EventsWithAggregateId(aggrId) 54 | .To(alternativeEventHandler); 55 | 56 | _eventHandler.RecievedEvents.Count.Should().Be(4); 57 | alternativeEventHandler.RecievedEvents.Count.Should().Be(4); 58 | 59 | _eventHandler.RecievedEvents.ShouldAllBeEquivalentTo(alternativeEventHandler.RecievedEvents); 60 | } 61 | 62 | [Test] 63 | public void When_replay_Events_it_should_only_replay_from_a_specific_Aggregate() 64 | { 65 | var aggrId = "TestAggr"; 66 | 67 | _aggregates 68 | .PublishNewStateTo(_eventHandler); 69 | 70 | _aggregates 71 | .Provide() 72 | .With(new TestCommand { Id = aggrId }) 73 | .Do(aggr => aggr.DoFourActions()); 74 | 75 | _aggregates 76 | .Provide() 77 | .With(new TestCommand { Id = aggrId }) 78 | .Do(aggr => aggr.DoAlsoSomething()); 79 | 80 | _eventStore.RetrieveFor(aggrId).Count().Should().Be(5); 81 | 82 | _eventHandler.RecievedEvents.Clear(); 83 | 84 | _aggregates 85 | .ReplayFor() 86 | .EventsWithAggregateId(aggrId) 87 | .ToAllEventHandlers(); 88 | 89 | _eventHandler.RecievedEvents.Count.Should().Be(4); 90 | } 91 | 92 | [Test] 93 | public void When_replay_Events_of_only_one_type_it_should_ignore_all_other() 94 | { 95 | var aggrId = "TestAggr"; 96 | 97 | _aggregates 98 | .PublishNewStateTo(_eventHandler); 99 | 100 | _aggregates 101 | .Provide() 102 | .With(new TestCommand { Id = aggrId }) 103 | .Do(aggr => aggr.DoSomethingOnce()); 104 | 105 | _aggregates 106 | .Provide() 107 | .With(new TestCommand { Id = aggrId }) 108 | .Do(aggr => aggr.DoSomething()); 109 | 110 | _eventStore.RetrieveFor(aggrId).Count().Should().Be(2); 111 | 112 | _eventHandler.RecievedEvents.Clear(); 113 | 114 | _aggregates 115 | .ReplayFor() 116 | .EventsWithAggregateId(aggrId) 117 | .OfType() 118 | .ToAllEventHandlers(); 119 | 120 | _eventHandler.RecievedEvents.Count.Should().Be(1); 121 | } 122 | 123 | [Test] 124 | public void When_replay_Events_it_should_replay_all_events_for_aggregate_type() 125 | { 126 | var aggrId = "TestAggr"; 127 | var otherAggrId = "OtherTestAggr"; 128 | 129 | _aggregates 130 | .PublishNewStateTo(_eventHandler); 131 | 132 | _aggregates 133 | .Provide() 134 | .With(new TestCommand { Id = aggrId }) 135 | .Do(aggr => aggr.DoSomething()); 136 | 137 | _aggregates 138 | .Provide() 139 | .With(new TestCommand { Id = otherAggrId }) 140 | .Do(aggr => aggr.DoSomething()); 141 | 142 | _eventStore.RetrieveFor(aggrId).Count().Should().Be(1); 143 | _eventStore.RetrieveFor(otherAggrId).Count().Should().Be(1); 144 | 145 | _eventHandler.RecievedEvents.Clear(); 146 | 147 | _aggregates 148 | .ReplayFor() 149 | .AllEvents() 150 | .ToAllEventHandlers(); 151 | 152 | _eventHandler.RecievedEvents.Count.Should().Be(2); 153 | } 154 | 155 | [Test] 156 | public void When_replay_Events_it_should_replay_only_events_for_that_aggregate_type() 157 | { 158 | var aggrId = "TestAggr"; 159 | 160 | _aggregates 161 | .PublishNewStateTo(_eventHandler); 162 | 163 | _aggregates 164 | .Provide() 165 | .With(new TestCommand { Id = aggrId }) 166 | .Do(aggr => aggr.DoSomething()); 167 | 168 | _aggregates 169 | .Provide() 170 | .With(new TestCommand { Id = aggrId }) 171 | .Do(aggr => aggr.DoAlsoSomething()); 172 | 173 | _eventStore.RetrieveFor(aggrId).Count().Should().Be(2); 174 | 175 | _eventHandler.RecievedEvents.Clear(); 176 | 177 | _aggregates 178 | .ReplayFor() 179 | .AllEvents() 180 | .ToAllEventHandlers(); 181 | 182 | _eventHandler.RecievedEvents.Count.Should().Be(1); 183 | } 184 | 185 | [Test] 186 | public void When_get_an_Aggregate_by_AggregateId() 187 | { 188 | var aggrId = "Test"; 189 | 190 | _aggregates 191 | .Provide() 192 | .With(new AggregateId(aggrId)) 193 | .Do(aggr => aggr.Id.Should().Be(aggrId)); 194 | } 195 | } 196 | } 197 | -------------------------------------------------------------------------------- /src/Fluent-CQRS.Tests/Fluent-CQRS.Tests.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {B78C9B6E-CEF1-4361-BD26-F62F73A51188} 8 | Library 9 | Properties 10 | Fluent_CQRS.Tests 11 | Fluent-CQRS.Tests 12 | v4.5 13 | 512 14 | 15 | 16 | true 17 | full 18 | false 19 | bin\Debug\ 20 | DEBUG;TRACE 21 | prompt 22 | 4 23 | 24 | 25 | pdbonly 26 | true 27 | bin\Release\ 28 | TRACE 29 | prompt 30 | 4 31 | 32 | 33 | 34 | False 35 | ..\..\packages\FluentAssertions\lib\net45\FluentAssertions.dll 36 | 37 | 38 | False 39 | ..\..\packages\FluentAssertions\lib\net45\FluentAssertions.Core.dll 40 | 41 | 42 | False 43 | ..\..\packages\NUnit\lib\nunit.framework.dll 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | {413E358A-9009-418A-B9ED-0969F3C325F0} 80 | Fluent-CQRS 81 | 82 | 83 | 84 | 85 | 86 | 87 | 94 | -------------------------------------------------------------------------------- /src/Fluent-CQRS.Tests/Infrastructure/AlternativeTestAggregate.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | 4 | namespace Fluent_CQRS.Tests.Infrastructure 5 | { 6 | class AlternativeTestAggregate : Aggregate 7 | { 8 | public AlternativeTestAggregate(string id, IEnumerable history) : base(id, history) 9 | { 10 | } 11 | 12 | public void DoAlsoSomething() 13 | { 14 | Changes.Add(new SomethingHappend()); 15 | } 16 | } 17 | } -------------------------------------------------------------------------------- /src/Fluent-CQRS.Tests/Infrastructure/AlternativeTestEventHandler.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace Fluent_CQRS.Tests.Infrastructure 4 | { 5 | class AlternativeTestEventHandler : IHandleEvents 6 | { 7 | public List RecievedEvents = new List(); 8 | 9 | public void Receive(IEnumerable events) 10 | { 11 | RecievedEvents.AddRange(events); 12 | } 13 | } 14 | } -------------------------------------------------------------------------------- /src/Fluent-CQRS.Tests/Infrastructure/BusinessFault.cs: -------------------------------------------------------------------------------- 1 | namespace Fluent_CQRS.Tests.Infrastructure 2 | { 3 | public class BusinessFault : Fault 4 | { 5 | public BusinessFault() : base("My BusinessFault") 6 | { 7 | } 8 | } 9 | } -------------------------------------------------------------------------------- /src/Fluent-CQRS.Tests/Infrastructure/EmptyTestAggregate.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Linq; 3 | 4 | namespace Fluent_CQRS.Tests.Infrastructure 5 | { 6 | class EmptyTestAggregate : Aggregate 7 | { 8 | public EmptyTestAggregate(string id, IEnumerable history) : base(id, history) 9 | { 10 | if (MessagesOfType().Any()) return; 11 | 12 | Changes.Add(new InstanceCreated 13 | { 14 | Id = Id 15 | }); 16 | } 17 | 18 | public void DoNothing() { } 19 | } 20 | } -------------------------------------------------------------------------------- /src/Fluent-CQRS.Tests/Infrastructure/InstanceCreated.cs: -------------------------------------------------------------------------------- 1 | namespace Fluent_CQRS.Tests.Infrastructure 2 | { 3 | public class InstanceCreated : IAmAnEventMessage 4 | { 5 | public string Id { get; set; } 6 | } 7 | } -------------------------------------------------------------------------------- /src/Fluent-CQRS.Tests/Infrastructure/NeverUsed.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace Fluent_CQRS.Tests.Infrastructure 8 | { 9 | class NeverUsed : IAmAnEventMessage 10 | { 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/Fluent-CQRS.Tests/Infrastructure/SomeThingElseHappened.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace Fluent_CQRS.Tests.Infrastructure 8 | { 9 | class SomeThingElseHappened : IAmAnEventMessage 10 | { 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/Fluent-CQRS.Tests/Infrastructure/SomethingHappend.cs: -------------------------------------------------------------------------------- 1 | namespace Fluent_CQRS.Tests.Infrastructure 2 | { 3 | public class SomethingHappend : IAmAnEventMessage 4 | { 5 | } 6 | } -------------------------------------------------------------------------------- /src/Fluent-CQRS.Tests/Infrastructure/SomethingHappendOnce.cs: -------------------------------------------------------------------------------- 1 | namespace Fluent_CQRS.Tests.Infrastructure 2 | { 3 | internal class SomethingHappendOnce : IAmAnEventMessage 4 | { 5 | } 6 | } -------------------------------------------------------------------------------- /src/Fluent-CQRS.Tests/Infrastructure/SomethingSpecialHappend.cs: -------------------------------------------------------------------------------- 1 | namespace Fluent_CQRS.Tests.Infrastructure 2 | { 3 | public class SomethingSpecialHappend : IAmAnEventMessage 4 | { 5 | public string NiceProperty { get; set; } 6 | } 7 | } -------------------------------------------------------------------------------- /src/Fluent-CQRS.Tests/Infrastructure/TestAggregate.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.ComponentModel; 4 | using System.Linq; 5 | 6 | namespace Fluent_CQRS.Tests.Infrastructure 7 | { 8 | class TestAggregate : Aggregate 9 | { 10 | public TestAggregate(string id, IEnumerable history) 11 | : base(id, history) 12 | { 13 | 14 | } 15 | 16 | public void DoSomething() 17 | { 18 | Changes.Add(new SomethingHappend()); 19 | } 20 | 21 | public void DoSomethingOnce() 22 | { 23 | if (MessagesOfType().Any()) 24 | { 25 | Replay(new SomethingHappendOnce()); 26 | } 27 | else 28 | { 29 | Changes.Add(new SomethingHappendOnce()); 30 | } 31 | } 32 | 33 | public void ThrowException() 34 | { 35 | Changes.Add(new SomethingHappend()); 36 | 37 | throw new ApplicationException("This is a intentionally Exception"); 38 | } 39 | 40 | public void ThrowFault() 41 | { 42 | Changes.Add(new SomethingHappend()); 43 | 44 | throw new BusinessFault(); 45 | } 46 | 47 | public void DoFourActions() 48 | { 49 | Changes.Add(new SomethingSpecialHappend 50 | { 51 | NiceProperty = "Test1" 52 | }); 53 | 54 | Changes.Add(new SomethingSpecialHappend 55 | { 56 | NiceProperty = "Test2" 57 | }); 58 | 59 | Changes.Add(new SomethingSpecialHappend 60 | { 61 | NiceProperty = "Test3" 62 | }); 63 | 64 | Changes.Add(new SomethingSpecialHappend 65 | { 66 | NiceProperty = "Test4" 67 | }); 68 | } 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /src/Fluent-CQRS.Tests/Infrastructure/TestCommand.cs: -------------------------------------------------------------------------------- 1 | namespace Fluent_CQRS.Tests.Infrastructure 2 | { 3 | public class TestCommand : IAmACommandMessage 4 | { 5 | public string Id { get; set; } 6 | } 7 | } -------------------------------------------------------------------------------- /src/Fluent-CQRS.Tests/Infrastructure/TestEventHandler.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace Fluent_CQRS.Tests.Infrastructure 4 | { 5 | class TestEventHandler : IHandleEvents 6 | { 7 | public List RecievedEvents = new List(); 8 | 9 | public void Receive(IEnumerable events) 10 | { 11 | RecievedEvents.AddRange(events); 12 | } 13 | } 14 | } -------------------------------------------------------------------------------- /src/Fluent-CQRS.Tests/Infrastructure/ValueChanged.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace Fluent_CQRS.Tests.Infrastructure 8 | { 9 | class ValueChanged : IAmAnEventMessage 10 | { 11 | public int Value { get; set; } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/Fluent-CQRS.Tests/Infrastructure/ValueInitialized.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace Fluent_CQRS.Tests.Infrastructure 8 | { 9 | class ValueInitialized : IAmAnEventMessage 10 | { 11 | public int Value { get; set; } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/Fluent-CQRS.Tests/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("Fluent-CQRS.Tests")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("Rhenus Assets & Services GmbH & Co. KG")] 12 | [assembly: AssemblyProduct("Fluent-CQRS.Tests")] 13 | [assembly: AssemblyCopyright("Copyright © Rhenus Assets & Services GmbH & Co. KG 2015")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("9b70d27d-1abc-4cb0-94b9-d1f1651b0e61")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /src/Fluent-CQRS.Tests/StateDehydrationTests.cs: -------------------------------------------------------------------------------- 1 | using Fluent_CQRS.Tests.Infrastructure; 2 | using NUnit.Framework; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | 9 | namespace Fluent_CQRS.Tests 10 | { 11 | [TestFixture] 12 | public class StateDehydrationTests : With_AggregateUnderTest 13 | { 14 | [Test] 15 | public void When_messages_are_examined_with_MessagesOfType_they_can_be_counted() 16 | { 17 | var result = _aggregate.NumberOfValueChanges; 18 | Assert.AreEqual(4, result); 19 | } 20 | [Test] 21 | public void When_messages_are_examined_with_MessagesOfType_they_can_be_summed_up() 22 | { 23 | var result = _aggregate.SumOfValueChanges; 24 | Assert.AreEqual(14, result); 25 | } 26 | [Test] 27 | public void When_messages_are_examined_with_MessagesOfType_multiple_events_can_be_adressed() 28 | { 29 | var result = _aggregate.SumOfAnyValues; 30 | Assert.AreEqual(15, result); 31 | } 32 | [Test] 33 | public void When_messages_are_aggregated_they_can_be_summed_up() 34 | { 35 | var result = _aggregate.SumOfAnyValuesAggregated; 36 | Assert.AreEqual(15, result); 37 | } 38 | [Test] 39 | public void When_messages_are_aggregated_their_history_must_be_respected() 40 | { 41 | var result = _aggregate.SumOfAllValuesAfterSomeThingHappened; 42 | Assert.AreEqual(9, result); 43 | } 44 | [Test] 45 | public void When_messages_are_aggregated_the_state_can_be_initialized() 46 | { 47 | var result = _aggregate.PassInitialState; 48 | Assert.AreEqual("CQRS", result); 49 | } 50 | [Test] 51 | public void When_messages_are_aggregated_the_state_can_be_set_to_the_default_value_of_the_return_type() 52 | { 53 | var result = _aggregate.ReturnDefaultValue; 54 | Assert.AreEqual(null, result); 55 | } 56 | [Test] 57 | public void When_messages_are_aggregated_and_no_suitable_events_are_found_a_elsecase_can_be_defined() 58 | { 59 | var result = _aggregate.ElseCase; 60 | Assert.AreEqual("Else", result); 61 | } 62 | [Test] 63 | public void When_messages_are_aggregated_and__suitable_events_are_found_elsecase_is_ignored() 64 | { 65 | var result = _aggregate.ElseCaseNotNeeded; 66 | Assert.AreEqual("Else Not Needed", result); 67 | } 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/Fluent-CQRS.Tests/With_AggregateUnderTest.cs: -------------------------------------------------------------------------------- 1 | using Fluent_CQRS.Tests.Infrastructure; 2 | using NUnit.Framework; 3 | 4 | namespace Fluent_CQRS.Tests 5 | { 6 | public class With_AggregateUnderTest 7 | { 8 | internal AggregateUnderTest _aggregate; 9 | 10 | [SetUp] 11 | public void Setup() 12 | { 13 | var events = new IAmAnEventMessage[] 14 | { 15 | new ValueInitialized {Value = 1 }, 16 | new ValueChanged {Value = 2}, 17 | new ValueChanged {Value = 3}, 18 | new SomeThingElseHappened(), 19 | new ValueChanged {Value = 4}, 20 | new ValueChanged {Value = 5} 21 | }; 22 | 23 | _aggregate = new AggregateUnderTest("", events); 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Fluent-CQRS.Tests/With_Aggregates_Repository.cs: -------------------------------------------------------------------------------- 1 | using Fluent_CQRS.Tests.Infrastructure; 2 | using NUnit.Framework; 3 | 4 | namespace Fluent_CQRS.Tests 5 | { 6 | public class With_Aggregates_Repository 7 | { 8 | internal InMemoryEventStore _eventStore; 9 | internal Aggregates _aggregates; 10 | internal TestEventHandler _eventHandler; 11 | 12 | [SetUp] 13 | public void Setup() 14 | { 15 | _eventStore = new InMemoryEventStore(); 16 | _aggregates = Aggregates.CreateWith(_eventStore); 17 | 18 | _eventHandler = new TestEventHandler(); 19 | } 20 | } 21 | } -------------------------------------------------------------------------------- /src/Fluent-CQRS.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 2013 4 | VisualStudioVersion = 12.0.40629.0 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Framework", "Framework", "{F6610365-3D1C-4247-92DE-E5A8017645A4}" 7 | EndProject 8 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Sample", "Sample", "{1B3DBE80-9EEB-4BC3-8032-E3984BF73CFF}" 9 | EndProject 10 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{37249C96-B225-40FC-86A5-F9663D76759A}" 11 | EndProject 12 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Fluent-CQRS", "Fluent-CQRS\Fluent-CQRS.csproj", "{413E358A-9009-418A-B9ED-0969F3C325F0}" 13 | EndProject 14 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Fluent-CQRS.Sample.Domain", "Fluent-CQRS.Sample.Domain\Fluent-CQRS.Sample.Domain.csproj", "{D7F28833-BC97-4F70-866B-195AB16C0296}" 15 | EndProject 16 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Fluent-CQRS.Sample.Contracts", "Fluent-CQRS.Sample.Contracts\Fluent-CQRS.Sample.Contracts.csproj", "{00396ADD-73D9-46CF-B7A0-A74CAE5F92D2}" 17 | EndProject 18 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Fluent-CQRS.Tests", "Fluent-CQRS.Tests\Fluent-CQRS.Tests.csproj", "{B78C9B6E-CEF1-4361-BD26-F62F73A51188}" 19 | EndProject 20 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "_project", "_project", "{5463B768-5B4F-49BC-B502-0EE0CC97A156}" 21 | ProjectSection(SolutionItems) = preProject 22 | ..\scripts\build-solution.fsx = ..\scripts\build-solution.fsx 23 | ..\build.bat = ..\build.bat 24 | ..\LICENSE = ..\LICENSE 25 | package.nuspec = package.nuspec 26 | ..\paket.dependencies = ..\paket.dependencies 27 | ..\README.md = ..\README.md 28 | EndProjectSection 29 | EndProject 30 | Global 31 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 32 | Debug|Any CPU = Debug|Any CPU 33 | Debug-Net35|Any CPU = Debug-Net35|Any CPU 34 | Debug-Net40|Any CPU = Debug-Net40|Any CPU 35 | Release|Any CPU = Release|Any CPU 36 | Release-Net35|Any CPU = Release-Net35|Any CPU 37 | Release-Net40|Any CPU = Release-Net40|Any CPU 38 | EndGlobalSection 39 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 40 | {413E358A-9009-418A-B9ED-0969F3C325F0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 41 | {413E358A-9009-418A-B9ED-0969F3C325F0}.Debug|Any CPU.Build.0 = Debug|Any CPU 42 | {413E358A-9009-418A-B9ED-0969F3C325F0}.Debug-Net35|Any CPU.ActiveCfg = Debug-Net35|Any CPU 43 | {413E358A-9009-418A-B9ED-0969F3C325F0}.Debug-Net35|Any CPU.Build.0 = Debug-Net35|Any CPU 44 | {413E358A-9009-418A-B9ED-0969F3C325F0}.Debug-Net40|Any CPU.ActiveCfg = Debug-Net40|Any CPU 45 | {413E358A-9009-418A-B9ED-0969F3C325F0}.Debug-Net40|Any CPU.Build.0 = Debug-Net40|Any CPU 46 | {413E358A-9009-418A-B9ED-0969F3C325F0}.Release|Any CPU.ActiveCfg = Release|Any CPU 47 | {413E358A-9009-418A-B9ED-0969F3C325F0}.Release|Any CPU.Build.0 = Release|Any CPU 48 | {413E358A-9009-418A-B9ED-0969F3C325F0}.Release-Net35|Any CPU.ActiveCfg = Release-Net35|Any CPU 49 | {413E358A-9009-418A-B9ED-0969F3C325F0}.Release-Net35|Any CPU.Build.0 = Release-Net35|Any CPU 50 | {413E358A-9009-418A-B9ED-0969F3C325F0}.Release-Net40|Any CPU.ActiveCfg = Release-Net40|Any CPU 51 | {413E358A-9009-418A-B9ED-0969F3C325F0}.Release-Net40|Any CPU.Build.0 = Release-Net40|Any CPU 52 | {D7F28833-BC97-4F70-866B-195AB16C0296}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 53 | {D7F28833-BC97-4F70-866B-195AB16C0296}.Debug|Any CPU.Build.0 = Debug|Any CPU 54 | {D7F28833-BC97-4F70-866B-195AB16C0296}.Debug-Net35|Any CPU.ActiveCfg = Debug-Net35|Any CPU 55 | {D7F28833-BC97-4F70-866B-195AB16C0296}.Debug-Net35|Any CPU.Build.0 = Debug-Net35|Any CPU 56 | {D7F28833-BC97-4F70-866B-195AB16C0296}.Debug-Net40|Any CPU.ActiveCfg = Debug-Net40|Any CPU 57 | {D7F28833-BC97-4F70-866B-195AB16C0296}.Debug-Net40|Any CPU.Build.0 = Debug-Net40|Any CPU 58 | {D7F28833-BC97-4F70-866B-195AB16C0296}.Release|Any CPU.ActiveCfg = Release|Any CPU 59 | {D7F28833-BC97-4F70-866B-195AB16C0296}.Release|Any CPU.Build.0 = Release|Any CPU 60 | {D7F28833-BC97-4F70-866B-195AB16C0296}.Release-Net35|Any CPU.ActiveCfg = Release-Net35|Any CPU 61 | {D7F28833-BC97-4F70-866B-195AB16C0296}.Release-Net35|Any CPU.Build.0 = Release-Net35|Any CPU 62 | {D7F28833-BC97-4F70-866B-195AB16C0296}.Release-Net40|Any CPU.ActiveCfg = Release-Net40|Any CPU 63 | {D7F28833-BC97-4F70-866B-195AB16C0296}.Release-Net40|Any CPU.Build.0 = Release-Net40|Any CPU 64 | {00396ADD-73D9-46CF-B7A0-A74CAE5F92D2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 65 | {00396ADD-73D9-46CF-B7A0-A74CAE5F92D2}.Debug|Any CPU.Build.0 = Debug|Any CPU 66 | {00396ADD-73D9-46CF-B7A0-A74CAE5F92D2}.Debug-Net35|Any CPU.ActiveCfg = Debug-Net35|Any CPU 67 | {00396ADD-73D9-46CF-B7A0-A74CAE5F92D2}.Debug-Net35|Any CPU.Build.0 = Debug-Net35|Any CPU 68 | {00396ADD-73D9-46CF-B7A0-A74CAE5F92D2}.Debug-Net40|Any CPU.ActiveCfg = Debug-Net40|Any CPU 69 | {00396ADD-73D9-46CF-B7A0-A74CAE5F92D2}.Debug-Net40|Any CPU.Build.0 = Debug-Net40|Any CPU 70 | {00396ADD-73D9-46CF-B7A0-A74CAE5F92D2}.Release|Any CPU.ActiveCfg = Release|Any CPU 71 | {00396ADD-73D9-46CF-B7A0-A74CAE5F92D2}.Release|Any CPU.Build.0 = Release|Any CPU 72 | {00396ADD-73D9-46CF-B7A0-A74CAE5F92D2}.Release-Net35|Any CPU.ActiveCfg = Release-Net35|Any CPU 73 | {00396ADD-73D9-46CF-B7A0-A74CAE5F92D2}.Release-Net35|Any CPU.Build.0 = Release-Net35|Any CPU 74 | {00396ADD-73D9-46CF-B7A0-A74CAE5F92D2}.Release-Net40|Any CPU.ActiveCfg = Release-Net40|Any CPU 75 | {00396ADD-73D9-46CF-B7A0-A74CAE5F92D2}.Release-Net40|Any CPU.Build.0 = Release-Net40|Any CPU 76 | {B78C9B6E-CEF1-4361-BD26-F62F73A51188}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 77 | {B78C9B6E-CEF1-4361-BD26-F62F73A51188}.Debug|Any CPU.Build.0 = Debug|Any CPU 78 | {B78C9B6E-CEF1-4361-BD26-F62F73A51188}.Debug-Net35|Any CPU.ActiveCfg = Debug|Any CPU 79 | {B78C9B6E-CEF1-4361-BD26-F62F73A51188}.Debug-Net35|Any CPU.Build.0 = Debug|Any CPU 80 | {B78C9B6E-CEF1-4361-BD26-F62F73A51188}.Debug-Net40|Any CPU.ActiveCfg = Debug|Any CPU 81 | {B78C9B6E-CEF1-4361-BD26-F62F73A51188}.Debug-Net40|Any CPU.Build.0 = Debug|Any CPU 82 | {B78C9B6E-CEF1-4361-BD26-F62F73A51188}.Release|Any CPU.ActiveCfg = Release|Any CPU 83 | {B78C9B6E-CEF1-4361-BD26-F62F73A51188}.Release|Any CPU.Build.0 = Release|Any CPU 84 | {B78C9B6E-CEF1-4361-BD26-F62F73A51188}.Release-Net35|Any CPU.ActiveCfg = Release|Any CPU 85 | {B78C9B6E-CEF1-4361-BD26-F62F73A51188}.Release-Net35|Any CPU.Build.0 = Release|Any CPU 86 | {B78C9B6E-CEF1-4361-BD26-F62F73A51188}.Release-Net40|Any CPU.ActiveCfg = Release|Any CPU 87 | {B78C9B6E-CEF1-4361-BD26-F62F73A51188}.Release-Net40|Any CPU.Build.0 = Release|Any CPU 88 | EndGlobalSection 89 | GlobalSection(SolutionProperties) = preSolution 90 | HideSolutionNode = FALSE 91 | EndGlobalSection 92 | GlobalSection(NestedProjects) = preSolution 93 | {413E358A-9009-418A-B9ED-0969F3C325F0} = {F6610365-3D1C-4247-92DE-E5A8017645A4} 94 | {D7F28833-BC97-4F70-866B-195AB16C0296} = {1B3DBE80-9EEB-4BC3-8032-E3984BF73CFF} 95 | {00396ADD-73D9-46CF-B7A0-A74CAE5F92D2} = {1B3DBE80-9EEB-4BC3-8032-E3984BF73CFF} 96 | {B78C9B6E-CEF1-4361-BD26-F62F73A51188} = {37249C96-B225-40FC-86A5-F9663D76759A} 97 | EndGlobalSection 98 | EndGlobal 99 | -------------------------------------------------------------------------------- /src/Fluent-CQRS/Aggregate.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | 5 | namespace Fluent_CQRS 6 | { 7 | public class Aggregate 8 | { 9 | public Aggregate(string id, IEnumerable history) 10 | { 11 | Changes = new List(); 12 | History = new List(history); 13 | EventsToReplay = new List(); 14 | 15 | Id = id; 16 | } 17 | 18 | private IEnumerable History { get; set; } 19 | 20 | protected internal IList Changes { get; private set; } 21 | 22 | protected internal IList EventsToReplay { get; private set; } 23 | 24 | protected internal IEnumerable MessagesOfType() where T : IAmAnEventMessage 25 | { 26 | return History.Concat(Changes).OfType(); 27 | } 28 | 29 | protected internal Fold InitializedAs() 30 | { 31 | return new Fold(History.Concat(Changes)); 32 | } 33 | 34 | protected internal Fold InitializedWith(TState startValue) 35 | { 36 | return new Fold(History.Concat(Changes), startValue); 37 | } 38 | 39 | public void Replay(IAmAnEventMessage eventMessage) 40 | { 41 | EventsToReplay.Add(eventMessage); 42 | } 43 | 44 | public string Id { get; protected internal set; } 45 | } 46 | } -------------------------------------------------------------------------------- /src/Fluent-CQRS/AggregateId.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Fluent_CQRS 4 | { 5 | public struct AggregateId 6 | { 7 | readonly string _aggregateId; 8 | 9 | public AggregateId(string aggregateId) 10 | { 11 | _aggregateId = aggregateId; 12 | } 13 | 14 | public string Value { get { return _aggregateId; } } 15 | } 16 | } -------------------------------------------------------------------------------- /src/Fluent-CQRS/AggregateLifeCycle.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using Fluent_CQRS.Extensions; 5 | using Fluent_CQRS.Fluentation; 6 | 7 | namespace Fluent_CQRS 8 | { 9 | internal class AggregateLifeCycle : 10 | IProvideAnAggregate, 11 | IInvokeActionsOnAggregates where TAggregate : Aggregate 12 | { 13 | readonly IStoreAndRetrieveEvents _eventStore; 14 | readonly Action> _publishMethod; 15 | static TAggregate _aggregate; 16 | 17 | public AggregateLifeCycle(IStoreAndRetrieveEvents eventStore, Action> publishMethod) 18 | { 19 | _eventStore = eventStore; 20 | _publishMethod = publishMethod; 21 | } 22 | 23 | public IInvokeActionsOnAggregates With(IAmACommandMessage command) 24 | { 25 | var aggregateId = new AggregateId(command.Id); 26 | 27 | return With(aggregateId); 28 | } 29 | 30 | public IInvokeActionsOnAggregates With(AggregateId id) 31 | { 32 | var aggregateEvents = _eventStore.RetrieveFor(id.Value); 33 | 34 | _aggregate = BuildAggregateFromHistory(id.Value, aggregateEvents); 35 | 36 | return this; 37 | } 38 | 39 | public void Do(Action doAction) 40 | { 41 | var executionResult = InvokeAggregateMethod(doAction); 42 | 43 | executionResult = StoreChanges(executionResult); 44 | 45 | executionResult = PublishChanges(executionResult); 46 | 47 | } 48 | 49 | public ExecutionResult Try(Action doAction) 50 | { 51 | var executionResult = TryInvokeAggregateMethod(doAction); 52 | 53 | executionResult = TryStoreChanges(executionResult); 54 | 55 | executionResult = TryPublishChanges(executionResult); 56 | 57 | return executionResult; 58 | } 59 | 60 | private static TAggregate BuildAggregateFromHistory(string aggregateId, IEnumerable history) 61 | { 62 | var aggregateAsObject = Activator.CreateInstance(typeof(TAggregate), aggregateId, history); 63 | 64 | return ((TAggregate)aggregateAsObject); 65 | } 66 | 67 | private ExecutionResult TryInvokeAggregateMethod(Action doAction) 68 | { 69 | var executionResult = new ExecutionResult(); 70 | 71 | try 72 | { 73 | executionResult = InvokeAggregateMethod(doAction); 74 | 75 | } 76 | catch (Fault fault) 77 | { 78 | executionResult.Fault = fault; 79 | } 80 | catch (Exception ex) 81 | { 82 | 83 | executionResult.Exception = ex; 84 | } 85 | 86 | return executionResult; 87 | } 88 | 89 | private ExecutionResult InvokeAggregateMethod(Action doAction) 90 | { 91 | var executionResult = new ExecutionResult(); 92 | 93 | doAction.Invoke(_aggregate); 94 | 95 | executionResult.Executed = true; 96 | 97 | return executionResult; 98 | } 99 | 100 | private ExecutionResult TryStoreChanges(ExecutionResult executionResult) 101 | { 102 | try 103 | { 104 | executionResult = StoreChanges(executionResult); 105 | } 106 | catch (Exception ex) 107 | { 108 | executionResult.Exception = ex; 109 | return executionResult; 110 | } 111 | 112 | return executionResult; 113 | } 114 | 115 | private ExecutionResult StoreChanges(ExecutionResult executionResult) 116 | { 117 | if (executionResult.HasErrors()) return executionResult; 118 | 119 | var aggregateId = _aggregate.Id; 120 | 121 | foreach (var eventMessage in _aggregate.Changes) 122 | { 123 | _eventStore.StoreFor(aggregateId, eventMessage); 124 | } 125 | 126 | executionResult.Saved = true; 127 | 128 | return executionResult; 129 | } 130 | 131 | private ExecutionResult TryPublishChanges(ExecutionResult executionResult) 132 | { 133 | try 134 | { 135 | executionResult = PublishChanges(executionResult); 136 | } 137 | catch (Exception ex) 138 | { 139 | executionResult.Exception = ex; 140 | return executionResult; 141 | } 142 | 143 | return executionResult; 144 | } 145 | 146 | private ExecutionResult PublishChanges(ExecutionResult executionResult) 147 | { 148 | if (executionResult.HasErrors()) return executionResult; 149 | 150 | _publishMethod(_aggregate.Changes); 151 | _publishMethod(_aggregate.EventsToReplay); 152 | 153 | _aggregate.Changes.Clear(); 154 | _aggregate.EventsToReplay.Clear(); 155 | 156 | executionResult.Published = true; 157 | return executionResult; 158 | } 159 | } 160 | } -------------------------------------------------------------------------------- /src/Fluent-CQRS/Aggregates.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using Fluent_CQRS.Fluentation; 3 | 4 | namespace Fluent_CQRS 5 | { 6 | public sealed class Aggregates 7 | { 8 | private readonly IStoreAndRetrieveEvents _eventStore; 9 | private readonly EventHandlers _eventHandlers; 10 | 11 | public static Aggregates CreateWith (IStoreAndRetrieveEvents eventStore) 12 | { 13 | return new Aggregates (eventStore); 14 | } 15 | 16 | internal Aggregates (IStoreAndRetrieveEvents eventStore) 17 | { 18 | _eventStore = eventStore; 19 | _eventHandlers = new EventHandlers (); 20 | } 21 | 22 | public IProvideAnAggregate Provide () where TAggregate : Aggregate 23 | { 24 | return new AggregateLifeCycle (_eventStore, NewStateCallback); 25 | } 26 | 27 | public IConcatenateEventHandler PublishNewStateTo (IHandleEvents eventHandler) 28 | { 29 | _eventHandlers.Add (eventHandler); 30 | 31 | return _eventHandlers; 32 | } 33 | 34 | public ICollectEvents ReplayFor() where TAggregate : Aggregate 35 | { 36 | return new PlaybackEvents(_eventStore, ReplayCallback); 37 | } 38 | 39 | private void NewStateCallback (IEnumerable events) 40 | { 41 | _eventHandlers.Receive (events); 42 | } 43 | 44 | private void ReplayCallback(IEnumerable events) 45 | { 46 | _eventHandlers.Receive(events); 47 | } 48 | } 49 | } -------------------------------------------------------------------------------- /src/Fluent-CQRS/EventBag.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Fluent_CQRS 4 | { 5 | class EventBag 6 | { 7 | public string AggregateId { get; set; } 8 | public Type TypeOfAggregate { get; set; } 9 | public Type TypeOfEvent { get; set; } 10 | public IAmAnEventMessage Event { get; set; } 11 | public DateTime TimeStamp { get; set; } 12 | } 13 | } -------------------------------------------------------------------------------- /src/Fluent-CQRS/EventHandlers.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Linq; 3 | 4 | namespace Fluent_CQRS 5 | { 6 | class EventHandlers : IConcatenateEventHandler 7 | { 8 | readonly IList _receiver; 9 | 10 | public EventHandlers () 11 | { 12 | _receiver = new List (); 13 | } 14 | 15 | public void Add (IHandleEvents eventHandler) 16 | { 17 | _receiver.Add (eventHandler); 18 | } 19 | 20 | public void Receive (IEnumerable events) 21 | { 22 | foreach (var receiver in _receiver) 23 | { 24 | receiver.Receive (events); 25 | } 26 | } 27 | 28 | public IConcatenateEventHandler And (IHandleEvents eventHandler) 29 | { 30 | _receiver.Add (eventHandler); 31 | 32 | return this; 33 | } 34 | } 35 | } -------------------------------------------------------------------------------- /src/Fluent-CQRS/ExecutionResult.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Fluent_CQRS 4 | { 5 | public class ExecutionResult 6 | { 7 | public Exception Exception { get; set; } 8 | 9 | public Fault Fault { get; set; } 10 | 11 | public bool Executed { get; set; } 12 | 13 | public bool Saved { get; set; } 14 | 15 | public bool Published { get; set; } 16 | } 17 | } -------------------------------------------------------------------------------- /src/Fluent-CQRS/Extensions/EventExtension.cs: -------------------------------------------------------------------------------- 1 | using System.Linq; 2 | using System.Reflection; 3 | 4 | namespace Fluent_CQRS.Extensions 5 | { 6 | 7 | public static class EventExtension 8 | { 9 | public static void HandleMeWith (this IAmAnEventMessage @event, IHandleEvents handler) 10 | { 11 | var handlerType = handler.GetType (); 12 | var eventType = @event.GetType (); 13 | 14 | var eventHandlerMethods = 15 | from method in handlerType.GetTypeInfo().DeclaredMethods 16 | from parameter in method.GetParameters () 17 | where parameter.ParameterType == eventType 18 | select method; 19 | 20 | var allMethodes = eventHandlerMethods.ToList (); 21 | 22 | if (allMethodes.Any ()) { 23 | if (allMethodes.Count () > 1) { 24 | throw new MultipleHandlerMethodesDetected (eventType.Name); 25 | } 26 | 27 | allMethodes.First ().Invoke (handler, new object[]{ @event }); 28 | } 29 | } 30 | } 31 | } -------------------------------------------------------------------------------- /src/Fluent-CQRS/Extensions/ExecutionResultExtension.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Fluent_CQRS.Extensions 4 | { 5 | public static class ExecutionResultExtension 6 | { 7 | public static void CatchException(this ExecutionResult result, Action handle) 8 | { 9 | if (result.Exception == null) 10 | return; 11 | 12 | handle(result.Exception); 13 | } 14 | 15 | public static ExecutionResult CatchFault(this ExecutionResult result, Action handle) 16 | { 17 | if (result.Fault != null) 18 | { 19 | handle(result.Fault); 20 | } 21 | 22 | return result; 23 | } 24 | 25 | public static bool HasErrors(this ExecutionResult result) 26 | { 27 | return result.Exception != null || result.Fault != null; 28 | } 29 | } 30 | } -------------------------------------------------------------------------------- /src/Fluent-CQRS/Fault.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Fluent_CQRS 4 | { 5 | /// 6 | /// This class have to use for issues and/or faults in the business logic, not for Exceptions 7 | /// 8 | public abstract class Fault : Exception 9 | { 10 | protected Fault(string message) : base(message) 11 | { 12 | 13 | } 14 | } 15 | } -------------------------------------------------------------------------------- /src/Fluent-CQRS/Fluent-CQRS.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {413E358A-9009-418A-B9ED-0969F3C325F0} 8 | Library 9 | Properties 10 | Fluent_CQRS 11 | Fluent-CQRS 12 | 512 13 | {786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} 14 | Profile111 15 | v4.5 16 | 17 | 18 | true 19 | full 20 | false 21 | bin\Debug\ 22 | DEBUG;TRACE 23 | prompt 24 | 4 25 | false 26 | v4.5 27 | 28 | 29 | pdbonly 30 | true 31 | bin\Release\ 32 | TRACE 33 | prompt 34 | 4 35 | false 36 | v4.5 37 | 38 | 39 | v4.0 40 | true 41 | bin\Debug-Net40\ 42 | DEBUG;TRACE 43 | full 44 | AnyCPU 45 | prompt 46 | MinimumRecommendedRules.ruleset 47 | false 48 | 4 49 | false 50 | 51 | 52 | v4.0 53 | bin\Release-Net40\ 54 | TRACE 55 | true 56 | pdbonly 57 | AnyCPU 58 | prompt 59 | MinimumRecommendedRules.ruleset 60 | false 61 | 4 62 | 63 | 64 | v3.5 65 | true 66 | bin\Debug-Net35\ 67 | DEBUG;TRACE 68 | full 69 | AnyCPU 70 | prompt 71 | MinimumRecommendedRules.ruleset 72 | false 73 | 4 74 | false 75 | 76 | 77 | v3.5 78 | bin\Release-Net35\ 79 | TRACE 80 | true 81 | pdbonly 82 | AnyCPU 83 | prompt 84 | MinimumRecommendedRules.ruleset 85 | false 86 | 4 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 129 | -------------------------------------------------------------------------------- /src/Fluent-CQRS/Fluentation/IInvokeActionsOnAggregates.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Fluent_CQRS.Fluentation 4 | { 5 | public interface IInvokeActionsOnAggregates 6 | { 7 | void Do(Action doAction); 8 | ExecutionResult Try(Action doAction); 9 | } 10 | } -------------------------------------------------------------------------------- /src/Fluent-CQRS/Fluentation/IProvideAnAggregate.cs: -------------------------------------------------------------------------------- 1 | namespace Fluent_CQRS.Fluentation 2 | { 3 | public interface IProvideAnAggregate 4 | { 5 | IInvokeActionsOnAggregates With(IAmACommandMessage message); 6 | IInvokeActionsOnAggregates With(AggregateId id); 7 | } 8 | } -------------------------------------------------------------------------------- /src/Fluent-CQRS/Fold.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | 5 | namespace Fluent_CQRS 6 | { 7 | public class Fold : IAggregateMessages 8 | { 9 | IEnumerable _messages; 10 | Mappings _mappings = new Mappings(); 11 | Func _otherwise = state => state; //identity 12 | TState _startValue; 13 | 14 | internal Fold(IEnumerable events) 15 | : this(events, default(TState)) 16 | { } 17 | 18 | internal Fold(IEnumerable messages, TState startValue) 19 | { 20 | _messages = messages; 21 | _startValue = startValue; 22 | } 23 | 24 | public Fold ApplyForAny(TState value) 25 | where TMessage : IAmAnEventMessage 26 | { 27 | return ApplyForAny((state, message) => value); 28 | } 29 | 30 | public Fold ApplyForAny(Func apply) 31 | where TMessage : IAmAnEventMessage 32 | { 33 | return ApplyForAny((state, message) => apply(message)); 34 | } 35 | 36 | public Fold ApplyForAny(Func apply) 37 | where TMessage : IAmAnEventMessage 38 | { 39 | _mappings.Add(apply); 40 | return this; 41 | } 42 | 43 | public IAggregateMessages Otherwise(TState v) 44 | { 45 | return Otherwise(state => v); 46 | } 47 | 48 | public IAggregateMessages Otherwise(Func f) 49 | { 50 | return Otherwise(state => f()); 51 | } 52 | 53 | public IAggregateMessages Otherwise(Func func) 54 | { 55 | _otherwise = func; 56 | return this; 57 | } 58 | 59 | public TState AggregateAllMessages() 60 | { 61 | var initialState = new ResultSet(_startValue, false); 62 | var dehydrated = _messages.Aggregate(initialState, (currentState, @event) 63 | => _mappings.Apply(currentState, @event)); 64 | return dehydrated.Modified 65 | ? dehydrated.State 66 | : _otherwise(dehydrated.State); 67 | } 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/Fluent-CQRS/IAggregateMessages.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace Fluent_CQRS 8 | { 9 | public interface IAggregateMessages 10 | { 11 | TResult AggregateAllMessages(); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/Fluent-CQRS/IAmACommandMessage.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Fluent_CQRS 4 | { 5 | public interface IAmACommandMessage 6 | { 7 | String Id { get; set; } 8 | } 9 | } -------------------------------------------------------------------------------- /src/Fluent-CQRS/IAmAnEventMessage.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Fluent_CQRS 4 | { 5 | public interface IAmAnEventMessage 6 | { 7 | } 8 | } -------------------------------------------------------------------------------- /src/Fluent-CQRS/ICollectEvents.cs: -------------------------------------------------------------------------------- 1 | namespace Fluent_CQRS 2 | { 3 | public interface ICollectEvents 4 | { 5 | IReplayEvents EventsWithAggregateId(string aggregateId); 6 | IReplayEvents AllEvents(); 7 | } 8 | } -------------------------------------------------------------------------------- /src/Fluent-CQRS/IConcatenateEventHandler.cs: -------------------------------------------------------------------------------- 1 | namespace Fluent_CQRS 2 | { 3 | public interface IConcatenateEventHandler 4 | { 5 | IConcatenateEventHandler And (IHandleEvents eventHandler); 6 | } 7 | } -------------------------------------------------------------------------------- /src/Fluent-CQRS/IHandleEvents.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace Fluent_CQRS 5 | { 6 | public interface IHandleEvents 7 | { 8 | void Receive (IEnumerable events); 9 | } 10 | } 11 | 12 | -------------------------------------------------------------------------------- /src/Fluent-CQRS/IReplayEvents.cs: -------------------------------------------------------------------------------- 1 | namespace Fluent_CQRS 2 | { 3 | public interface IReplayEvents 4 | { 5 | void To(IHandleEvents eventHandler); 6 | void ToAllEventHandlers(); 7 | IReplayEvents OfType() where T:IAmAnEventMessage; 8 | } 9 | } -------------------------------------------------------------------------------- /src/Fluent-CQRS/IStoreAndRetrieveEvents.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace Fluent_CQRS 4 | { 5 | public interface IStoreAndRetrieveEvents 6 | { 7 | void StoreFor(string aggegateId, IAmAnEventMessage eventMessage) where TAggregate : Aggregate; 8 | IEnumerable RetrieveFor(string aggregateId); 9 | IEnumerable RetrieveFor(string aggregateId) where TAggregate : Aggregate; 10 | IEnumerable RetrieveFor() where TAggregate : Aggregate; 11 | } 12 | } -------------------------------------------------------------------------------- /src/Fluent-CQRS/InMemoryEventStore.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | 5 | namespace Fluent_CQRS 6 | { 7 | public sealed class InMemoryEventStore : IStoreAndRetrieveEvents 8 | { 9 | private readonly IList _events; 10 | 11 | public InMemoryEventStore() 12 | { 13 | _events = new List(); 14 | } 15 | 16 | public void StoreFor(string aggregateId, IAmAnEventMessage eventMessage) where TAggregate : Aggregate 17 | { 18 | _events.Add(new EventBag 19 | { 20 | AggregateId = aggregateId, 21 | TypeOfAggregate = typeof(TAggregate), 22 | TypeOfEvent = eventMessage.GetType(), 23 | TimeStamp = DateTime.UtcNow, 24 | Event = eventMessage 25 | }); 26 | } 27 | 28 | public IEnumerable RetrieveFor(String aggregateId) 29 | { 30 | return _events 31 | .Where(eventMessage => eventMessage.AggregateId.Equals(aggregateId)) 32 | .Select(eventBag => eventBag.Event) 33 | .ToArray(); 34 | } 35 | 36 | public IEnumerable RetrieveFor(string aggregateId) 37 | where TAggregate : Aggregate 38 | { 39 | return _events 40 | .Where(eventMessage => 41 | eventMessage.AggregateId.Equals(aggregateId) 42 | && eventMessage.TypeOfAggregate == typeof(TAggregate)) 43 | .Select(eventBag => eventBag.Event) 44 | .ToArray(); 45 | } 46 | 47 | public IEnumerable RetrieveFor() 48 | where TAggregate : Aggregate 49 | { 50 | return _events 51 | .Where(eventMessage => 52 | eventMessage.TypeOfAggregate == typeof(TAggregate)) 53 | .Select(eventBag => eventBag.Event) 54 | .ToArray(); 55 | } 56 | } 57 | } -------------------------------------------------------------------------------- /src/Fluent-CQRS/Mappings.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | 5 | namespace Fluent_CQRS 6 | { 7 | class Mappings 8 | { 9 | IList, IAmAnEventMessage, ResultSet>> _mappings 10 | = new List, IAmAnEventMessage, ResultSet>>(); 11 | 12 | public ResultSet Apply(ResultSet startState, IAmAnEventMessage message) 13 | { 14 | return _mappings.Aggregate(startState, (currentState, apply) => apply(currentState, message)); 15 | } 16 | 17 | public void Add(Func apply) where TMessage : IAmAnEventMessage 18 | { 19 | _mappings.Add((state, message) => 20 | { 21 | if (message is TMessage) 22 | return new ResultSet(apply(state.State, (TMessage)message), true); 23 | else 24 | return state; 25 | }); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/Fluent-CQRS/MissingEventsPublishingTarget.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Fluent_CQRS 4 | { 5 | public class MissingEventsPublishingTarget : Exception 6 | { 7 | public MissingEventsPublishingTarget () : base ( 8 | "There is no target to recieve the Events. " + 9 | "Assign a Action> to Aggregates.PublishNewState") 10 | { 11 | } 12 | } 13 | 14 | } -------------------------------------------------------------------------------- /src/Fluent-CQRS/MultipleHandlerMethodesDetected.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Fluent_CQRS 4 | { 5 | 6 | public class MultipleHandlerMethodesDetected : Exception 7 | { 8 | public MultipleHandlerMethodesDetected (string eventType) : base ( 9 | "There are to many methodes to handle the Event " + eventType) { } 10 | } 11 | } -------------------------------------------------------------------------------- /src/Fluent-CQRS/PlaybackEvents.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | 5 | namespace Fluent_CQRS 6 | { 7 | internal class PlaybackEvents : ICollectEvents, IReplayEvents where TAggregate : Aggregate 8 | { 9 | readonly IStoreAndRetrieveEvents _eventStore; 10 | readonly Action> _replayCallback; 11 | IEnumerable _currentAggregateEvents; 12 | 13 | public PlaybackEvents(IStoreAndRetrieveEvents eventStore, Action> replayCallback) 14 | { 15 | _eventStore = eventStore; 16 | _replayCallback = replayCallback; 17 | } 18 | 19 | public IReplayEvents EventsWithAggregateId(string aggregateId) 20 | { 21 | _currentAggregateEvents = _eventStore.RetrieveFor(aggregateId); 22 | 23 | return this; 24 | } 25 | 26 | public IReplayEvents AllEvents() 27 | { 28 | _currentAggregateEvents = _eventStore.RetrieveFor(); 29 | 30 | return this; 31 | } 32 | 33 | public void To(IHandleEvents eventHandler) 34 | { 35 | eventHandler.Receive(_currentAggregateEvents); 36 | } 37 | 38 | public void ToAllEventHandlers() 39 | { 40 | _replayCallback(_currentAggregateEvents); 41 | } 42 | 43 | public IReplayEvents OfType() where TEvent : IAmAnEventMessage 44 | { 45 | _currentAggregateEvents = _currentAggregateEvents.OfType() as IEnumerable ; 46 | return this; 47 | } 48 | } 49 | } -------------------------------------------------------------------------------- /src/Fluent-CQRS/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | // 2 | using System.Reflection; 3 | using System.Runtime.InteropServices; 4 | 5 | [assembly: AssemblyTitleAttribute("Fluent CQRS Framework")] 6 | [assembly: AssemblyDescriptionAttribute("The next generation of CQRS Framework.")] 7 | [assembly: AssemblyCompanyAttribute("")] 8 | [assembly: AssemblyCopyrightAttribute("Copyright © Jan Fellien")] 9 | [assembly: GuidAttribute("41246c8a-9b02-4be4-83ec-c88a26ee6054")] 10 | [assembly: AssemblyProductAttribute("Fluent-CQRS")] 11 | [assembly: AssemblyVersionAttribute("0.0.1")] 12 | [assembly: AssemblyFileVersionAttribute("0.0.1")] 13 | namespace System { 14 | internal static class AssemblyVersionInformation { 15 | internal const string Version = "0.0.1"; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/Fluent-CQRS/ResultSet.cs: -------------------------------------------------------------------------------- 1 | namespace Fluent_CQRS 2 | { 3 | class ResultSet 4 | { 5 | public ResultSet(TResult result, bool modified) 6 | { 7 | Modified = modified; 8 | State = result; 9 | } 10 | public bool Modified { get; private set; } 11 | public TResult State { get; private set; } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/package.nuspec: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | @project@ 5 | @build.number@ 6 | @project@ 7 | @authors@ 8 | http://opensource.org/licenses/MIT 9 | https://github.com/jfellien/fluent-cqrs 10 | false 11 | @description@ 12 | @description@ 13 | @tags@ 14 | @dependencies@ 15 | @references@ 16 | 17 | @files@ 18 | -------------------------------------------------------------------------------- /tools/NuGet/nuget.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devCrowd-Team/fluent-cqrs/4f79c30b74d248ad9877a7b2de594e0155f1ed1d/tools/NuGet/nuget.exe -------------------------------------------------------------------------------- /tools/Paket/license.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | license 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 21 | 38 | 45 | 46 | 47 |
48 |
49 | 52 |

Paket

53 |
54 |
55 |
56 |
57 | 58 |

The MIT License (MIT)

59 | 60 |

Copyright (c) 2015 Alexander Groß, Steffen Forkmann

61 | 62 |

Permission is hereby granted, free of charge, to any person obtaining a copy 63 | of this software and associated documentation files (the "Software"), to deal 64 | in the Software without restriction, including without limitation the rights 65 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 66 | copies of the Software, and to permit persons to whom the Software is 67 | furnished to do so, subject to the following conditions:

68 | 69 |

The above copyright notice and this permission notice shall be included in 70 | all copies or substantial portions of the Software.

71 | 72 |

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 73 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 74 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 75 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 76 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 77 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 78 | THE SOFTWARE.

79 | 80 | 81 |
82 |
83 | F# Project 84 | 142 |
143 |
144 |
145 | Fork me on GitHub 146 | 147 | 148 | -------------------------------------------------------------------------------- /tools/Paket/paket.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devCrowd-Team/fluent-cqrs/4f79c30b74d248ad9877a7b2de594e0155f1ed1d/tools/Paket/paket.exe --------------------------------------------------------------------------------