diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0c986da --- /dev/null +++ b/.gitignore @@ -0,0 +1,258 @@ +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. + +# User-specific files +*.suo +*.user +*.userosscache +*.sln.docstates + +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +x64/ +x86/ +bld/ +[Bb]in/ +[Oo]bj/ +[Ll]og/ + +# Visual Studio 2015 cache/options directory +.vs/ +# Uncomment if you have tasks that create the project's static files in wwwroot +**/wwwroot/lib/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +# NUNIT +*.VisualState.xml +TestResult.xml + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +# DNX +project.lock.json +artifacts/ + +*_i.c +*_p.c +*_i.h +*.ilk +*.meta +*.obj +*.pch +*.pdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*.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 + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# JustCode is a .NET coding add-in +.JustCode + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# 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 +# 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 +# The packages folder can be ignored because of Package Restore +**/packages/* +# except build/, which is used as an MSBuild target. +!**/packages/build/ +# Uncomment if necessary however generally it will be regenerated when needed +#!**/packages/repositories.config +# NuGet v3's project.json files produces more ignoreable 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 + +# 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 +*.pfx +*.publishsettings +node_modules/ +orleans.codegen.cs + +# 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 + +# SQL Server files +*.mdf +*.ldf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings + +# Microsoft Fakes +FakesAssemblies/ + +# GhostDoc plugin setting file +*.GhostDoc.xml + +# Node.js Tools for Visual Studio +.ntvs_analysis.dat + +# Visual Studio 6 build log +*.plg + +# Visual Studio 6 workspace options file +*.opt + +# 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/ + +# JetBrains Rider +.idea/ +*.sln.iml +pub/ +/src/Web/WebMVC/Properties/PublishProfiles/eShopOnContainersWebMVC2016 - Web Deploy-publish.ps1 +/src/Web/WebMVC/Properties/PublishProfiles/publish-module.psm1 +/src/Services/Identity/eShopOnContainers.Identity/Properties/launchSettings.json + +#Ignore marker-file used to know which docker files we have. +.eshopdocker_* diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0c986da --- /dev/null +++ b/.gitignore @@ -0,0 +1,258 @@ +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. + +# User-specific files +*.suo +*.user +*.userosscache +*.sln.docstates + +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +x64/ +x86/ +bld/ +[Bb]in/ +[Oo]bj/ +[Ll]og/ + +# Visual Studio 2015 cache/options directory +.vs/ +# Uncomment if you have tasks that create the project's static files in wwwroot +**/wwwroot/lib/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +# NUNIT +*.VisualState.xml +TestResult.xml + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +# DNX +project.lock.json +artifacts/ + +*_i.c +*_p.c +*_i.h +*.ilk +*.meta +*.obj +*.pch +*.pdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*.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 + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# JustCode is a .NET coding add-in +.JustCode + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# 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 +# 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 +# The packages folder can be ignored because of Package Restore +**/packages/* +# except build/, which is used as an MSBuild target. +!**/packages/build/ +# Uncomment if necessary however generally it will be regenerated when needed +#!**/packages/repositories.config +# NuGet v3's project.json files produces more ignoreable 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 + +# 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 +*.pfx +*.publishsettings +node_modules/ +orleans.codegen.cs + +# 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 + +# SQL Server files +*.mdf +*.ldf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings + +# Microsoft Fakes +FakesAssemblies/ + +# GhostDoc plugin setting file +*.GhostDoc.xml + +# Node.js Tools for Visual Studio +.ntvs_analysis.dat + +# Visual Studio 6 build log +*.plg + +# Visual Studio 6 workspace options file +*.opt + +# 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/ + +# JetBrains Rider +.idea/ +*.sln.iml +pub/ +/src/Web/WebMVC/Properties/PublishProfiles/eShopOnContainersWebMVC2016 - Web Deploy-publish.ps1 +/src/Web/WebMVC/Properties/PublishProfiles/publish-module.psm1 +/src/Services/Identity/eShopOnContainers.Identity/Properties/launchSettings.json + +#Ignore marker-file used to know which docker files we have. +.eshopdocker_* diff --git a/SalesOrderAggregateRoot.cs b/SalesOrderAggregateRoot.cs new file mode 100644 index 0000000..c01b961 --- /dev/null +++ b/SalesOrderAggregateRoot.cs @@ -0,0 +1,29 @@ +namespace AggregateConsistencyBoundary +{ + public class OrderAggregateRoot + { + private readonly Address _restaurant; + private readonly Address _customer; + + public OrderAggregateRoot(Address restaurant, Address customer) + { + _restaurant = restaurant; + _customer = customer; + } + + public void Pay() + { + + } + + public void Cancel() + { + + } + + public ShipmentAggregateRoot Ship() + { + return ShipmentAggregateRoot.Factory(new PickupStop(1, _restaurant), new DeliveryStop(2, _customer)); + } + } +} \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0c986da --- /dev/null +++ b/.gitignore @@ -0,0 +1,258 @@ +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. + +# User-specific files +*.suo +*.user +*.userosscache +*.sln.docstates + +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +x64/ +x86/ +bld/ +[Bb]in/ +[Oo]bj/ +[Ll]og/ + +# Visual Studio 2015 cache/options directory +.vs/ +# Uncomment if you have tasks that create the project's static files in wwwroot +**/wwwroot/lib/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +# NUNIT +*.VisualState.xml +TestResult.xml + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +# DNX +project.lock.json +artifacts/ + +*_i.c +*_p.c +*_i.h +*.ilk +*.meta +*.obj +*.pch +*.pdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*.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 + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# JustCode is a .NET coding add-in +.JustCode + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# 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 +# 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 +# The packages folder can be ignored because of Package Restore +**/packages/* +# except build/, which is used as an MSBuild target. +!**/packages/build/ +# Uncomment if necessary however generally it will be regenerated when needed +#!**/packages/repositories.config +# NuGet v3's project.json files produces more ignoreable 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 + +# 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 +*.pfx +*.publishsettings +node_modules/ +orleans.codegen.cs + +# 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 + +# SQL Server files +*.mdf +*.ldf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings + +# Microsoft Fakes +FakesAssemblies/ + +# GhostDoc plugin setting file +*.GhostDoc.xml + +# Node.js Tools for Visual Studio +.ntvs_analysis.dat + +# Visual Studio 6 build log +*.plg + +# Visual Studio 6 workspace options file +*.opt + +# 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/ + +# JetBrains Rider +.idea/ +*.sln.iml +pub/ +/src/Web/WebMVC/Properties/PublishProfiles/eShopOnContainersWebMVC2016 - Web Deploy-publish.ps1 +/src/Web/WebMVC/Properties/PublishProfiles/publish-module.psm1 +/src/Services/Identity/eShopOnContainers.Identity/Properties/launchSettings.json + +#Ignore marker-file used to know which docker files we have. +.eshopdocker_* diff --git a/SalesOrderAggregateRoot.cs b/SalesOrderAggregateRoot.cs new file mode 100644 index 0000000..c01b961 --- /dev/null +++ b/SalesOrderAggregateRoot.cs @@ -0,0 +1,29 @@ +namespace AggregateConsistencyBoundary +{ + public class OrderAggregateRoot + { + private readonly Address _restaurant; + private readonly Address _customer; + + public OrderAggregateRoot(Address restaurant, Address customer) + { + _restaurant = restaurant; + _customer = customer; + } + + public void Pay() + { + + } + + public void Cancel() + { + + } + + public ShipmentAggregateRoot Ship() + { + return ShipmentAggregateRoot.Factory(new PickupStop(1, _restaurant), new DeliveryStop(2, _customer)); + } + } +} \ No newline at end of file diff --git a/ShipmentAggregateRoot.cs b/ShipmentAggregateRoot.cs new file mode 100644 index 0000000..595b96c --- /dev/null +++ b/ShipmentAggregateRoot.cs @@ -0,0 +1,100 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace AggregateConsistencyBoundary +{ + public class ShipmentAggregateRoot + { + private SortedList Stops { get; } = new(); + + private ShipmentAggregateRoot(IReadOnlyList stops) + { + for(var x = 0; x < stops.Count; x++) + { + Stops.Add(x, stops[x]); + } + } + + public static ShipmentAggregateRoot Factory(PickupStop pickup, DeliveryStop delivery) + { + return new ShipmentAggregateRoot(new Stop[] { pickup, delivery }); + } + + public static ShipmentAggregateRoot Factory(Stop[] stops) + { + if (stops.Length < 2) + { + throw new InvalidOperationException("Shipment requires at least 2 stops."); + } + + if (stops.First() is not PickupStop) + { + throw new InvalidOperationException("First stop must be a Pickup"); + } + + if (stops.Last() is not DeliveryStop) + { + throw new InvalidOperationException("first stop must be a Pickup"); + } + + return new ShipmentAggregateRoot(stops); + } + + public void Arrive(int stopId) + { + var currentStop = Stops.SingleOrDefault(x => x.Value.StopId == stopId); + if (currentStop.Value == null) + { + throw new InvalidOperationException("Stop does not exist."); + } + + var previousStopsAreNotDeparted = Stops.Any(x => x.Key < currentStop.Key && x.Value.Status != StopStatus.Departed); + if (previousStopsAreNotDeparted) + { + throw new InvalidOperationException("Previous stops have not departed."); + } + + currentStop.Value.Arrive(); + } + + public void Pickup(int stopId) + { + var currentStop = Stops.SingleOrDefault(x => x.Value.StopId == stopId); + if (currentStop.Value == null) + { + throw new InvalidOperationException("Stop does not exist."); + } + + if (currentStop.Value is not PickupStop) + { + throw new InvalidOperationException("Stop is not a pickup."); + } + + currentStop.Value.Depart(); + } + + public void Deliver(int stopId) + { + var currentStop = Stops.SingleOrDefault(x => x.Value.StopId == stopId); + if (currentStop.Value == null) + { + throw new InvalidOperationException("Stop does not exist."); + } + + if (currentStop.Value is not DeliveryStop) + { + throw new InvalidOperationException("Stop is not a delivery."); + } + + currentStop.Value.Depart(); + } + + public bool IsComplete() + { + return Stops.All(x => x.Value.Status == StopStatus.Departed); + } + } + + public record Address(string Street, string City, string Postal, string Country); +} diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0c986da --- /dev/null +++ b/.gitignore @@ -0,0 +1,258 @@ +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. + +# User-specific files +*.suo +*.user +*.userosscache +*.sln.docstates + +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +x64/ +x86/ +bld/ +[Bb]in/ +[Oo]bj/ +[Ll]og/ + +# Visual Studio 2015 cache/options directory +.vs/ +# Uncomment if you have tasks that create the project's static files in wwwroot +**/wwwroot/lib/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +# NUNIT +*.VisualState.xml +TestResult.xml + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +# DNX +project.lock.json +artifacts/ + +*_i.c +*_p.c +*_i.h +*.ilk +*.meta +*.obj +*.pch +*.pdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*.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 + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# JustCode is a .NET coding add-in +.JustCode + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# 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 +# 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 +# The packages folder can be ignored because of Package Restore +**/packages/* +# except build/, which is used as an MSBuild target. +!**/packages/build/ +# Uncomment if necessary however generally it will be regenerated when needed +#!**/packages/repositories.config +# NuGet v3's project.json files produces more ignoreable 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 + +# 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 +*.pfx +*.publishsettings +node_modules/ +orleans.codegen.cs + +# 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 + +# SQL Server files +*.mdf +*.ldf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings + +# Microsoft Fakes +FakesAssemblies/ + +# GhostDoc plugin setting file +*.GhostDoc.xml + +# Node.js Tools for Visual Studio +.ntvs_analysis.dat + +# Visual Studio 6 build log +*.plg + +# Visual Studio 6 workspace options file +*.opt + +# 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/ + +# JetBrains Rider +.idea/ +*.sln.iml +pub/ +/src/Web/WebMVC/Properties/PublishProfiles/eShopOnContainersWebMVC2016 - Web Deploy-publish.ps1 +/src/Web/WebMVC/Properties/PublishProfiles/publish-module.psm1 +/src/Services/Identity/eShopOnContainers.Identity/Properties/launchSettings.json + +#Ignore marker-file used to know which docker files we have. +.eshopdocker_* diff --git a/SalesOrderAggregateRoot.cs b/SalesOrderAggregateRoot.cs new file mode 100644 index 0000000..c01b961 --- /dev/null +++ b/SalesOrderAggregateRoot.cs @@ -0,0 +1,29 @@ +namespace AggregateConsistencyBoundary +{ + public class OrderAggregateRoot + { + private readonly Address _restaurant; + private readonly Address _customer; + + public OrderAggregateRoot(Address restaurant, Address customer) + { + _restaurant = restaurant; + _customer = customer; + } + + public void Pay() + { + + } + + public void Cancel() + { + + } + + public ShipmentAggregateRoot Ship() + { + return ShipmentAggregateRoot.Factory(new PickupStop(1, _restaurant), new DeliveryStop(2, _customer)); + } + } +} \ No newline at end of file diff --git a/ShipmentAggregateRoot.cs b/ShipmentAggregateRoot.cs new file mode 100644 index 0000000..595b96c --- /dev/null +++ b/ShipmentAggregateRoot.cs @@ -0,0 +1,100 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace AggregateConsistencyBoundary +{ + public class ShipmentAggregateRoot + { + private SortedList Stops { get; } = new(); + + private ShipmentAggregateRoot(IReadOnlyList stops) + { + for(var x = 0; x < stops.Count; x++) + { + Stops.Add(x, stops[x]); + } + } + + public static ShipmentAggregateRoot Factory(PickupStop pickup, DeliveryStop delivery) + { + return new ShipmentAggregateRoot(new Stop[] { pickup, delivery }); + } + + public static ShipmentAggregateRoot Factory(Stop[] stops) + { + if (stops.Length < 2) + { + throw new InvalidOperationException("Shipment requires at least 2 stops."); + } + + if (stops.First() is not PickupStop) + { + throw new InvalidOperationException("First stop must be a Pickup"); + } + + if (stops.Last() is not DeliveryStop) + { + throw new InvalidOperationException("first stop must be a Pickup"); + } + + return new ShipmentAggregateRoot(stops); + } + + public void Arrive(int stopId) + { + var currentStop = Stops.SingleOrDefault(x => x.Value.StopId == stopId); + if (currentStop.Value == null) + { + throw new InvalidOperationException("Stop does not exist."); + } + + var previousStopsAreNotDeparted = Stops.Any(x => x.Key < currentStop.Key && x.Value.Status != StopStatus.Departed); + if (previousStopsAreNotDeparted) + { + throw new InvalidOperationException("Previous stops have not departed."); + } + + currentStop.Value.Arrive(); + } + + public void Pickup(int stopId) + { + var currentStop = Stops.SingleOrDefault(x => x.Value.StopId == stopId); + if (currentStop.Value == null) + { + throw new InvalidOperationException("Stop does not exist."); + } + + if (currentStop.Value is not PickupStop) + { + throw new InvalidOperationException("Stop is not a pickup."); + } + + currentStop.Value.Depart(); + } + + public void Deliver(int stopId) + { + var currentStop = Stops.SingleOrDefault(x => x.Value.StopId == stopId); + if (currentStop.Value == null) + { + throw new InvalidOperationException("Stop does not exist."); + } + + if (currentStop.Value is not DeliveryStop) + { + throw new InvalidOperationException("Stop is not a delivery."); + } + + currentStop.Value.Depart(); + } + + public bool IsComplete() + { + return Stops.All(x => x.Value.Status == StopStatus.Departed); + } + } + + public record Address(string Street, string City, string Postal, string Country); +} diff --git a/Stop.cs b/Stop.cs new file mode 100644 index 0000000..2451e0a --- /dev/null +++ b/Stop.cs @@ -0,0 +1,64 @@ +using System; + +namespace AggregateConsistencyBoundary +{ + public class PickupStop : Stop + { + public PickupStop(int stopId, Address address) + { + StopId = stopId; + Address = address; + } + } + + public class DeliveryStop : Stop + { + public DeliveryStop(int stopId, Address address) + { + StopId = stopId; + Address = address; + } + } + + public abstract class Stop + { + public int StopId { get; protected set; } + public StopStatus Status { get; private set; } = StopStatus.InTransit; + public Address Address { get; protected set;} + public DateTime Scheduled { get; protected set;} + public DateTime? Departed { get; protected set; } + + public void Arrive() + { + if (Status != StopStatus.InTransit) + { + throw new InvalidOperationException("Stop has already arrived."); + } + + Status = StopStatus.Arrived; + } + + public void Depart() + { + if (Status == StopStatus.Departed) + { + throw new InvalidOperationException("Stop has already departed."); + } + + if (Status == StopStatus.InTransit) + { + throw new InvalidOperationException("Stop hasn't arrived yet."); + } + + Status = StopStatus.Departed; + Departed = DateTime.UtcNow; + } + } + + public enum StopStatus + { + InTransit, + Arrived, + Departed + } +} \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0c986da --- /dev/null +++ b/.gitignore @@ -0,0 +1,258 @@ +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. + +# User-specific files +*.suo +*.user +*.userosscache +*.sln.docstates + +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +x64/ +x86/ +bld/ +[Bb]in/ +[Oo]bj/ +[Ll]og/ + +# Visual Studio 2015 cache/options directory +.vs/ +# Uncomment if you have tasks that create the project's static files in wwwroot +**/wwwroot/lib/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +# NUNIT +*.VisualState.xml +TestResult.xml + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +# DNX +project.lock.json +artifacts/ + +*_i.c +*_p.c +*_i.h +*.ilk +*.meta +*.obj +*.pch +*.pdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*.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 + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# JustCode is a .NET coding add-in +.JustCode + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# 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 +# 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 +# The packages folder can be ignored because of Package Restore +**/packages/* +# except build/, which is used as an MSBuild target. +!**/packages/build/ +# Uncomment if necessary however generally it will be regenerated when needed +#!**/packages/repositories.config +# NuGet v3's project.json files produces more ignoreable 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 + +# 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 +*.pfx +*.publishsettings +node_modules/ +orleans.codegen.cs + +# 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 + +# SQL Server files +*.mdf +*.ldf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings + +# Microsoft Fakes +FakesAssemblies/ + +# GhostDoc plugin setting file +*.GhostDoc.xml + +# Node.js Tools for Visual Studio +.ntvs_analysis.dat + +# Visual Studio 6 build log +*.plg + +# Visual Studio 6 workspace options file +*.opt + +# 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/ + +# JetBrains Rider +.idea/ +*.sln.iml +pub/ +/src/Web/WebMVC/Properties/PublishProfiles/eShopOnContainersWebMVC2016 - Web Deploy-publish.ps1 +/src/Web/WebMVC/Properties/PublishProfiles/publish-module.psm1 +/src/Services/Identity/eShopOnContainers.Identity/Properties/launchSettings.json + +#Ignore marker-file used to know which docker files we have. +.eshopdocker_* diff --git a/SalesOrderAggregateRoot.cs b/SalesOrderAggregateRoot.cs new file mode 100644 index 0000000..c01b961 --- /dev/null +++ b/SalesOrderAggregateRoot.cs @@ -0,0 +1,29 @@ +namespace AggregateConsistencyBoundary +{ + public class OrderAggregateRoot + { + private readonly Address _restaurant; + private readonly Address _customer; + + public OrderAggregateRoot(Address restaurant, Address customer) + { + _restaurant = restaurant; + _customer = customer; + } + + public void Pay() + { + + } + + public void Cancel() + { + + } + + public ShipmentAggregateRoot Ship() + { + return ShipmentAggregateRoot.Factory(new PickupStop(1, _restaurant), new DeliveryStop(2, _customer)); + } + } +} \ No newline at end of file diff --git a/ShipmentAggregateRoot.cs b/ShipmentAggregateRoot.cs new file mode 100644 index 0000000..595b96c --- /dev/null +++ b/ShipmentAggregateRoot.cs @@ -0,0 +1,100 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace AggregateConsistencyBoundary +{ + public class ShipmentAggregateRoot + { + private SortedList Stops { get; } = new(); + + private ShipmentAggregateRoot(IReadOnlyList stops) + { + for(var x = 0; x < stops.Count; x++) + { + Stops.Add(x, stops[x]); + } + } + + public static ShipmentAggregateRoot Factory(PickupStop pickup, DeliveryStop delivery) + { + return new ShipmentAggregateRoot(new Stop[] { pickup, delivery }); + } + + public static ShipmentAggregateRoot Factory(Stop[] stops) + { + if (stops.Length < 2) + { + throw new InvalidOperationException("Shipment requires at least 2 stops."); + } + + if (stops.First() is not PickupStop) + { + throw new InvalidOperationException("First stop must be a Pickup"); + } + + if (stops.Last() is not DeliveryStop) + { + throw new InvalidOperationException("first stop must be a Pickup"); + } + + return new ShipmentAggregateRoot(stops); + } + + public void Arrive(int stopId) + { + var currentStop = Stops.SingleOrDefault(x => x.Value.StopId == stopId); + if (currentStop.Value == null) + { + throw new InvalidOperationException("Stop does not exist."); + } + + var previousStopsAreNotDeparted = Stops.Any(x => x.Key < currentStop.Key && x.Value.Status != StopStatus.Departed); + if (previousStopsAreNotDeparted) + { + throw new InvalidOperationException("Previous stops have not departed."); + } + + currentStop.Value.Arrive(); + } + + public void Pickup(int stopId) + { + var currentStop = Stops.SingleOrDefault(x => x.Value.StopId == stopId); + if (currentStop.Value == null) + { + throw new InvalidOperationException("Stop does not exist."); + } + + if (currentStop.Value is not PickupStop) + { + throw new InvalidOperationException("Stop is not a pickup."); + } + + currentStop.Value.Depart(); + } + + public void Deliver(int stopId) + { + var currentStop = Stops.SingleOrDefault(x => x.Value.StopId == stopId); + if (currentStop.Value == null) + { + throw new InvalidOperationException("Stop does not exist."); + } + + if (currentStop.Value is not DeliveryStop) + { + throw new InvalidOperationException("Stop is not a delivery."); + } + + currentStop.Value.Depart(); + } + + public bool IsComplete() + { + return Stops.All(x => x.Value.Status == StopStatus.Departed); + } + } + + public record Address(string Street, string City, string Postal, string Country); +} diff --git a/Stop.cs b/Stop.cs new file mode 100644 index 0000000..2451e0a --- /dev/null +++ b/Stop.cs @@ -0,0 +1,64 @@ +using System; + +namespace AggregateConsistencyBoundary +{ + public class PickupStop : Stop + { + public PickupStop(int stopId, Address address) + { + StopId = stopId; + Address = address; + } + } + + public class DeliveryStop : Stop + { + public DeliveryStop(int stopId, Address address) + { + StopId = stopId; + Address = address; + } + } + + public abstract class Stop + { + public int StopId { get; protected set; } + public StopStatus Status { get; private set; } = StopStatus.InTransit; + public Address Address { get; protected set;} + public DateTime Scheduled { get; protected set;} + public DateTime? Departed { get; protected set; } + + public void Arrive() + { + if (Status != StopStatus.InTransit) + { + throw new InvalidOperationException("Stop has already arrived."); + } + + Status = StopStatus.Arrived; + } + + public void Depart() + { + if (Status == StopStatus.Departed) + { + throw new InvalidOperationException("Stop has already departed."); + } + + if (Status == StopStatus.InTransit) + { + throw new InvalidOperationException("Stop hasn't arrived yet."); + } + + Status = StopStatus.Departed; + Departed = DateTime.UtcNow; + } + } + + public enum StopStatus + { + InTransit, + Arrived, + Departed + } +} \ No newline at end of file diff --git a/Tests.cs b/Tests.cs new file mode 100644 index 0000000..3aa766b --- /dev/null +++ b/Tests.cs @@ -0,0 +1,102 @@ +using System; +using Shouldly; +using Xunit; + +namespace AggregateConsistencyBoundary +{ + public class Tests + { + private readonly ShipmentAggregateRoot _shipmentAggregateRoot; + + public Tests() + { + var pickup = new PickupStop(1, new Address("123 Street", "Lakeshore", "N0R1A0", "CA")); + var delivery = new DeliveryStop(2, new Address("456 Ave", "Windsor", "N8N0A1", "CA")); + _shipmentAggregateRoot = ShipmentAggregateRoot.Factory(pickup, delivery); + } + + [Fact] + public void CompleteShipment() + { + _shipmentAggregateRoot.Arrive(1); + _shipmentAggregateRoot.Pickup(1); + _shipmentAggregateRoot.Arrive(2); + _shipmentAggregateRoot.Deliver(2); + _shipmentAggregateRoot.IsComplete().ShouldBeTrue(); + } + + [Fact] + public void CannotPickupWithoutArriving() + { + Should.Throw(() => _shipmentAggregateRoot.Pickup(1), "Stop hasn't arrived yet."); + } + + [Fact] + public void CannotDeliverWithoutArriving() + { + Should.Throw(() => _shipmentAggregateRoot.Deliver(2), "Stop hasn't arrived yet."); + } + + [Fact] + public void CannotPickupAtDelivery() + { + Should.Throw(() => _shipmentAggregateRoot.Pickup(2), "Stop is not a delivery."); + } + + [Fact] + public void CannotDeliverAtPickup() + { + Should.Throw(() => _shipmentAggregateRoot.Deliver(1), "Stop is not a pickup."); + } + + [Fact] + public void ArriveStopDoesNotExist() + { + Should.Throw(() => _shipmentAggregateRoot.Arrive(0), "Stop does not exist."); + } + + [Fact] + public void PickupStopDoesNotExist() + { + Should.Throw(() => _shipmentAggregateRoot.Pickup(0), "Stop does not exist."); + } + + [Fact] + public void DeliverStopDoesNotExist() + { + Should.Throw(() => _shipmentAggregateRoot.Deliver(0), "Stop does not exist."); + } + + [Fact] + public void ArriveNonDepartedStops() + { + _shipmentAggregateRoot.Arrive(1); + Should.Throw(() => _shipmentAggregateRoot.Arrive(2), "Previous stops have not departed."); + } + + [Fact] + public void AlreadyArrived() + { + _shipmentAggregateRoot.Arrive(1); + Should.Throw(() => _shipmentAggregateRoot.Arrive(1), "Stop has already arrived."); + } + + [Fact] + public void AlreadyPickedUp() + { + _shipmentAggregateRoot.Arrive(1); + _shipmentAggregateRoot.Pickup(1); + Should.Throw(() => _shipmentAggregateRoot.Pickup(1), "Stop has already departed."); + } + + [Fact] + public void AlreadyDelivered() + { + _shipmentAggregateRoot.Arrive(1); + _shipmentAggregateRoot.Pickup(1); + _shipmentAggregateRoot.Arrive(2); + _shipmentAggregateRoot.Deliver(2); + Should.Throw(() => _shipmentAggregateRoot.Deliver(2), "Stop has already departed."); + } + } +} \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0c986da --- /dev/null +++ b/.gitignore @@ -0,0 +1,258 @@ +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. + +# User-specific files +*.suo +*.user +*.userosscache +*.sln.docstates + +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +x64/ +x86/ +bld/ +[Bb]in/ +[Oo]bj/ +[Ll]og/ + +# Visual Studio 2015 cache/options directory +.vs/ +# Uncomment if you have tasks that create the project's static files in wwwroot +**/wwwroot/lib/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +# NUNIT +*.VisualState.xml +TestResult.xml + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +# DNX +project.lock.json +artifacts/ + +*_i.c +*_p.c +*_i.h +*.ilk +*.meta +*.obj +*.pch +*.pdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*.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 + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# JustCode is a .NET coding add-in +.JustCode + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# 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 +# 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 +# The packages folder can be ignored because of Package Restore +**/packages/* +# except build/, which is used as an MSBuild target. +!**/packages/build/ +# Uncomment if necessary however generally it will be regenerated when needed +#!**/packages/repositories.config +# NuGet v3's project.json files produces more ignoreable 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 + +# 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 +*.pfx +*.publishsettings +node_modules/ +orleans.codegen.cs + +# 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 + +# SQL Server files +*.mdf +*.ldf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings + +# Microsoft Fakes +FakesAssemblies/ + +# GhostDoc plugin setting file +*.GhostDoc.xml + +# Node.js Tools for Visual Studio +.ntvs_analysis.dat + +# Visual Studio 6 build log +*.plg + +# Visual Studio 6 workspace options file +*.opt + +# 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/ + +# JetBrains Rider +.idea/ +*.sln.iml +pub/ +/src/Web/WebMVC/Properties/PublishProfiles/eShopOnContainersWebMVC2016 - Web Deploy-publish.ps1 +/src/Web/WebMVC/Properties/PublishProfiles/publish-module.psm1 +/src/Services/Identity/eShopOnContainers.Identity/Properties/launchSettings.json + +#Ignore marker-file used to know which docker files we have. +.eshopdocker_* diff --git a/SalesOrderAggregateRoot.cs b/SalesOrderAggregateRoot.cs new file mode 100644 index 0000000..c01b961 --- /dev/null +++ b/SalesOrderAggregateRoot.cs @@ -0,0 +1,29 @@ +namespace AggregateConsistencyBoundary +{ + public class OrderAggregateRoot + { + private readonly Address _restaurant; + private readonly Address _customer; + + public OrderAggregateRoot(Address restaurant, Address customer) + { + _restaurant = restaurant; + _customer = customer; + } + + public void Pay() + { + + } + + public void Cancel() + { + + } + + public ShipmentAggregateRoot Ship() + { + return ShipmentAggregateRoot.Factory(new PickupStop(1, _restaurant), new DeliveryStop(2, _customer)); + } + } +} \ No newline at end of file diff --git a/ShipmentAggregateRoot.cs b/ShipmentAggregateRoot.cs new file mode 100644 index 0000000..595b96c --- /dev/null +++ b/ShipmentAggregateRoot.cs @@ -0,0 +1,100 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace AggregateConsistencyBoundary +{ + public class ShipmentAggregateRoot + { + private SortedList Stops { get; } = new(); + + private ShipmentAggregateRoot(IReadOnlyList stops) + { + for(var x = 0; x < stops.Count; x++) + { + Stops.Add(x, stops[x]); + } + } + + public static ShipmentAggregateRoot Factory(PickupStop pickup, DeliveryStop delivery) + { + return new ShipmentAggregateRoot(new Stop[] { pickup, delivery }); + } + + public static ShipmentAggregateRoot Factory(Stop[] stops) + { + if (stops.Length < 2) + { + throw new InvalidOperationException("Shipment requires at least 2 stops."); + } + + if (stops.First() is not PickupStop) + { + throw new InvalidOperationException("First stop must be a Pickup"); + } + + if (stops.Last() is not DeliveryStop) + { + throw new InvalidOperationException("first stop must be a Pickup"); + } + + return new ShipmentAggregateRoot(stops); + } + + public void Arrive(int stopId) + { + var currentStop = Stops.SingleOrDefault(x => x.Value.StopId == stopId); + if (currentStop.Value == null) + { + throw new InvalidOperationException("Stop does not exist."); + } + + var previousStopsAreNotDeparted = Stops.Any(x => x.Key < currentStop.Key && x.Value.Status != StopStatus.Departed); + if (previousStopsAreNotDeparted) + { + throw new InvalidOperationException("Previous stops have not departed."); + } + + currentStop.Value.Arrive(); + } + + public void Pickup(int stopId) + { + var currentStop = Stops.SingleOrDefault(x => x.Value.StopId == stopId); + if (currentStop.Value == null) + { + throw new InvalidOperationException("Stop does not exist."); + } + + if (currentStop.Value is not PickupStop) + { + throw new InvalidOperationException("Stop is not a pickup."); + } + + currentStop.Value.Depart(); + } + + public void Deliver(int stopId) + { + var currentStop = Stops.SingleOrDefault(x => x.Value.StopId == stopId); + if (currentStop.Value == null) + { + throw new InvalidOperationException("Stop does not exist."); + } + + if (currentStop.Value is not DeliveryStop) + { + throw new InvalidOperationException("Stop is not a delivery."); + } + + currentStop.Value.Depart(); + } + + public bool IsComplete() + { + return Stops.All(x => x.Value.Status == StopStatus.Departed); + } + } + + public record Address(string Street, string City, string Postal, string Country); +} diff --git a/Stop.cs b/Stop.cs new file mode 100644 index 0000000..2451e0a --- /dev/null +++ b/Stop.cs @@ -0,0 +1,64 @@ +using System; + +namespace AggregateConsistencyBoundary +{ + public class PickupStop : Stop + { + public PickupStop(int stopId, Address address) + { + StopId = stopId; + Address = address; + } + } + + public class DeliveryStop : Stop + { + public DeliveryStop(int stopId, Address address) + { + StopId = stopId; + Address = address; + } + } + + public abstract class Stop + { + public int StopId { get; protected set; } + public StopStatus Status { get; private set; } = StopStatus.InTransit; + public Address Address { get; protected set;} + public DateTime Scheduled { get; protected set;} + public DateTime? Departed { get; protected set; } + + public void Arrive() + { + if (Status != StopStatus.InTransit) + { + throw new InvalidOperationException("Stop has already arrived."); + } + + Status = StopStatus.Arrived; + } + + public void Depart() + { + if (Status == StopStatus.Departed) + { + throw new InvalidOperationException("Stop has already departed."); + } + + if (Status == StopStatus.InTransit) + { + throw new InvalidOperationException("Stop hasn't arrived yet."); + } + + Status = StopStatus.Departed; + Departed = DateTime.UtcNow; + } + } + + public enum StopStatus + { + InTransit, + Arrived, + Departed + } +} \ No newline at end of file diff --git a/Tests.cs b/Tests.cs new file mode 100644 index 0000000..3aa766b --- /dev/null +++ b/Tests.cs @@ -0,0 +1,102 @@ +using System; +using Shouldly; +using Xunit; + +namespace AggregateConsistencyBoundary +{ + public class Tests + { + private readonly ShipmentAggregateRoot _shipmentAggregateRoot; + + public Tests() + { + var pickup = new PickupStop(1, new Address("123 Street", "Lakeshore", "N0R1A0", "CA")); + var delivery = new DeliveryStop(2, new Address("456 Ave", "Windsor", "N8N0A1", "CA")); + _shipmentAggregateRoot = ShipmentAggregateRoot.Factory(pickup, delivery); + } + + [Fact] + public void CompleteShipment() + { + _shipmentAggregateRoot.Arrive(1); + _shipmentAggregateRoot.Pickup(1); + _shipmentAggregateRoot.Arrive(2); + _shipmentAggregateRoot.Deliver(2); + _shipmentAggregateRoot.IsComplete().ShouldBeTrue(); + } + + [Fact] + public void CannotPickupWithoutArriving() + { + Should.Throw(() => _shipmentAggregateRoot.Pickup(1), "Stop hasn't arrived yet."); + } + + [Fact] + public void CannotDeliverWithoutArriving() + { + Should.Throw(() => _shipmentAggregateRoot.Deliver(2), "Stop hasn't arrived yet."); + } + + [Fact] + public void CannotPickupAtDelivery() + { + Should.Throw(() => _shipmentAggregateRoot.Pickup(2), "Stop is not a delivery."); + } + + [Fact] + public void CannotDeliverAtPickup() + { + Should.Throw(() => _shipmentAggregateRoot.Deliver(1), "Stop is not a pickup."); + } + + [Fact] + public void ArriveStopDoesNotExist() + { + Should.Throw(() => _shipmentAggregateRoot.Arrive(0), "Stop does not exist."); + } + + [Fact] + public void PickupStopDoesNotExist() + { + Should.Throw(() => _shipmentAggregateRoot.Pickup(0), "Stop does not exist."); + } + + [Fact] + public void DeliverStopDoesNotExist() + { + Should.Throw(() => _shipmentAggregateRoot.Deliver(0), "Stop does not exist."); + } + + [Fact] + public void ArriveNonDepartedStops() + { + _shipmentAggregateRoot.Arrive(1); + Should.Throw(() => _shipmentAggregateRoot.Arrive(2), "Previous stops have not departed."); + } + + [Fact] + public void AlreadyArrived() + { + _shipmentAggregateRoot.Arrive(1); + Should.Throw(() => _shipmentAggregateRoot.Arrive(1), "Stop has already arrived."); + } + + [Fact] + public void AlreadyPickedUp() + { + _shipmentAggregateRoot.Arrive(1); + _shipmentAggregateRoot.Pickup(1); + Should.Throw(() => _shipmentAggregateRoot.Pickup(1), "Stop has already departed."); + } + + [Fact] + public void AlreadyDelivered() + { + _shipmentAggregateRoot.Arrive(1); + _shipmentAggregateRoot.Pickup(1); + _shipmentAggregateRoot.Arrive(2); + _shipmentAggregateRoot.Deliver(2); + Should.Throw(() => _shipmentAggregateRoot.Deliver(2), "Stop has already departed."); + } + } +} \ No newline at end of file diff --git a/ValidState.csproj b/ValidState.csproj new file mode 100644 index 0000000..42e198e --- /dev/null +++ b/ValidState.csproj @@ -0,0 +1,13 @@ + + + + net5.0 + enable + + + + + + + +