diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..46aef81 --- /dev/null +++ b/.gitignore @@ -0,0 +1,353 @@ +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. +## +## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore + +# User-specific files +*.rsuser +*.suo +*.user +*.userosscache +*.sln.docstates + +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + +# Mono auto generated files +mono_crash.* + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +x64/ +x86/ +[Aa][Rr][Mm]/ +[Aa][Rr][Mm]64/ +bld/ +[Bb]in/ +[Oo]bj/ +[Ll]og/ +[Ll]ogs/ + +# Visual Studio 2015/2017 cache/options directory +.vs/ +# Uncomment if you have tasks that create the project's static files in wwwroot +#wwwroot/ + +# Visual Studio 2017 auto generated files +Generated\ Files/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +# NUnit +*.VisualState.xml +TestResult.xml +nunit-*.xml + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +# Benchmark Results +BenchmarkDotNet.Artifacts/ + +# .NET Core +project.lock.json +project.fragment.lock.json +artifacts/ + +# StyleCop +StyleCopReport.xml + +# Files built by Visual Studio +*_i.c +*_p.c +*_h.h +*.ilk +*.meta +*.obj +*.iobj +*.pch +*.pdb +*.ipdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*_wpftmp.csproj +*.log +*.vspscc +*.vssscc +.builds +*.pidb +*.svclog +*.scc + +# Chutzpah Test files +_Chutzpah* + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opendb +*.opensdf +*.sdf +*.cachefile +*.VC.db +*.VC.VC.opendb + +# Visual Studio profiler +*.psess +*.vsp +*.vspx +*.sap + +# Visual Studio Trace Files +*.e2e + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# AxoCover is a Code Coverage Tool +.axoCover/* +!.axoCover/settings.json + +# Visual Studio code coverage results +*.coverage +*.coveragexml + +# NCrunch +_NCrunch_* +.*crunch*.local.xml +nCrunchTemp_* + +# MightyMoose +*.mm.* +AutoTest.Net/ + +# Web workbench (sass) +.sass-cache/ + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.[Pp]ublish.xml +*.azurePubxml +# Note: Comment the next line if you want to checkin your web deploy settings, +# but database connection strings (with potential passwords) will be unencrypted +*.pubxml +*.publishproj + +# Microsoft Azure Web App publish settings. Comment the next line if you want to +# checkin your Azure Web App publish settings, but sensitive information contained +# in these scripts will be unencrypted +PublishScripts/ + +# NuGet Packages +*.nupkg +# NuGet Symbol Packages +*.snupkg +# The packages folder can be ignored because of Package Restore +**/[Pp]ackages/* +# except build/, which is used as an MSBuild target. +!**/[Pp]ackages/build/ +# Uncomment if necessary however generally it will be regenerated when needed +#!**/[Pp]ackages/repositories.config +# NuGet v3's project.json files produces more ignorable files +*.nuget.props +*.nuget.targets + +# Microsoft Azure Build Output +csx/ +*.build.csdef + +# Microsoft Azure Emulator +ecf/ +rcf/ + +# Windows Store app package directories and files +AppPackages/ +BundleArtifacts/ +Package.StoreAssociation.xml +_pkginfo.txt +*.appx +*.appxbundle +*.appxupload + +# Visual Studio cache files +# files ending in .cache can be ignored +*.[Cc]ache +# but keep track of directories ending in .cache +!?*.[Cc]ache/ + +# Others +ClientBin/ +~$* +*~ +*.dbmdl +*.dbproj.schemaview +*.jfm +*.pfx +*.publishsettings +orleans.codegen.cs + +# Including strong name files can present a security risk +# (https://github.com/github/gitignore/pull/2483#issue-259490424) +#*.snk + +# Since there are multiple workflows, uncomment next line to ignore bower_components +# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) +#bower_components/ + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file +# to a newer Visual Studio version. Backup files are not needed, +# because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm +ServiceFabricBackup/ +*.rptproj.bak + +# SQL Server files +*.mdf +*.ldf +*.ndf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings +*.rptproj.rsuser +*- [Bb]ackup.rdl +*- [Bb]ackup ([0-9]).rdl +*- [Bb]ackup ([0-9][0-9]).rdl + +# Microsoft Fakes +FakesAssemblies/ + +# GhostDoc plugin setting file +*.GhostDoc.xml + +# Node.js Tools for Visual Studio +.ntvs_analysis.dat +node_modules/ + +# Visual Studio 6 build log +*.plg + +# Visual Studio 6 workspace options file +*.opt + +# Visual Studio 6 auto-generated workspace file (contains which files were open etc.) +*.vbw + +# Visual Studio LightSwitch build output +**/*.HTMLClient/GeneratedArtifacts +**/*.DesktopClient/GeneratedArtifacts +**/*.DesktopClient/ModelManifest.xml +**/*.Server/GeneratedArtifacts +**/*.Server/ModelManifest.xml +_Pvt_Extensions + +# Paket dependency manager +.paket/paket.exe +paket-files/ + +# FAKE - F# Make +.fake/ + +# CodeRush personal settings +.cr/personal + +# Python Tools for Visual Studio (PTVS) +__pycache__/ +*.pyc + +# Cake - Uncomment if you are using it +# tools/** +# !tools/packages.config + +# Tabs Studio +*.tss + +# Telerik's JustMock configuration file +*.jmconfig + +# BizTalk build output +*.btp.cs +*.btm.cs +*.odx.cs +*.xsd.cs + +# OpenCover UI analysis results +OpenCover/ + +# Azure Stream Analytics local run output +ASALocalRun/ + +# MSBuild Binary and Structured Log +*.binlog + +# NVidia Nsight GPU debugger configuration file +*.nvuser + +# MFractors (Xamarin productivity tool) working folder +.mfractor/ + +# Local History for Visual Studio +.localhistory/ + +# BeatPulse healthcheck temp database +healthchecksdb + +# Backup folder for Package Reference Convert tool in Visual Studio 2017 +MigrationBackup/ + +# Ionide (cross platform F# VS Code tools) working folder +.ionide/ + +# JetBrains Rider +.idea/ diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..46aef81 --- /dev/null +++ b/.gitignore @@ -0,0 +1,353 @@ +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. +## +## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore + +# User-specific files +*.rsuser +*.suo +*.user +*.userosscache +*.sln.docstates + +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + +# Mono auto generated files +mono_crash.* + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +x64/ +x86/ +[Aa][Rr][Mm]/ +[Aa][Rr][Mm]64/ +bld/ +[Bb]in/ +[Oo]bj/ +[Ll]og/ +[Ll]ogs/ + +# Visual Studio 2015/2017 cache/options directory +.vs/ +# Uncomment if you have tasks that create the project's static files in wwwroot +#wwwroot/ + +# Visual Studio 2017 auto generated files +Generated\ Files/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +# NUnit +*.VisualState.xml +TestResult.xml +nunit-*.xml + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +# Benchmark Results +BenchmarkDotNet.Artifacts/ + +# .NET Core +project.lock.json +project.fragment.lock.json +artifacts/ + +# StyleCop +StyleCopReport.xml + +# Files built by Visual Studio +*_i.c +*_p.c +*_h.h +*.ilk +*.meta +*.obj +*.iobj +*.pch +*.pdb +*.ipdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*_wpftmp.csproj +*.log +*.vspscc +*.vssscc +.builds +*.pidb +*.svclog +*.scc + +# Chutzpah Test files +_Chutzpah* + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opendb +*.opensdf +*.sdf +*.cachefile +*.VC.db +*.VC.VC.opendb + +# Visual Studio profiler +*.psess +*.vsp +*.vspx +*.sap + +# Visual Studio Trace Files +*.e2e + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# AxoCover is a Code Coverage Tool +.axoCover/* +!.axoCover/settings.json + +# Visual Studio code coverage results +*.coverage +*.coveragexml + +# NCrunch +_NCrunch_* +.*crunch*.local.xml +nCrunchTemp_* + +# MightyMoose +*.mm.* +AutoTest.Net/ + +# Web workbench (sass) +.sass-cache/ + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.[Pp]ublish.xml +*.azurePubxml +# Note: Comment the next line if you want to checkin your web deploy settings, +# but database connection strings (with potential passwords) will be unencrypted +*.pubxml +*.publishproj + +# Microsoft Azure Web App publish settings. Comment the next line if you want to +# checkin your Azure Web App publish settings, but sensitive information contained +# in these scripts will be unencrypted +PublishScripts/ + +# NuGet Packages +*.nupkg +# NuGet Symbol Packages +*.snupkg +# The packages folder can be ignored because of Package Restore +**/[Pp]ackages/* +# except build/, which is used as an MSBuild target. +!**/[Pp]ackages/build/ +# Uncomment if necessary however generally it will be regenerated when needed +#!**/[Pp]ackages/repositories.config +# NuGet v3's project.json files produces more ignorable files +*.nuget.props +*.nuget.targets + +# Microsoft Azure Build Output +csx/ +*.build.csdef + +# Microsoft Azure Emulator +ecf/ +rcf/ + +# Windows Store app package directories and files +AppPackages/ +BundleArtifacts/ +Package.StoreAssociation.xml +_pkginfo.txt +*.appx +*.appxbundle +*.appxupload + +# Visual Studio cache files +# files ending in .cache can be ignored +*.[Cc]ache +# but keep track of directories ending in .cache +!?*.[Cc]ache/ + +# Others +ClientBin/ +~$* +*~ +*.dbmdl +*.dbproj.schemaview +*.jfm +*.pfx +*.publishsettings +orleans.codegen.cs + +# Including strong name files can present a security risk +# (https://github.com/github/gitignore/pull/2483#issue-259490424) +#*.snk + +# Since there are multiple workflows, uncomment next line to ignore bower_components +# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) +#bower_components/ + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file +# to a newer Visual Studio version. Backup files are not needed, +# because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm +ServiceFabricBackup/ +*.rptproj.bak + +# SQL Server files +*.mdf +*.ldf +*.ndf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings +*.rptproj.rsuser +*- [Bb]ackup.rdl +*- [Bb]ackup ([0-9]).rdl +*- [Bb]ackup ([0-9][0-9]).rdl + +# Microsoft Fakes +FakesAssemblies/ + +# GhostDoc plugin setting file +*.GhostDoc.xml + +# Node.js Tools for Visual Studio +.ntvs_analysis.dat +node_modules/ + +# Visual Studio 6 build log +*.plg + +# Visual Studio 6 workspace options file +*.opt + +# Visual Studio 6 auto-generated workspace file (contains which files were open etc.) +*.vbw + +# Visual Studio LightSwitch build output +**/*.HTMLClient/GeneratedArtifacts +**/*.DesktopClient/GeneratedArtifacts +**/*.DesktopClient/ModelManifest.xml +**/*.Server/GeneratedArtifacts +**/*.Server/ModelManifest.xml +_Pvt_Extensions + +# Paket dependency manager +.paket/paket.exe +paket-files/ + +# FAKE - F# Make +.fake/ + +# CodeRush personal settings +.cr/personal + +# Python Tools for Visual Studio (PTVS) +__pycache__/ +*.pyc + +# Cake - Uncomment if you are using it +# tools/** +# !tools/packages.config + +# Tabs Studio +*.tss + +# Telerik's JustMock configuration file +*.jmconfig + +# BizTalk build output +*.btp.cs +*.btm.cs +*.odx.cs +*.xsd.cs + +# OpenCover UI analysis results +OpenCover/ + +# Azure Stream Analytics local run output +ASALocalRun/ + +# MSBuild Binary and Structured Log +*.binlog + +# NVidia Nsight GPU debugger configuration file +*.nvuser + +# MFractors (Xamarin productivity tool) working folder +.mfractor/ + +# Local History for Visual Studio +.localhistory/ + +# BeatPulse healthcheck temp database +healthchecksdb + +# Backup folder for Package Reference Convert tool in Visual Studio 2017 +MigrationBackup/ + +# Ionide (cross platform F# VS Code tools) working folder +.ionide/ + +# JetBrains Rider +.idea/ diff --git a/src/AggregatePersistence.sln b/src/AggregatePersistence.sln new file mode 100644 index 0000000..66a8230 --- /dev/null +++ b/src/AggregatePersistence.sln @@ -0,0 +1,39 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.26124.0 +MinimumVisualStudioVersion = 15.0.26124.0 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sales", "Sales\Sales.csproj", "{018A96A1-C587-43C6-BB9C-12AB654F6CC6}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Sales", "Sales", "{7D57F573-B99C-45B1-A5C3-5710F7E4083B}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|Any CPU = Release|Any CPU + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {018A96A1-C587-43C6-BB9C-12AB654F6CC6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {018A96A1-C587-43C6-BB9C-12AB654F6CC6}.Debug|Any CPU.Build.0 = Debug|Any CPU + {018A96A1-C587-43C6-BB9C-12AB654F6CC6}.Debug|x64.ActiveCfg = Debug|Any CPU + {018A96A1-C587-43C6-BB9C-12AB654F6CC6}.Debug|x64.Build.0 = Debug|Any CPU + {018A96A1-C587-43C6-BB9C-12AB654F6CC6}.Debug|x86.ActiveCfg = Debug|Any CPU + {018A96A1-C587-43C6-BB9C-12AB654F6CC6}.Debug|x86.Build.0 = Debug|Any CPU + {018A96A1-C587-43C6-BB9C-12AB654F6CC6}.Release|Any CPU.ActiveCfg = Release|Any CPU + {018A96A1-C587-43C6-BB9C-12AB654F6CC6}.Release|Any CPU.Build.0 = Release|Any CPU + {018A96A1-C587-43C6-BB9C-12AB654F6CC6}.Release|x64.ActiveCfg = Release|Any CPU + {018A96A1-C587-43C6-BB9C-12AB654F6CC6}.Release|x64.Build.0 = Release|Any CPU + {018A96A1-C587-43C6-BB9C-12AB654F6CC6}.Release|x86.ActiveCfg = Release|Any CPU + {018A96A1-C587-43C6-BB9C-12AB654F6CC6}.Release|x86.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(NestedProjects) = preSolution + {018A96A1-C587-43C6-BB9C-12AB654F6CC6} = {7D57F573-B99C-45B1-A5C3-5710F7E4083B} + EndGlobalSection +EndGlobal diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..46aef81 --- /dev/null +++ b/.gitignore @@ -0,0 +1,353 @@ +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. +## +## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore + +# User-specific files +*.rsuser +*.suo +*.user +*.userosscache +*.sln.docstates + +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + +# Mono auto generated files +mono_crash.* + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +x64/ +x86/ +[Aa][Rr][Mm]/ +[Aa][Rr][Mm]64/ +bld/ +[Bb]in/ +[Oo]bj/ +[Ll]og/ +[Ll]ogs/ + +# Visual Studio 2015/2017 cache/options directory +.vs/ +# Uncomment if you have tasks that create the project's static files in wwwroot +#wwwroot/ + +# Visual Studio 2017 auto generated files +Generated\ Files/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +# NUnit +*.VisualState.xml +TestResult.xml +nunit-*.xml + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +# Benchmark Results +BenchmarkDotNet.Artifacts/ + +# .NET Core +project.lock.json +project.fragment.lock.json +artifacts/ + +# StyleCop +StyleCopReport.xml + +# Files built by Visual Studio +*_i.c +*_p.c +*_h.h +*.ilk +*.meta +*.obj +*.iobj +*.pch +*.pdb +*.ipdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*_wpftmp.csproj +*.log +*.vspscc +*.vssscc +.builds +*.pidb +*.svclog +*.scc + +# Chutzpah Test files +_Chutzpah* + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opendb +*.opensdf +*.sdf +*.cachefile +*.VC.db +*.VC.VC.opendb + +# Visual Studio profiler +*.psess +*.vsp +*.vspx +*.sap + +# Visual Studio Trace Files +*.e2e + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# AxoCover is a Code Coverage Tool +.axoCover/* +!.axoCover/settings.json + +# Visual Studio code coverage results +*.coverage +*.coveragexml + +# NCrunch +_NCrunch_* +.*crunch*.local.xml +nCrunchTemp_* + +# MightyMoose +*.mm.* +AutoTest.Net/ + +# Web workbench (sass) +.sass-cache/ + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.[Pp]ublish.xml +*.azurePubxml +# Note: Comment the next line if you want to checkin your web deploy settings, +# but database connection strings (with potential passwords) will be unencrypted +*.pubxml +*.publishproj + +# Microsoft Azure Web App publish settings. Comment the next line if you want to +# checkin your Azure Web App publish settings, but sensitive information contained +# in these scripts will be unencrypted +PublishScripts/ + +# NuGet Packages +*.nupkg +# NuGet Symbol Packages +*.snupkg +# The packages folder can be ignored because of Package Restore +**/[Pp]ackages/* +# except build/, which is used as an MSBuild target. +!**/[Pp]ackages/build/ +# Uncomment if necessary however generally it will be regenerated when needed +#!**/[Pp]ackages/repositories.config +# NuGet v3's project.json files produces more ignorable files +*.nuget.props +*.nuget.targets + +# Microsoft Azure Build Output +csx/ +*.build.csdef + +# Microsoft Azure Emulator +ecf/ +rcf/ + +# Windows Store app package directories and files +AppPackages/ +BundleArtifacts/ +Package.StoreAssociation.xml +_pkginfo.txt +*.appx +*.appxbundle +*.appxupload + +# Visual Studio cache files +# files ending in .cache can be ignored +*.[Cc]ache +# but keep track of directories ending in .cache +!?*.[Cc]ache/ + +# Others +ClientBin/ +~$* +*~ +*.dbmdl +*.dbproj.schemaview +*.jfm +*.pfx +*.publishsettings +orleans.codegen.cs + +# Including strong name files can present a security risk +# (https://github.com/github/gitignore/pull/2483#issue-259490424) +#*.snk + +# Since there are multiple workflows, uncomment next line to ignore bower_components +# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) +#bower_components/ + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file +# to a newer Visual Studio version. Backup files are not needed, +# because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm +ServiceFabricBackup/ +*.rptproj.bak + +# SQL Server files +*.mdf +*.ldf +*.ndf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings +*.rptproj.rsuser +*- [Bb]ackup.rdl +*- [Bb]ackup ([0-9]).rdl +*- [Bb]ackup ([0-9][0-9]).rdl + +# Microsoft Fakes +FakesAssemblies/ + +# GhostDoc plugin setting file +*.GhostDoc.xml + +# Node.js Tools for Visual Studio +.ntvs_analysis.dat +node_modules/ + +# Visual Studio 6 build log +*.plg + +# Visual Studio 6 workspace options file +*.opt + +# Visual Studio 6 auto-generated workspace file (contains which files were open etc.) +*.vbw + +# Visual Studio LightSwitch build output +**/*.HTMLClient/GeneratedArtifacts +**/*.DesktopClient/GeneratedArtifacts +**/*.DesktopClient/ModelManifest.xml +**/*.Server/GeneratedArtifacts +**/*.Server/ModelManifest.xml +_Pvt_Extensions + +# Paket dependency manager +.paket/paket.exe +paket-files/ + +# FAKE - F# Make +.fake/ + +# CodeRush personal settings +.cr/personal + +# Python Tools for Visual Studio (PTVS) +__pycache__/ +*.pyc + +# Cake - Uncomment if you are using it +# tools/** +# !tools/packages.config + +# Tabs Studio +*.tss + +# Telerik's JustMock configuration file +*.jmconfig + +# BizTalk build output +*.btp.cs +*.btm.cs +*.odx.cs +*.xsd.cs + +# OpenCover UI analysis results +OpenCover/ + +# Azure Stream Analytics local run output +ASALocalRun/ + +# MSBuild Binary and Structured Log +*.binlog + +# NVidia Nsight GPU debugger configuration file +*.nvuser + +# MFractors (Xamarin productivity tool) working folder +.mfractor/ + +# Local History for Visual Studio +.localhistory/ + +# BeatPulse healthcheck temp database +healthchecksdb + +# Backup folder for Package Reference Convert tool in Visual Studio 2017 +MigrationBackup/ + +# Ionide (cross platform F# VS Code tools) working folder +.ionide/ + +# JetBrains Rider +.idea/ diff --git a/src/AggregatePersistence.sln b/src/AggregatePersistence.sln new file mode 100644 index 0000000..66a8230 --- /dev/null +++ b/src/AggregatePersistence.sln @@ -0,0 +1,39 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.26124.0 +MinimumVisualStudioVersion = 15.0.26124.0 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sales", "Sales\Sales.csproj", "{018A96A1-C587-43C6-BB9C-12AB654F6CC6}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Sales", "Sales", "{7D57F573-B99C-45B1-A5C3-5710F7E4083B}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|Any CPU = Release|Any CPU + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {018A96A1-C587-43C6-BB9C-12AB654F6CC6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {018A96A1-C587-43C6-BB9C-12AB654F6CC6}.Debug|Any CPU.Build.0 = Debug|Any CPU + {018A96A1-C587-43C6-BB9C-12AB654F6CC6}.Debug|x64.ActiveCfg = Debug|Any CPU + {018A96A1-C587-43C6-BB9C-12AB654F6CC6}.Debug|x64.Build.0 = Debug|Any CPU + {018A96A1-C587-43C6-BB9C-12AB654F6CC6}.Debug|x86.ActiveCfg = Debug|Any CPU + {018A96A1-C587-43C6-BB9C-12AB654F6CC6}.Debug|x86.Build.0 = Debug|Any CPU + {018A96A1-C587-43C6-BB9C-12AB654F6CC6}.Release|Any CPU.ActiveCfg = Release|Any CPU + {018A96A1-C587-43C6-BB9C-12AB654F6CC6}.Release|Any CPU.Build.0 = Release|Any CPU + {018A96A1-C587-43C6-BB9C-12AB654F6CC6}.Release|x64.ActiveCfg = Release|Any CPU + {018A96A1-C587-43C6-BB9C-12AB654F6CC6}.Release|x64.Build.0 = Release|Any CPU + {018A96A1-C587-43C6-BB9C-12AB654F6CC6}.Release|x86.ActiveCfg = Release|Any CPU + {018A96A1-C587-43C6-BB9C-12AB654F6CC6}.Release|x86.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(NestedProjects) = preSolution + {018A96A1-C587-43C6-BB9C-12AB654F6CC6} = {7D57F573-B99C-45B1-A5C3-5710F7E4083B} + EndGlobalSection +EndGlobal diff --git a/src/Sales/AggregateExamples/Domain.cs b/src/Sales/AggregateExamples/Domain.cs new file mode 100644 index 0000000..2862f86 --- /dev/null +++ b/src/Sales/AggregateExamples/Domain.cs @@ -0,0 +1,93 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore; + +namespace Sales.AggregateExamples.Domain +{ + public class ShoppingCart + { + public ShoppingCart(Guid shoppingCartId, Guid customerId) + { + ShoppingCartId = shoppingCartId; + CustomerId = customerId; + } + + public Guid ShoppingCartId { get; private set; } + public Guid CustomerId { get; private set; } + public IList Items { get; set; } = new List(); + } + + public class ShoppingCartItem + { + public ShoppingCartItem(Guid shoppingCartId, Guid productId, int quantity, decimal price) + { + ShoppingCartId = shoppingCartId; + ProductId = productId; + Quantity = quantity; + Price = price; + } + + public Guid ShoppingCartId { get; set; } + public Guid ProductId { get; private set; } + public int Quantity { get; set; } + public decimal Price { get; private set; } + } + + public class ShoppingCartDomain + { + private readonly ShoppingCart _shoppingCart; + + public ShoppingCartDomain(ShoppingCart shoppingCart) + { + _shoppingCart = shoppingCart; + } + + public void AddItem(Guid productId, int quantity, decimal price) + { + var existingItem = _shoppingCart.Items.SingleOrDefault(x => x.ProductId == productId); + if (existingItem != null) + { + existingItem.Quantity += quantity; + } + else + { + _shoppingCart.Items.Add(new ShoppingCartItem(_shoppingCart.ShoppingCartId, productId, quantity, price)); + } + } + + public void RemoveItem(Guid productId) + { + var product = _shoppingCart.Items.SingleOrDefault(x => x.ProductId == productId); + if (product != null) + { + _shoppingCart.Items.Remove(product); + } + } + } + + public class ShoppingCartDomainRepository + { + private readonly SalesDbContext _dbContext; + + public ShoppingCartDomainRepository(SalesDbContext dbContext) + { + _dbContext = dbContext; + } + + public async Task GetShoppingCart(Guid shoppingCartId) + { + var data = await _dbContext.ShoppingCarts + .Include(x => x.Items) + .SingleAsync(x => x.ShoppingCartId == shoppingCartId); + + return new ShoppingCartDomain(data); + } + + public async Task Save() + { + await _dbContext.SaveChangesAsync(); + } + } +} \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..46aef81 --- /dev/null +++ b/.gitignore @@ -0,0 +1,353 @@ +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. +## +## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore + +# User-specific files +*.rsuser +*.suo +*.user +*.userosscache +*.sln.docstates + +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + +# Mono auto generated files +mono_crash.* + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +x64/ +x86/ +[Aa][Rr][Mm]/ +[Aa][Rr][Mm]64/ +bld/ +[Bb]in/ +[Oo]bj/ +[Ll]og/ +[Ll]ogs/ + +# Visual Studio 2015/2017 cache/options directory +.vs/ +# Uncomment if you have tasks that create the project's static files in wwwroot +#wwwroot/ + +# Visual Studio 2017 auto generated files +Generated\ Files/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +# NUnit +*.VisualState.xml +TestResult.xml +nunit-*.xml + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +# Benchmark Results +BenchmarkDotNet.Artifacts/ + +# .NET Core +project.lock.json +project.fragment.lock.json +artifacts/ + +# StyleCop +StyleCopReport.xml + +# Files built by Visual Studio +*_i.c +*_p.c +*_h.h +*.ilk +*.meta +*.obj +*.iobj +*.pch +*.pdb +*.ipdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*_wpftmp.csproj +*.log +*.vspscc +*.vssscc +.builds +*.pidb +*.svclog +*.scc + +# Chutzpah Test files +_Chutzpah* + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opendb +*.opensdf +*.sdf +*.cachefile +*.VC.db +*.VC.VC.opendb + +# Visual Studio profiler +*.psess +*.vsp +*.vspx +*.sap + +# Visual Studio Trace Files +*.e2e + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# AxoCover is a Code Coverage Tool +.axoCover/* +!.axoCover/settings.json + +# Visual Studio code coverage results +*.coverage +*.coveragexml + +# NCrunch +_NCrunch_* +.*crunch*.local.xml +nCrunchTemp_* + +# MightyMoose +*.mm.* +AutoTest.Net/ + +# Web workbench (sass) +.sass-cache/ + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.[Pp]ublish.xml +*.azurePubxml +# Note: Comment the next line if you want to checkin your web deploy settings, +# but database connection strings (with potential passwords) will be unencrypted +*.pubxml +*.publishproj + +# Microsoft Azure Web App publish settings. Comment the next line if you want to +# checkin your Azure Web App publish settings, but sensitive information contained +# in these scripts will be unencrypted +PublishScripts/ + +# NuGet Packages +*.nupkg +# NuGet Symbol Packages +*.snupkg +# The packages folder can be ignored because of Package Restore +**/[Pp]ackages/* +# except build/, which is used as an MSBuild target. +!**/[Pp]ackages/build/ +# Uncomment if necessary however generally it will be regenerated when needed +#!**/[Pp]ackages/repositories.config +# NuGet v3's project.json files produces more ignorable files +*.nuget.props +*.nuget.targets + +# Microsoft Azure Build Output +csx/ +*.build.csdef + +# Microsoft Azure Emulator +ecf/ +rcf/ + +# Windows Store app package directories and files +AppPackages/ +BundleArtifacts/ +Package.StoreAssociation.xml +_pkginfo.txt +*.appx +*.appxbundle +*.appxupload + +# Visual Studio cache files +# files ending in .cache can be ignored +*.[Cc]ache +# but keep track of directories ending in .cache +!?*.[Cc]ache/ + +# Others +ClientBin/ +~$* +*~ +*.dbmdl +*.dbproj.schemaview +*.jfm +*.pfx +*.publishsettings +orleans.codegen.cs + +# Including strong name files can present a security risk +# (https://github.com/github/gitignore/pull/2483#issue-259490424) +#*.snk + +# Since there are multiple workflows, uncomment next line to ignore bower_components +# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) +#bower_components/ + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file +# to a newer Visual Studio version. Backup files are not needed, +# because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm +ServiceFabricBackup/ +*.rptproj.bak + +# SQL Server files +*.mdf +*.ldf +*.ndf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings +*.rptproj.rsuser +*- [Bb]ackup.rdl +*- [Bb]ackup ([0-9]).rdl +*- [Bb]ackup ([0-9][0-9]).rdl + +# Microsoft Fakes +FakesAssemblies/ + +# GhostDoc plugin setting file +*.GhostDoc.xml + +# Node.js Tools for Visual Studio +.ntvs_analysis.dat +node_modules/ + +# Visual Studio 6 build log +*.plg + +# Visual Studio 6 workspace options file +*.opt + +# Visual Studio 6 auto-generated workspace file (contains which files were open etc.) +*.vbw + +# Visual Studio LightSwitch build output +**/*.HTMLClient/GeneratedArtifacts +**/*.DesktopClient/GeneratedArtifacts +**/*.DesktopClient/ModelManifest.xml +**/*.Server/GeneratedArtifacts +**/*.Server/ModelManifest.xml +_Pvt_Extensions + +# Paket dependency manager +.paket/paket.exe +paket-files/ + +# FAKE - F# Make +.fake/ + +# CodeRush personal settings +.cr/personal + +# Python Tools for Visual Studio (PTVS) +__pycache__/ +*.pyc + +# Cake - Uncomment if you are using it +# tools/** +# !tools/packages.config + +# Tabs Studio +*.tss + +# Telerik's JustMock configuration file +*.jmconfig + +# BizTalk build output +*.btp.cs +*.btm.cs +*.odx.cs +*.xsd.cs + +# OpenCover UI analysis results +OpenCover/ + +# Azure Stream Analytics local run output +ASALocalRun/ + +# MSBuild Binary and Structured Log +*.binlog + +# NVidia Nsight GPU debugger configuration file +*.nvuser + +# MFractors (Xamarin productivity tool) working folder +.mfractor/ + +# Local History for Visual Studio +.localhistory/ + +# BeatPulse healthcheck temp database +healthchecksdb + +# Backup folder for Package Reference Convert tool in Visual Studio 2017 +MigrationBackup/ + +# Ionide (cross platform F# VS Code tools) working folder +.ionide/ + +# JetBrains Rider +.idea/ diff --git a/src/AggregatePersistence.sln b/src/AggregatePersistence.sln new file mode 100644 index 0000000..66a8230 --- /dev/null +++ b/src/AggregatePersistence.sln @@ -0,0 +1,39 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.26124.0 +MinimumVisualStudioVersion = 15.0.26124.0 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sales", "Sales\Sales.csproj", "{018A96A1-C587-43C6-BB9C-12AB654F6CC6}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Sales", "Sales", "{7D57F573-B99C-45B1-A5C3-5710F7E4083B}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|Any CPU = Release|Any CPU + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {018A96A1-C587-43C6-BB9C-12AB654F6CC6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {018A96A1-C587-43C6-BB9C-12AB654F6CC6}.Debug|Any CPU.Build.0 = Debug|Any CPU + {018A96A1-C587-43C6-BB9C-12AB654F6CC6}.Debug|x64.ActiveCfg = Debug|Any CPU + {018A96A1-C587-43C6-BB9C-12AB654F6CC6}.Debug|x64.Build.0 = Debug|Any CPU + {018A96A1-C587-43C6-BB9C-12AB654F6CC6}.Debug|x86.ActiveCfg = Debug|Any CPU + {018A96A1-C587-43C6-BB9C-12AB654F6CC6}.Debug|x86.Build.0 = Debug|Any CPU + {018A96A1-C587-43C6-BB9C-12AB654F6CC6}.Release|Any CPU.ActiveCfg = Release|Any CPU + {018A96A1-C587-43C6-BB9C-12AB654F6CC6}.Release|Any CPU.Build.0 = Release|Any CPU + {018A96A1-C587-43C6-BB9C-12AB654F6CC6}.Release|x64.ActiveCfg = Release|Any CPU + {018A96A1-C587-43C6-BB9C-12AB654F6CC6}.Release|x64.Build.0 = Release|Any CPU + {018A96A1-C587-43C6-BB9C-12AB654F6CC6}.Release|x86.ActiveCfg = Release|Any CPU + {018A96A1-C587-43C6-BB9C-12AB654F6CC6}.Release|x86.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(NestedProjects) = preSolution + {018A96A1-C587-43C6-BB9C-12AB654F6CC6} = {7D57F573-B99C-45B1-A5C3-5710F7E4083B} + EndGlobalSection +EndGlobal diff --git a/src/Sales/AggregateExamples/Domain.cs b/src/Sales/AggregateExamples/Domain.cs new file mode 100644 index 0000000..2862f86 --- /dev/null +++ b/src/Sales/AggregateExamples/Domain.cs @@ -0,0 +1,93 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore; + +namespace Sales.AggregateExamples.Domain +{ + public class ShoppingCart + { + public ShoppingCart(Guid shoppingCartId, Guid customerId) + { + ShoppingCartId = shoppingCartId; + CustomerId = customerId; + } + + public Guid ShoppingCartId { get; private set; } + public Guid CustomerId { get; private set; } + public IList Items { get; set; } = new List(); + } + + public class ShoppingCartItem + { + public ShoppingCartItem(Guid shoppingCartId, Guid productId, int quantity, decimal price) + { + ShoppingCartId = shoppingCartId; + ProductId = productId; + Quantity = quantity; + Price = price; + } + + public Guid ShoppingCartId { get; set; } + public Guid ProductId { get; private set; } + public int Quantity { get; set; } + public decimal Price { get; private set; } + } + + public class ShoppingCartDomain + { + private readonly ShoppingCart _shoppingCart; + + public ShoppingCartDomain(ShoppingCart shoppingCart) + { + _shoppingCart = shoppingCart; + } + + public void AddItem(Guid productId, int quantity, decimal price) + { + var existingItem = _shoppingCart.Items.SingleOrDefault(x => x.ProductId == productId); + if (existingItem != null) + { + existingItem.Quantity += quantity; + } + else + { + _shoppingCart.Items.Add(new ShoppingCartItem(_shoppingCart.ShoppingCartId, productId, quantity, price)); + } + } + + public void RemoveItem(Guid productId) + { + var product = _shoppingCart.Items.SingleOrDefault(x => x.ProductId == productId); + if (product != null) + { + _shoppingCart.Items.Remove(product); + } + } + } + + public class ShoppingCartDomainRepository + { + private readonly SalesDbContext _dbContext; + + public ShoppingCartDomainRepository(SalesDbContext dbContext) + { + _dbContext = dbContext; + } + + public async Task GetShoppingCart(Guid shoppingCartId) + { + var data = await _dbContext.ShoppingCarts + .Include(x => x.Items) + .SingleAsync(x => x.ShoppingCartId == shoppingCartId); + + return new ShoppingCartDomain(data); + } + + public async Task Save() + { + await _dbContext.SaveChangesAsync(); + } + } +} \ No newline at end of file diff --git a/src/Sales/AggregateExamples/Entity.cs b/src/Sales/AggregateExamples/Entity.cs new file mode 100644 index 0000000..f391701 --- /dev/null +++ b/src/Sales/AggregateExamples/Entity.cs @@ -0,0 +1,31 @@ +using System; +using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore; +using Sales.AggregateExamples.Domain; + +namespace Sales.AggregateExamples.Entity +{ + public class ShoppingCartRepository + { + private readonly SalesDbContext _dbContext; + + public ShoppingCartRepository(SalesDbContext dbContext) + { + _dbContext = dbContext; + } + + public async Task GetShoppingCart(Guid shoppingCartId) + { + return await _dbContext.ShoppingCarts + .Include(x => x.Items) + .SingleAsync(x => x.ShoppingCartId == shoppingCartId); + } + + public async Task Save() + { + await _dbContext.SaveChangesAsync(); + } + } + + +} \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..46aef81 --- /dev/null +++ b/.gitignore @@ -0,0 +1,353 @@ +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. +## +## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore + +# User-specific files +*.rsuser +*.suo +*.user +*.userosscache +*.sln.docstates + +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + +# Mono auto generated files +mono_crash.* + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +x64/ +x86/ +[Aa][Rr][Mm]/ +[Aa][Rr][Mm]64/ +bld/ +[Bb]in/ +[Oo]bj/ +[Ll]og/ +[Ll]ogs/ + +# Visual Studio 2015/2017 cache/options directory +.vs/ +# Uncomment if you have tasks that create the project's static files in wwwroot +#wwwroot/ + +# Visual Studio 2017 auto generated files +Generated\ Files/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +# NUnit +*.VisualState.xml +TestResult.xml +nunit-*.xml + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +# Benchmark Results +BenchmarkDotNet.Artifacts/ + +# .NET Core +project.lock.json +project.fragment.lock.json +artifacts/ + +# StyleCop +StyleCopReport.xml + +# Files built by Visual Studio +*_i.c +*_p.c +*_h.h +*.ilk +*.meta +*.obj +*.iobj +*.pch +*.pdb +*.ipdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*_wpftmp.csproj +*.log +*.vspscc +*.vssscc +.builds +*.pidb +*.svclog +*.scc + +# Chutzpah Test files +_Chutzpah* + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opendb +*.opensdf +*.sdf +*.cachefile +*.VC.db +*.VC.VC.opendb + +# Visual Studio profiler +*.psess +*.vsp +*.vspx +*.sap + +# Visual Studio Trace Files +*.e2e + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# AxoCover is a Code Coverage Tool +.axoCover/* +!.axoCover/settings.json + +# Visual Studio code coverage results +*.coverage +*.coveragexml + +# NCrunch +_NCrunch_* +.*crunch*.local.xml +nCrunchTemp_* + +# MightyMoose +*.mm.* +AutoTest.Net/ + +# Web workbench (sass) +.sass-cache/ + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.[Pp]ublish.xml +*.azurePubxml +# Note: Comment the next line if you want to checkin your web deploy settings, +# but database connection strings (with potential passwords) will be unencrypted +*.pubxml +*.publishproj + +# Microsoft Azure Web App publish settings. Comment the next line if you want to +# checkin your Azure Web App publish settings, but sensitive information contained +# in these scripts will be unencrypted +PublishScripts/ + +# NuGet Packages +*.nupkg +# NuGet Symbol Packages +*.snupkg +# The packages folder can be ignored because of Package Restore +**/[Pp]ackages/* +# except build/, which is used as an MSBuild target. +!**/[Pp]ackages/build/ +# Uncomment if necessary however generally it will be regenerated when needed +#!**/[Pp]ackages/repositories.config +# NuGet v3's project.json files produces more ignorable files +*.nuget.props +*.nuget.targets + +# Microsoft Azure Build Output +csx/ +*.build.csdef + +# Microsoft Azure Emulator +ecf/ +rcf/ + +# Windows Store app package directories and files +AppPackages/ +BundleArtifacts/ +Package.StoreAssociation.xml +_pkginfo.txt +*.appx +*.appxbundle +*.appxupload + +# Visual Studio cache files +# files ending in .cache can be ignored +*.[Cc]ache +# but keep track of directories ending in .cache +!?*.[Cc]ache/ + +# Others +ClientBin/ +~$* +*~ +*.dbmdl +*.dbproj.schemaview +*.jfm +*.pfx +*.publishsettings +orleans.codegen.cs + +# Including strong name files can present a security risk +# (https://github.com/github/gitignore/pull/2483#issue-259490424) +#*.snk + +# Since there are multiple workflows, uncomment next line to ignore bower_components +# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) +#bower_components/ + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file +# to a newer Visual Studio version. Backup files are not needed, +# because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm +ServiceFabricBackup/ +*.rptproj.bak + +# SQL Server files +*.mdf +*.ldf +*.ndf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings +*.rptproj.rsuser +*- [Bb]ackup.rdl +*- [Bb]ackup ([0-9]).rdl +*- [Bb]ackup ([0-9][0-9]).rdl + +# Microsoft Fakes +FakesAssemblies/ + +# GhostDoc plugin setting file +*.GhostDoc.xml + +# Node.js Tools for Visual Studio +.ntvs_analysis.dat +node_modules/ + +# Visual Studio 6 build log +*.plg + +# Visual Studio 6 workspace options file +*.opt + +# Visual Studio 6 auto-generated workspace file (contains which files were open etc.) +*.vbw + +# Visual Studio LightSwitch build output +**/*.HTMLClient/GeneratedArtifacts +**/*.DesktopClient/GeneratedArtifacts +**/*.DesktopClient/ModelManifest.xml +**/*.Server/GeneratedArtifacts +**/*.Server/ModelManifest.xml +_Pvt_Extensions + +# Paket dependency manager +.paket/paket.exe +paket-files/ + +# FAKE - F# Make +.fake/ + +# CodeRush personal settings +.cr/personal + +# Python Tools for Visual Studio (PTVS) +__pycache__/ +*.pyc + +# Cake - Uncomment if you are using it +# tools/** +# !tools/packages.config + +# Tabs Studio +*.tss + +# Telerik's JustMock configuration file +*.jmconfig + +# BizTalk build output +*.btp.cs +*.btm.cs +*.odx.cs +*.xsd.cs + +# OpenCover UI analysis results +OpenCover/ + +# Azure Stream Analytics local run output +ASALocalRun/ + +# MSBuild Binary and Structured Log +*.binlog + +# NVidia Nsight GPU debugger configuration file +*.nvuser + +# MFractors (Xamarin productivity tool) working folder +.mfractor/ + +# Local History for Visual Studio +.localhistory/ + +# BeatPulse healthcheck temp database +healthchecksdb + +# Backup folder for Package Reference Convert tool in Visual Studio 2017 +MigrationBackup/ + +# Ionide (cross platform F# VS Code tools) working folder +.ionide/ + +# JetBrains Rider +.idea/ diff --git a/src/AggregatePersistence.sln b/src/AggregatePersistence.sln new file mode 100644 index 0000000..66a8230 --- /dev/null +++ b/src/AggregatePersistence.sln @@ -0,0 +1,39 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.26124.0 +MinimumVisualStudioVersion = 15.0.26124.0 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sales", "Sales\Sales.csproj", "{018A96A1-C587-43C6-BB9C-12AB654F6CC6}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Sales", "Sales", "{7D57F573-B99C-45B1-A5C3-5710F7E4083B}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|Any CPU = Release|Any CPU + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {018A96A1-C587-43C6-BB9C-12AB654F6CC6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {018A96A1-C587-43C6-BB9C-12AB654F6CC6}.Debug|Any CPU.Build.0 = Debug|Any CPU + {018A96A1-C587-43C6-BB9C-12AB654F6CC6}.Debug|x64.ActiveCfg = Debug|Any CPU + {018A96A1-C587-43C6-BB9C-12AB654F6CC6}.Debug|x64.Build.0 = Debug|Any CPU + {018A96A1-C587-43C6-BB9C-12AB654F6CC6}.Debug|x86.ActiveCfg = Debug|Any CPU + {018A96A1-C587-43C6-BB9C-12AB654F6CC6}.Debug|x86.Build.0 = Debug|Any CPU + {018A96A1-C587-43C6-BB9C-12AB654F6CC6}.Release|Any CPU.ActiveCfg = Release|Any CPU + {018A96A1-C587-43C6-BB9C-12AB654F6CC6}.Release|Any CPU.Build.0 = Release|Any CPU + {018A96A1-C587-43C6-BB9C-12AB654F6CC6}.Release|x64.ActiveCfg = Release|Any CPU + {018A96A1-C587-43C6-BB9C-12AB654F6CC6}.Release|x64.Build.0 = Release|Any CPU + {018A96A1-C587-43C6-BB9C-12AB654F6CC6}.Release|x86.ActiveCfg = Release|Any CPU + {018A96A1-C587-43C6-BB9C-12AB654F6CC6}.Release|x86.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(NestedProjects) = preSolution + {018A96A1-C587-43C6-BB9C-12AB654F6CC6} = {7D57F573-B99C-45B1-A5C3-5710F7E4083B} + EndGlobalSection +EndGlobal diff --git a/src/Sales/AggregateExamples/Domain.cs b/src/Sales/AggregateExamples/Domain.cs new file mode 100644 index 0000000..2862f86 --- /dev/null +++ b/src/Sales/AggregateExamples/Domain.cs @@ -0,0 +1,93 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore; + +namespace Sales.AggregateExamples.Domain +{ + public class ShoppingCart + { + public ShoppingCart(Guid shoppingCartId, Guid customerId) + { + ShoppingCartId = shoppingCartId; + CustomerId = customerId; + } + + public Guid ShoppingCartId { get; private set; } + public Guid CustomerId { get; private set; } + public IList Items { get; set; } = new List(); + } + + public class ShoppingCartItem + { + public ShoppingCartItem(Guid shoppingCartId, Guid productId, int quantity, decimal price) + { + ShoppingCartId = shoppingCartId; + ProductId = productId; + Quantity = quantity; + Price = price; + } + + public Guid ShoppingCartId { get; set; } + public Guid ProductId { get; private set; } + public int Quantity { get; set; } + public decimal Price { get; private set; } + } + + public class ShoppingCartDomain + { + private readonly ShoppingCart _shoppingCart; + + public ShoppingCartDomain(ShoppingCart shoppingCart) + { + _shoppingCart = shoppingCart; + } + + public void AddItem(Guid productId, int quantity, decimal price) + { + var existingItem = _shoppingCart.Items.SingleOrDefault(x => x.ProductId == productId); + if (existingItem != null) + { + existingItem.Quantity += quantity; + } + else + { + _shoppingCart.Items.Add(new ShoppingCartItem(_shoppingCart.ShoppingCartId, productId, quantity, price)); + } + } + + public void RemoveItem(Guid productId) + { + var product = _shoppingCart.Items.SingleOrDefault(x => x.ProductId == productId); + if (product != null) + { + _shoppingCart.Items.Remove(product); + } + } + } + + public class ShoppingCartDomainRepository + { + private readonly SalesDbContext _dbContext; + + public ShoppingCartDomainRepository(SalesDbContext dbContext) + { + _dbContext = dbContext; + } + + public async Task GetShoppingCart(Guid shoppingCartId) + { + var data = await _dbContext.ShoppingCarts + .Include(x => x.Items) + .SingleAsync(x => x.ShoppingCartId == shoppingCartId); + + return new ShoppingCartDomain(data); + } + + public async Task Save() + { + await _dbContext.SaveChangesAsync(); + } + } +} \ No newline at end of file diff --git a/src/Sales/AggregateExamples/Entity.cs b/src/Sales/AggregateExamples/Entity.cs new file mode 100644 index 0000000..f391701 --- /dev/null +++ b/src/Sales/AggregateExamples/Entity.cs @@ -0,0 +1,31 @@ +using System; +using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore; +using Sales.AggregateExamples.Domain; + +namespace Sales.AggregateExamples.Entity +{ + public class ShoppingCartRepository + { + private readonly SalesDbContext _dbContext; + + public ShoppingCartRepository(SalesDbContext dbContext) + { + _dbContext = dbContext; + } + + public async Task GetShoppingCart(Guid shoppingCartId) + { + return await _dbContext.ShoppingCarts + .Include(x => x.Items) + .SingleAsync(x => x.ShoppingCartId == shoppingCartId); + } + + public async Task Save() + { + await _dbContext.SaveChangesAsync(); + } + } + + +} \ No newline at end of file diff --git a/src/Sales/AggregateExamples/Events.cs b/src/Sales/AggregateExamples/Events.cs new file mode 100644 index 0000000..f974e80 --- /dev/null +++ b/src/Sales/AggregateExamples/Events.cs @@ -0,0 +1,189 @@ +using System; +using System.Collections.Generic; +using System.Data; +using System.Linq; +using System.Threading.Tasks; +using Dapper; + +namespace Sales.AggregateExamples.Events +{ + public class ShoppingCart + { + public ShoppingCart(Guid shoppingCartId, Guid customerId) + { + ShoppingCartId = shoppingCartId; + CustomerId = customerId; + } + + public Guid ShoppingCartId { get; private set; } + public Guid CustomerId { get; private set; } + public IList Items { get; set; } = new List(); + } + + public class ShoppingCartItem + { + public ShoppingCartItem(Guid shoppingCartId, Guid productId) + { + ShoppingCartId = shoppingCartId; + ProductId = productId; + } + + public Guid ShoppingCartId { get; set; } + public Guid ProductId { get; set; } + } + + + + public class ShoppingCartEventDomain + { + private List _events = new List(); + private readonly ShoppingCart _shoppingCart; + + public ShoppingCartEventDomain(ShoppingCart shoppingCart) + { + _shoppingCart = shoppingCart; + } + + public List GetEvents() + { + return _events; + } + + public void AddItem(Guid productId, int quantity, decimal price) + { + var existingItem = _shoppingCart.Items.SingleOrDefault(x => x.ProductId == productId); + if (existingItem != null) + { + _events.Add(new QuantityIncremented + { + ShoppingCartId = _shoppingCart.ShoppingCartId, + ProductId = productId, + Quantity = quantity + }); + } + else + { + _shoppingCart.Items.Add(new ShoppingCartItem(_shoppingCart.ShoppingCartId, productId)); + + _events.Add(new ItemAdded + { + ShoppingCartId = _shoppingCart.ShoppingCartId, + ProductId = productId, + Quantity = quantity, + Price = price + }); + } + } + + public void RemoveItem(Guid productId) + { + var product = _shoppingCart.Items.SingleOrDefault(x => x.ProductId == productId); + if (product != null) + { + _events.Add(new ItemRemoved + { + ShoppingCartId = _shoppingCart.ShoppingCartId, + ProductId = productId, + }); + } + } + } + + public class ItemAdded + { + public Guid ShoppingCartId { get; set; } + public Guid ProductId { get; set; } + public int Quantity { get; set; } + public decimal Price { get; set; } + } + + public class ItemRemoved + { + public Guid ShoppingCartId { get; set; } + public Guid ProductId { get; set; } + } + + public class QuantityIncremented + { + public Guid ShoppingCartId { get; set; } + public Guid ProductId { get; set; } + public int Quantity { get; set; } + } + + public class ShoppingCartRepository + { + private readonly IDbConnection _connection; + + public ShoppingCartRepository(IDbConnection connection) + { + _connection = connection; + } + + public async Task GetShoppingCart(Guid shoppingCartId) + { + var shoppingCart = await _connection.QueryFirstAsync("SELECT CustomerId FROM ShoppingCarts WHERE ShoppingCartId=@ShopingCartId", + new {ShoppingCartId = shoppingCartId}); + + var items = await _connection.QueryAsync("SELECT ProductId, Quantity FROM ShoppingCartItems WHERE ShoppingCartId=@ShopingCartId", + new {ShoppingCartId = shoppingCartId}); + shoppingCart.Items = items.ToList(); + + return new ShoppingCartEventDomain(shoppingCart); + } + + public async Task Save(ShoppingCartEventDomain shoppingCart) + { + var trx = _connection.BeginTransaction(); + var evnts = shoppingCart.GetEvents(); + foreach (var evnt in evnts) + { + if (evnt is ItemAdded itemAdded) + { + await ItemAdded(itemAdded, trx); + } + else if (evnt is QuantityIncremented quantityIncremented) + { + await QuantityIncremented(quantityIncremented, trx); + } + else if (evnt is ItemRemoved itemRemoved) + { + await ItemRemoved(itemRemoved, trx); + } + } + trx.Commit(); + } + + private async Task ItemAdded(ItemAdded evnt, IDbTransaction trx) + { + await _connection.ExecuteAsync("INSERT INTO ShoppingCartItems (ShoppingCartId, ProductId, Quantity, Price) VALUES (@ShoppingCartId, @ProductId, @Quantity, @Price)", + new + { + ShoppingCartId = evnt.ShoppingCartId, + ProductID = evnt.ProductId, + Quantity = evnt.Quantity, + Price = evnt.Price + }, trx); + } + + private async Task QuantityIncremented(QuantityIncremented evnt, IDbTransaction trx) + { + await _connection.ExecuteAsync("UPDATE ShoppingCartItems SET Quantity=Quantity+@Quantity WHERE ShoppingCartId=@ShoppingCartId AND ProductID=@ProductId", + new + { + ShoppingCartId = evnt.ShoppingCartId, + ProductID = evnt.ProductId, + Quantity = evnt.Quantity, + }, trx); + } + + private async Task ItemRemoved(ItemRemoved evnt, IDbTransaction trx) + { + await _connection.ExecuteAsync("DELETE FROM ShoppingCartItems WHERE ShoppingCartId=@ShoppingCartId AND ProductId=@ProductId", + new + { + ShoppingCartId = evnt.ShoppingCartId, + ProductID = evnt.ProductId + }, trx); + } + } +} \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..46aef81 --- /dev/null +++ b/.gitignore @@ -0,0 +1,353 @@ +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. +## +## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore + +# User-specific files +*.rsuser +*.suo +*.user +*.userosscache +*.sln.docstates + +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + +# Mono auto generated files +mono_crash.* + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +x64/ +x86/ +[Aa][Rr][Mm]/ +[Aa][Rr][Mm]64/ +bld/ +[Bb]in/ +[Oo]bj/ +[Ll]og/ +[Ll]ogs/ + +# Visual Studio 2015/2017 cache/options directory +.vs/ +# Uncomment if you have tasks that create the project's static files in wwwroot +#wwwroot/ + +# Visual Studio 2017 auto generated files +Generated\ Files/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +# NUnit +*.VisualState.xml +TestResult.xml +nunit-*.xml + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +# Benchmark Results +BenchmarkDotNet.Artifacts/ + +# .NET Core +project.lock.json +project.fragment.lock.json +artifacts/ + +# StyleCop +StyleCopReport.xml + +# Files built by Visual Studio +*_i.c +*_p.c +*_h.h +*.ilk +*.meta +*.obj +*.iobj +*.pch +*.pdb +*.ipdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*_wpftmp.csproj +*.log +*.vspscc +*.vssscc +.builds +*.pidb +*.svclog +*.scc + +# Chutzpah Test files +_Chutzpah* + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opendb +*.opensdf +*.sdf +*.cachefile +*.VC.db +*.VC.VC.opendb + +# Visual Studio profiler +*.psess +*.vsp +*.vspx +*.sap + +# Visual Studio Trace Files +*.e2e + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# AxoCover is a Code Coverage Tool +.axoCover/* +!.axoCover/settings.json + +# Visual Studio code coverage results +*.coverage +*.coveragexml + +# NCrunch +_NCrunch_* +.*crunch*.local.xml +nCrunchTemp_* + +# MightyMoose +*.mm.* +AutoTest.Net/ + +# Web workbench (sass) +.sass-cache/ + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.[Pp]ublish.xml +*.azurePubxml +# Note: Comment the next line if you want to checkin your web deploy settings, +# but database connection strings (with potential passwords) will be unencrypted +*.pubxml +*.publishproj + +# Microsoft Azure Web App publish settings. Comment the next line if you want to +# checkin your Azure Web App publish settings, but sensitive information contained +# in these scripts will be unencrypted +PublishScripts/ + +# NuGet Packages +*.nupkg +# NuGet Symbol Packages +*.snupkg +# The packages folder can be ignored because of Package Restore +**/[Pp]ackages/* +# except build/, which is used as an MSBuild target. +!**/[Pp]ackages/build/ +# Uncomment if necessary however generally it will be regenerated when needed +#!**/[Pp]ackages/repositories.config +# NuGet v3's project.json files produces more ignorable files +*.nuget.props +*.nuget.targets + +# Microsoft Azure Build Output +csx/ +*.build.csdef + +# Microsoft Azure Emulator +ecf/ +rcf/ + +# Windows Store app package directories and files +AppPackages/ +BundleArtifacts/ +Package.StoreAssociation.xml +_pkginfo.txt +*.appx +*.appxbundle +*.appxupload + +# Visual Studio cache files +# files ending in .cache can be ignored +*.[Cc]ache +# but keep track of directories ending in .cache +!?*.[Cc]ache/ + +# Others +ClientBin/ +~$* +*~ +*.dbmdl +*.dbproj.schemaview +*.jfm +*.pfx +*.publishsettings +orleans.codegen.cs + +# Including strong name files can present a security risk +# (https://github.com/github/gitignore/pull/2483#issue-259490424) +#*.snk + +# Since there are multiple workflows, uncomment next line to ignore bower_components +# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) +#bower_components/ + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file +# to a newer Visual Studio version. Backup files are not needed, +# because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm +ServiceFabricBackup/ +*.rptproj.bak + +# SQL Server files +*.mdf +*.ldf +*.ndf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings +*.rptproj.rsuser +*- [Bb]ackup.rdl +*- [Bb]ackup ([0-9]).rdl +*- [Bb]ackup ([0-9][0-9]).rdl + +# Microsoft Fakes +FakesAssemblies/ + +# GhostDoc plugin setting file +*.GhostDoc.xml + +# Node.js Tools for Visual Studio +.ntvs_analysis.dat +node_modules/ + +# Visual Studio 6 build log +*.plg + +# Visual Studio 6 workspace options file +*.opt + +# Visual Studio 6 auto-generated workspace file (contains which files were open etc.) +*.vbw + +# Visual Studio LightSwitch build output +**/*.HTMLClient/GeneratedArtifacts +**/*.DesktopClient/GeneratedArtifacts +**/*.DesktopClient/ModelManifest.xml +**/*.Server/GeneratedArtifacts +**/*.Server/ModelManifest.xml +_Pvt_Extensions + +# Paket dependency manager +.paket/paket.exe +paket-files/ + +# FAKE - F# Make +.fake/ + +# CodeRush personal settings +.cr/personal + +# Python Tools for Visual Studio (PTVS) +__pycache__/ +*.pyc + +# Cake - Uncomment if you are using it +# tools/** +# !tools/packages.config + +# Tabs Studio +*.tss + +# Telerik's JustMock configuration file +*.jmconfig + +# BizTalk build output +*.btp.cs +*.btm.cs +*.odx.cs +*.xsd.cs + +# OpenCover UI analysis results +OpenCover/ + +# Azure Stream Analytics local run output +ASALocalRun/ + +# MSBuild Binary and Structured Log +*.binlog + +# NVidia Nsight GPU debugger configuration file +*.nvuser + +# MFractors (Xamarin productivity tool) working folder +.mfractor/ + +# Local History for Visual Studio +.localhistory/ + +# BeatPulse healthcheck temp database +healthchecksdb + +# Backup folder for Package Reference Convert tool in Visual Studio 2017 +MigrationBackup/ + +# Ionide (cross platform F# VS Code tools) working folder +.ionide/ + +# JetBrains Rider +.idea/ diff --git a/src/AggregatePersistence.sln b/src/AggregatePersistence.sln new file mode 100644 index 0000000..66a8230 --- /dev/null +++ b/src/AggregatePersistence.sln @@ -0,0 +1,39 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.26124.0 +MinimumVisualStudioVersion = 15.0.26124.0 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sales", "Sales\Sales.csproj", "{018A96A1-C587-43C6-BB9C-12AB654F6CC6}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Sales", "Sales", "{7D57F573-B99C-45B1-A5C3-5710F7E4083B}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|Any CPU = Release|Any CPU + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {018A96A1-C587-43C6-BB9C-12AB654F6CC6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {018A96A1-C587-43C6-BB9C-12AB654F6CC6}.Debug|Any CPU.Build.0 = Debug|Any CPU + {018A96A1-C587-43C6-BB9C-12AB654F6CC6}.Debug|x64.ActiveCfg = Debug|Any CPU + {018A96A1-C587-43C6-BB9C-12AB654F6CC6}.Debug|x64.Build.0 = Debug|Any CPU + {018A96A1-C587-43C6-BB9C-12AB654F6CC6}.Debug|x86.ActiveCfg = Debug|Any CPU + {018A96A1-C587-43C6-BB9C-12AB654F6CC6}.Debug|x86.Build.0 = Debug|Any CPU + {018A96A1-C587-43C6-BB9C-12AB654F6CC6}.Release|Any CPU.ActiveCfg = Release|Any CPU + {018A96A1-C587-43C6-BB9C-12AB654F6CC6}.Release|Any CPU.Build.0 = Release|Any CPU + {018A96A1-C587-43C6-BB9C-12AB654F6CC6}.Release|x64.ActiveCfg = Release|Any CPU + {018A96A1-C587-43C6-BB9C-12AB654F6CC6}.Release|x64.Build.0 = Release|Any CPU + {018A96A1-C587-43C6-BB9C-12AB654F6CC6}.Release|x86.ActiveCfg = Release|Any CPU + {018A96A1-C587-43C6-BB9C-12AB654F6CC6}.Release|x86.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(NestedProjects) = preSolution + {018A96A1-C587-43C6-BB9C-12AB654F6CC6} = {7D57F573-B99C-45B1-A5C3-5710F7E4083B} + EndGlobalSection +EndGlobal diff --git a/src/Sales/AggregateExamples/Domain.cs b/src/Sales/AggregateExamples/Domain.cs new file mode 100644 index 0000000..2862f86 --- /dev/null +++ b/src/Sales/AggregateExamples/Domain.cs @@ -0,0 +1,93 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore; + +namespace Sales.AggregateExamples.Domain +{ + public class ShoppingCart + { + public ShoppingCart(Guid shoppingCartId, Guid customerId) + { + ShoppingCartId = shoppingCartId; + CustomerId = customerId; + } + + public Guid ShoppingCartId { get; private set; } + public Guid CustomerId { get; private set; } + public IList Items { get; set; } = new List(); + } + + public class ShoppingCartItem + { + public ShoppingCartItem(Guid shoppingCartId, Guid productId, int quantity, decimal price) + { + ShoppingCartId = shoppingCartId; + ProductId = productId; + Quantity = quantity; + Price = price; + } + + public Guid ShoppingCartId { get; set; } + public Guid ProductId { get; private set; } + public int Quantity { get; set; } + public decimal Price { get; private set; } + } + + public class ShoppingCartDomain + { + private readonly ShoppingCart _shoppingCart; + + public ShoppingCartDomain(ShoppingCart shoppingCart) + { + _shoppingCart = shoppingCart; + } + + public void AddItem(Guid productId, int quantity, decimal price) + { + var existingItem = _shoppingCart.Items.SingleOrDefault(x => x.ProductId == productId); + if (existingItem != null) + { + existingItem.Quantity += quantity; + } + else + { + _shoppingCart.Items.Add(new ShoppingCartItem(_shoppingCart.ShoppingCartId, productId, quantity, price)); + } + } + + public void RemoveItem(Guid productId) + { + var product = _shoppingCart.Items.SingleOrDefault(x => x.ProductId == productId); + if (product != null) + { + _shoppingCart.Items.Remove(product); + } + } + } + + public class ShoppingCartDomainRepository + { + private readonly SalesDbContext _dbContext; + + public ShoppingCartDomainRepository(SalesDbContext dbContext) + { + _dbContext = dbContext; + } + + public async Task GetShoppingCart(Guid shoppingCartId) + { + var data = await _dbContext.ShoppingCarts + .Include(x => x.Items) + .SingleAsync(x => x.ShoppingCartId == shoppingCartId); + + return new ShoppingCartDomain(data); + } + + public async Task Save() + { + await _dbContext.SaveChangesAsync(); + } + } +} \ No newline at end of file diff --git a/src/Sales/AggregateExamples/Entity.cs b/src/Sales/AggregateExamples/Entity.cs new file mode 100644 index 0000000..f391701 --- /dev/null +++ b/src/Sales/AggregateExamples/Entity.cs @@ -0,0 +1,31 @@ +using System; +using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore; +using Sales.AggregateExamples.Domain; + +namespace Sales.AggregateExamples.Entity +{ + public class ShoppingCartRepository + { + private readonly SalesDbContext _dbContext; + + public ShoppingCartRepository(SalesDbContext dbContext) + { + _dbContext = dbContext; + } + + public async Task GetShoppingCart(Guid shoppingCartId) + { + return await _dbContext.ShoppingCarts + .Include(x => x.Items) + .SingleAsync(x => x.ShoppingCartId == shoppingCartId); + } + + public async Task Save() + { + await _dbContext.SaveChangesAsync(); + } + } + + +} \ No newline at end of file diff --git a/src/Sales/AggregateExamples/Events.cs b/src/Sales/AggregateExamples/Events.cs new file mode 100644 index 0000000..f974e80 --- /dev/null +++ b/src/Sales/AggregateExamples/Events.cs @@ -0,0 +1,189 @@ +using System; +using System.Collections.Generic; +using System.Data; +using System.Linq; +using System.Threading.Tasks; +using Dapper; + +namespace Sales.AggregateExamples.Events +{ + public class ShoppingCart + { + public ShoppingCart(Guid shoppingCartId, Guid customerId) + { + ShoppingCartId = shoppingCartId; + CustomerId = customerId; + } + + public Guid ShoppingCartId { get; private set; } + public Guid CustomerId { get; private set; } + public IList Items { get; set; } = new List(); + } + + public class ShoppingCartItem + { + public ShoppingCartItem(Guid shoppingCartId, Guid productId) + { + ShoppingCartId = shoppingCartId; + ProductId = productId; + } + + public Guid ShoppingCartId { get; set; } + public Guid ProductId { get; set; } + } + + + + public class ShoppingCartEventDomain + { + private List _events = new List(); + private readonly ShoppingCart _shoppingCart; + + public ShoppingCartEventDomain(ShoppingCart shoppingCart) + { + _shoppingCart = shoppingCart; + } + + public List GetEvents() + { + return _events; + } + + public void AddItem(Guid productId, int quantity, decimal price) + { + var existingItem = _shoppingCart.Items.SingleOrDefault(x => x.ProductId == productId); + if (existingItem != null) + { + _events.Add(new QuantityIncremented + { + ShoppingCartId = _shoppingCart.ShoppingCartId, + ProductId = productId, + Quantity = quantity + }); + } + else + { + _shoppingCart.Items.Add(new ShoppingCartItem(_shoppingCart.ShoppingCartId, productId)); + + _events.Add(new ItemAdded + { + ShoppingCartId = _shoppingCart.ShoppingCartId, + ProductId = productId, + Quantity = quantity, + Price = price + }); + } + } + + public void RemoveItem(Guid productId) + { + var product = _shoppingCart.Items.SingleOrDefault(x => x.ProductId == productId); + if (product != null) + { + _events.Add(new ItemRemoved + { + ShoppingCartId = _shoppingCart.ShoppingCartId, + ProductId = productId, + }); + } + } + } + + public class ItemAdded + { + public Guid ShoppingCartId { get; set; } + public Guid ProductId { get; set; } + public int Quantity { get; set; } + public decimal Price { get; set; } + } + + public class ItemRemoved + { + public Guid ShoppingCartId { get; set; } + public Guid ProductId { get; set; } + } + + public class QuantityIncremented + { + public Guid ShoppingCartId { get; set; } + public Guid ProductId { get; set; } + public int Quantity { get; set; } + } + + public class ShoppingCartRepository + { + private readonly IDbConnection _connection; + + public ShoppingCartRepository(IDbConnection connection) + { + _connection = connection; + } + + public async Task GetShoppingCart(Guid shoppingCartId) + { + var shoppingCart = await _connection.QueryFirstAsync("SELECT CustomerId FROM ShoppingCarts WHERE ShoppingCartId=@ShopingCartId", + new {ShoppingCartId = shoppingCartId}); + + var items = await _connection.QueryAsync("SELECT ProductId, Quantity FROM ShoppingCartItems WHERE ShoppingCartId=@ShopingCartId", + new {ShoppingCartId = shoppingCartId}); + shoppingCart.Items = items.ToList(); + + return new ShoppingCartEventDomain(shoppingCart); + } + + public async Task Save(ShoppingCartEventDomain shoppingCart) + { + var trx = _connection.BeginTransaction(); + var evnts = shoppingCart.GetEvents(); + foreach (var evnt in evnts) + { + if (evnt is ItemAdded itemAdded) + { + await ItemAdded(itemAdded, trx); + } + else if (evnt is QuantityIncremented quantityIncremented) + { + await QuantityIncremented(quantityIncremented, trx); + } + else if (evnt is ItemRemoved itemRemoved) + { + await ItemRemoved(itemRemoved, trx); + } + } + trx.Commit(); + } + + private async Task ItemAdded(ItemAdded evnt, IDbTransaction trx) + { + await _connection.ExecuteAsync("INSERT INTO ShoppingCartItems (ShoppingCartId, ProductId, Quantity, Price) VALUES (@ShoppingCartId, @ProductId, @Quantity, @Price)", + new + { + ShoppingCartId = evnt.ShoppingCartId, + ProductID = evnt.ProductId, + Quantity = evnt.Quantity, + Price = evnt.Price + }, trx); + } + + private async Task QuantityIncremented(QuantityIncremented evnt, IDbTransaction trx) + { + await _connection.ExecuteAsync("UPDATE ShoppingCartItems SET Quantity=Quantity+@Quantity WHERE ShoppingCartId=@ShoppingCartId AND ProductID=@ProductId", + new + { + ShoppingCartId = evnt.ShoppingCartId, + ProductID = evnt.ProductId, + Quantity = evnt.Quantity, + }, trx); + } + + private async Task ItemRemoved(ItemRemoved evnt, IDbTransaction trx) + { + await _connection.ExecuteAsync("DELETE FROM ShoppingCartItems WHERE ShoppingCartId=@ShoppingCartId AND ProductId=@ProductId", + new + { + ShoppingCartId = evnt.ShoppingCartId, + ProductID = evnt.ProductId + }, trx); + } + } +} \ No newline at end of file diff --git a/src/Sales/Sales.csproj b/src/Sales/Sales.csproj new file mode 100644 index 0000000..ac66408 --- /dev/null +++ b/src/Sales/Sales.csproj @@ -0,0 +1,31 @@ + + + + netcoreapp3.1 + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..46aef81 --- /dev/null +++ b/.gitignore @@ -0,0 +1,353 @@ +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. +## +## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore + +# User-specific files +*.rsuser +*.suo +*.user +*.userosscache +*.sln.docstates + +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + +# Mono auto generated files +mono_crash.* + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +x64/ +x86/ +[Aa][Rr][Mm]/ +[Aa][Rr][Mm]64/ +bld/ +[Bb]in/ +[Oo]bj/ +[Ll]og/ +[Ll]ogs/ + +# Visual Studio 2015/2017 cache/options directory +.vs/ +# Uncomment if you have tasks that create the project's static files in wwwroot +#wwwroot/ + +# Visual Studio 2017 auto generated files +Generated\ Files/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +# NUnit +*.VisualState.xml +TestResult.xml +nunit-*.xml + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +# Benchmark Results +BenchmarkDotNet.Artifacts/ + +# .NET Core +project.lock.json +project.fragment.lock.json +artifacts/ + +# StyleCop +StyleCopReport.xml + +# Files built by Visual Studio +*_i.c +*_p.c +*_h.h +*.ilk +*.meta +*.obj +*.iobj +*.pch +*.pdb +*.ipdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*_wpftmp.csproj +*.log +*.vspscc +*.vssscc +.builds +*.pidb +*.svclog +*.scc + +# Chutzpah Test files +_Chutzpah* + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opendb +*.opensdf +*.sdf +*.cachefile +*.VC.db +*.VC.VC.opendb + +# Visual Studio profiler +*.psess +*.vsp +*.vspx +*.sap + +# Visual Studio Trace Files +*.e2e + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# AxoCover is a Code Coverage Tool +.axoCover/* +!.axoCover/settings.json + +# Visual Studio code coverage results +*.coverage +*.coveragexml + +# NCrunch +_NCrunch_* +.*crunch*.local.xml +nCrunchTemp_* + +# MightyMoose +*.mm.* +AutoTest.Net/ + +# Web workbench (sass) +.sass-cache/ + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.[Pp]ublish.xml +*.azurePubxml +# Note: Comment the next line if you want to checkin your web deploy settings, +# but database connection strings (with potential passwords) will be unencrypted +*.pubxml +*.publishproj + +# Microsoft Azure Web App publish settings. Comment the next line if you want to +# checkin your Azure Web App publish settings, but sensitive information contained +# in these scripts will be unencrypted +PublishScripts/ + +# NuGet Packages +*.nupkg +# NuGet Symbol Packages +*.snupkg +# The packages folder can be ignored because of Package Restore +**/[Pp]ackages/* +# except build/, which is used as an MSBuild target. +!**/[Pp]ackages/build/ +# Uncomment if necessary however generally it will be regenerated when needed +#!**/[Pp]ackages/repositories.config +# NuGet v3's project.json files produces more ignorable files +*.nuget.props +*.nuget.targets + +# Microsoft Azure Build Output +csx/ +*.build.csdef + +# Microsoft Azure Emulator +ecf/ +rcf/ + +# Windows Store app package directories and files +AppPackages/ +BundleArtifacts/ +Package.StoreAssociation.xml +_pkginfo.txt +*.appx +*.appxbundle +*.appxupload + +# Visual Studio cache files +# files ending in .cache can be ignored +*.[Cc]ache +# but keep track of directories ending in .cache +!?*.[Cc]ache/ + +# Others +ClientBin/ +~$* +*~ +*.dbmdl +*.dbproj.schemaview +*.jfm +*.pfx +*.publishsettings +orleans.codegen.cs + +# Including strong name files can present a security risk +# (https://github.com/github/gitignore/pull/2483#issue-259490424) +#*.snk + +# Since there are multiple workflows, uncomment next line to ignore bower_components +# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) +#bower_components/ + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file +# to a newer Visual Studio version. Backup files are not needed, +# because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm +ServiceFabricBackup/ +*.rptproj.bak + +# SQL Server files +*.mdf +*.ldf +*.ndf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings +*.rptproj.rsuser +*- [Bb]ackup.rdl +*- [Bb]ackup ([0-9]).rdl +*- [Bb]ackup ([0-9][0-9]).rdl + +# Microsoft Fakes +FakesAssemblies/ + +# GhostDoc plugin setting file +*.GhostDoc.xml + +# Node.js Tools for Visual Studio +.ntvs_analysis.dat +node_modules/ + +# Visual Studio 6 build log +*.plg + +# Visual Studio 6 workspace options file +*.opt + +# Visual Studio 6 auto-generated workspace file (contains which files were open etc.) +*.vbw + +# Visual Studio LightSwitch build output +**/*.HTMLClient/GeneratedArtifacts +**/*.DesktopClient/GeneratedArtifacts +**/*.DesktopClient/ModelManifest.xml +**/*.Server/GeneratedArtifacts +**/*.Server/ModelManifest.xml +_Pvt_Extensions + +# Paket dependency manager +.paket/paket.exe +paket-files/ + +# FAKE - F# Make +.fake/ + +# CodeRush personal settings +.cr/personal + +# Python Tools for Visual Studio (PTVS) +__pycache__/ +*.pyc + +# Cake - Uncomment if you are using it +# tools/** +# !tools/packages.config + +# Tabs Studio +*.tss + +# Telerik's JustMock configuration file +*.jmconfig + +# BizTalk build output +*.btp.cs +*.btm.cs +*.odx.cs +*.xsd.cs + +# OpenCover UI analysis results +OpenCover/ + +# Azure Stream Analytics local run output +ASALocalRun/ + +# MSBuild Binary and Structured Log +*.binlog + +# NVidia Nsight GPU debugger configuration file +*.nvuser + +# MFractors (Xamarin productivity tool) working folder +.mfractor/ + +# Local History for Visual Studio +.localhistory/ + +# BeatPulse healthcheck temp database +healthchecksdb + +# Backup folder for Package Reference Convert tool in Visual Studio 2017 +MigrationBackup/ + +# Ionide (cross platform F# VS Code tools) working folder +.ionide/ + +# JetBrains Rider +.idea/ diff --git a/src/AggregatePersistence.sln b/src/AggregatePersistence.sln new file mode 100644 index 0000000..66a8230 --- /dev/null +++ b/src/AggregatePersistence.sln @@ -0,0 +1,39 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.26124.0 +MinimumVisualStudioVersion = 15.0.26124.0 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sales", "Sales\Sales.csproj", "{018A96A1-C587-43C6-BB9C-12AB654F6CC6}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Sales", "Sales", "{7D57F573-B99C-45B1-A5C3-5710F7E4083B}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|Any CPU = Release|Any CPU + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {018A96A1-C587-43C6-BB9C-12AB654F6CC6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {018A96A1-C587-43C6-BB9C-12AB654F6CC6}.Debug|Any CPU.Build.0 = Debug|Any CPU + {018A96A1-C587-43C6-BB9C-12AB654F6CC6}.Debug|x64.ActiveCfg = Debug|Any CPU + {018A96A1-C587-43C6-BB9C-12AB654F6CC6}.Debug|x64.Build.0 = Debug|Any CPU + {018A96A1-C587-43C6-BB9C-12AB654F6CC6}.Debug|x86.ActiveCfg = Debug|Any CPU + {018A96A1-C587-43C6-BB9C-12AB654F6CC6}.Debug|x86.Build.0 = Debug|Any CPU + {018A96A1-C587-43C6-BB9C-12AB654F6CC6}.Release|Any CPU.ActiveCfg = Release|Any CPU + {018A96A1-C587-43C6-BB9C-12AB654F6CC6}.Release|Any CPU.Build.0 = Release|Any CPU + {018A96A1-C587-43C6-BB9C-12AB654F6CC6}.Release|x64.ActiveCfg = Release|Any CPU + {018A96A1-C587-43C6-BB9C-12AB654F6CC6}.Release|x64.Build.0 = Release|Any CPU + {018A96A1-C587-43C6-BB9C-12AB654F6CC6}.Release|x86.ActiveCfg = Release|Any CPU + {018A96A1-C587-43C6-BB9C-12AB654F6CC6}.Release|x86.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(NestedProjects) = preSolution + {018A96A1-C587-43C6-BB9C-12AB654F6CC6} = {7D57F573-B99C-45B1-A5C3-5710F7E4083B} + EndGlobalSection +EndGlobal diff --git a/src/Sales/AggregateExamples/Domain.cs b/src/Sales/AggregateExamples/Domain.cs new file mode 100644 index 0000000..2862f86 --- /dev/null +++ b/src/Sales/AggregateExamples/Domain.cs @@ -0,0 +1,93 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore; + +namespace Sales.AggregateExamples.Domain +{ + public class ShoppingCart + { + public ShoppingCart(Guid shoppingCartId, Guid customerId) + { + ShoppingCartId = shoppingCartId; + CustomerId = customerId; + } + + public Guid ShoppingCartId { get; private set; } + public Guid CustomerId { get; private set; } + public IList Items { get; set; } = new List(); + } + + public class ShoppingCartItem + { + public ShoppingCartItem(Guid shoppingCartId, Guid productId, int quantity, decimal price) + { + ShoppingCartId = shoppingCartId; + ProductId = productId; + Quantity = quantity; + Price = price; + } + + public Guid ShoppingCartId { get; set; } + public Guid ProductId { get; private set; } + public int Quantity { get; set; } + public decimal Price { get; private set; } + } + + public class ShoppingCartDomain + { + private readonly ShoppingCart _shoppingCart; + + public ShoppingCartDomain(ShoppingCart shoppingCart) + { + _shoppingCart = shoppingCart; + } + + public void AddItem(Guid productId, int quantity, decimal price) + { + var existingItem = _shoppingCart.Items.SingleOrDefault(x => x.ProductId == productId); + if (existingItem != null) + { + existingItem.Quantity += quantity; + } + else + { + _shoppingCart.Items.Add(new ShoppingCartItem(_shoppingCart.ShoppingCartId, productId, quantity, price)); + } + } + + public void RemoveItem(Guid productId) + { + var product = _shoppingCart.Items.SingleOrDefault(x => x.ProductId == productId); + if (product != null) + { + _shoppingCart.Items.Remove(product); + } + } + } + + public class ShoppingCartDomainRepository + { + private readonly SalesDbContext _dbContext; + + public ShoppingCartDomainRepository(SalesDbContext dbContext) + { + _dbContext = dbContext; + } + + public async Task GetShoppingCart(Guid shoppingCartId) + { + var data = await _dbContext.ShoppingCarts + .Include(x => x.Items) + .SingleAsync(x => x.ShoppingCartId == shoppingCartId); + + return new ShoppingCartDomain(data); + } + + public async Task Save() + { + await _dbContext.SaveChangesAsync(); + } + } +} \ No newline at end of file diff --git a/src/Sales/AggregateExamples/Entity.cs b/src/Sales/AggregateExamples/Entity.cs new file mode 100644 index 0000000..f391701 --- /dev/null +++ b/src/Sales/AggregateExamples/Entity.cs @@ -0,0 +1,31 @@ +using System; +using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore; +using Sales.AggregateExamples.Domain; + +namespace Sales.AggregateExamples.Entity +{ + public class ShoppingCartRepository + { + private readonly SalesDbContext _dbContext; + + public ShoppingCartRepository(SalesDbContext dbContext) + { + _dbContext = dbContext; + } + + public async Task GetShoppingCart(Guid shoppingCartId) + { + return await _dbContext.ShoppingCarts + .Include(x => x.Items) + .SingleAsync(x => x.ShoppingCartId == shoppingCartId); + } + + public async Task Save() + { + await _dbContext.SaveChangesAsync(); + } + } + + +} \ No newline at end of file diff --git a/src/Sales/AggregateExamples/Events.cs b/src/Sales/AggregateExamples/Events.cs new file mode 100644 index 0000000..f974e80 --- /dev/null +++ b/src/Sales/AggregateExamples/Events.cs @@ -0,0 +1,189 @@ +using System; +using System.Collections.Generic; +using System.Data; +using System.Linq; +using System.Threading.Tasks; +using Dapper; + +namespace Sales.AggregateExamples.Events +{ + public class ShoppingCart + { + public ShoppingCart(Guid shoppingCartId, Guid customerId) + { + ShoppingCartId = shoppingCartId; + CustomerId = customerId; + } + + public Guid ShoppingCartId { get; private set; } + public Guid CustomerId { get; private set; } + public IList Items { get; set; } = new List(); + } + + public class ShoppingCartItem + { + public ShoppingCartItem(Guid shoppingCartId, Guid productId) + { + ShoppingCartId = shoppingCartId; + ProductId = productId; + } + + public Guid ShoppingCartId { get; set; } + public Guid ProductId { get; set; } + } + + + + public class ShoppingCartEventDomain + { + private List _events = new List(); + private readonly ShoppingCart _shoppingCart; + + public ShoppingCartEventDomain(ShoppingCart shoppingCart) + { + _shoppingCart = shoppingCart; + } + + public List GetEvents() + { + return _events; + } + + public void AddItem(Guid productId, int quantity, decimal price) + { + var existingItem = _shoppingCart.Items.SingleOrDefault(x => x.ProductId == productId); + if (existingItem != null) + { + _events.Add(new QuantityIncremented + { + ShoppingCartId = _shoppingCart.ShoppingCartId, + ProductId = productId, + Quantity = quantity + }); + } + else + { + _shoppingCart.Items.Add(new ShoppingCartItem(_shoppingCart.ShoppingCartId, productId)); + + _events.Add(new ItemAdded + { + ShoppingCartId = _shoppingCart.ShoppingCartId, + ProductId = productId, + Quantity = quantity, + Price = price + }); + } + } + + public void RemoveItem(Guid productId) + { + var product = _shoppingCart.Items.SingleOrDefault(x => x.ProductId == productId); + if (product != null) + { + _events.Add(new ItemRemoved + { + ShoppingCartId = _shoppingCart.ShoppingCartId, + ProductId = productId, + }); + } + } + } + + public class ItemAdded + { + public Guid ShoppingCartId { get; set; } + public Guid ProductId { get; set; } + public int Quantity { get; set; } + public decimal Price { get; set; } + } + + public class ItemRemoved + { + public Guid ShoppingCartId { get; set; } + public Guid ProductId { get; set; } + } + + public class QuantityIncremented + { + public Guid ShoppingCartId { get; set; } + public Guid ProductId { get; set; } + public int Quantity { get; set; } + } + + public class ShoppingCartRepository + { + private readonly IDbConnection _connection; + + public ShoppingCartRepository(IDbConnection connection) + { + _connection = connection; + } + + public async Task GetShoppingCart(Guid shoppingCartId) + { + var shoppingCart = await _connection.QueryFirstAsync("SELECT CustomerId FROM ShoppingCarts WHERE ShoppingCartId=@ShopingCartId", + new {ShoppingCartId = shoppingCartId}); + + var items = await _connection.QueryAsync("SELECT ProductId, Quantity FROM ShoppingCartItems WHERE ShoppingCartId=@ShopingCartId", + new {ShoppingCartId = shoppingCartId}); + shoppingCart.Items = items.ToList(); + + return new ShoppingCartEventDomain(shoppingCart); + } + + public async Task Save(ShoppingCartEventDomain shoppingCart) + { + var trx = _connection.BeginTransaction(); + var evnts = shoppingCart.GetEvents(); + foreach (var evnt in evnts) + { + if (evnt is ItemAdded itemAdded) + { + await ItemAdded(itemAdded, trx); + } + else if (evnt is QuantityIncremented quantityIncremented) + { + await QuantityIncremented(quantityIncremented, trx); + } + else if (evnt is ItemRemoved itemRemoved) + { + await ItemRemoved(itemRemoved, trx); + } + } + trx.Commit(); + } + + private async Task ItemAdded(ItemAdded evnt, IDbTransaction trx) + { + await _connection.ExecuteAsync("INSERT INTO ShoppingCartItems (ShoppingCartId, ProductId, Quantity, Price) VALUES (@ShoppingCartId, @ProductId, @Quantity, @Price)", + new + { + ShoppingCartId = evnt.ShoppingCartId, + ProductID = evnt.ProductId, + Quantity = evnt.Quantity, + Price = evnt.Price + }, trx); + } + + private async Task QuantityIncremented(QuantityIncremented evnt, IDbTransaction trx) + { + await _connection.ExecuteAsync("UPDATE ShoppingCartItems SET Quantity=Quantity+@Quantity WHERE ShoppingCartId=@ShoppingCartId AND ProductID=@ProductId", + new + { + ShoppingCartId = evnt.ShoppingCartId, + ProductID = evnt.ProductId, + Quantity = evnt.Quantity, + }, trx); + } + + private async Task ItemRemoved(ItemRemoved evnt, IDbTransaction trx) + { + await _connection.ExecuteAsync("DELETE FROM ShoppingCartItems WHERE ShoppingCartId=@ShoppingCartId AND ProductId=@ProductId", + new + { + ShoppingCartId = evnt.ShoppingCartId, + ProductID = evnt.ProductId + }, trx); + } + } +} \ No newline at end of file diff --git a/src/Sales/Sales.csproj b/src/Sales/Sales.csproj new file mode 100644 index 0000000..ac66408 --- /dev/null +++ b/src/Sales/Sales.csproj @@ -0,0 +1,31 @@ + + + + netcoreapp3.1 + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/Sales/SalesDbContext.cs b/src/Sales/SalesDbContext.cs new file mode 100644 index 0000000..c0becc0 --- /dev/null +++ b/src/Sales/SalesDbContext.cs @@ -0,0 +1,24 @@ +using Microsoft.EntityFrameworkCore; +using Sales.AggregateExamples.Domain; + +namespace Sales +{ + public class SalesDbContext : DbContext + { + public DbSet ShoppingCarts { get; set; } + + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + optionsBuilder.UseInMemoryDatabase("LooselyCoupledMonolith_Sales"); + base.OnConfiguring(optionsBuilder); + } + + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + modelBuilder.Entity().HasKey(x => x.ShoppingCartId); + modelBuilder.Entity().HasKey(x => new {x.ShoppingCartId, x.ProductId}); + base.OnModelCreating(modelBuilder); + + } + } +} \ No newline at end of file