-
-
Notifications
You must be signed in to change notification settings - Fork 502
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
235 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
module ECommerce.Domain.Config | ||
|
||
let log = Serilog.Log.ForContext("isMetric", true) | ||
let createDecider stream = Equinox.Decider(log, stream, maxAttempts = 3) | ||
|
||
module Memory = | ||
|
||
let create codec initial fold store = | ||
Equinox.MemoryStore.MemoryStoreCategory(store, codec, fold, initial) | ||
|
||
module Cosmos = | ||
|
||
let private createCached codec initial fold accessStrategy (context, cache) = | ||
let cacheStrategy = Equinox.CosmosStore.CachingStrategy.SlidingWindow (cache, System.TimeSpan.FromMinutes 20.) | ||
Equinox.CosmosStore.CosmosStoreCategory(context, codec, fold, initial, cacheStrategy, accessStrategy) | ||
|
||
let createUnoptimized codec initial fold (context, cache) = | ||
let accessStrategy = Equinox.CosmosStore.AccessStrategy.Unoptimized | ||
createCached codec initial fold accessStrategy (context, cache) | ||
|
||
// let createSnapshotted codec initial fold (isOrigin, toSnapshot) (context, cache) = | ||
// let accessStrategy = Equinox.CosmosStore.AccessStrategy.Snapshot (isOrigin, toSnapshot) | ||
// createCached codec initial fold accessStrategy (context, cache) | ||
// | ||
let createRollingState codec initial fold toSnapshot (context, cache) = | ||
let accessStrategy = Equinox.CosmosStore.AccessStrategy.RollingState toSnapshot | ||
createCached codec initial fold accessStrategy (context, cache) | ||
|
||
[<NoComparison; NoEquality; RequireQualifiedAccess>] | ||
type Store<'t> = | ||
| Memory of Equinox.MemoryStore.VolatileStore<'t> | ||
| Cosmos of Equinox.CosmosStore.CosmosStoreContext * Equinox.Core.ICache |
23 changes: 23 additions & 0 deletions
23
Sample/ECommerce.Equinox/ECommerce.Domain/ECommerce.Domain.fsproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net5.0</TargetFramework> | ||
<GenerateDocumentationFile>true</GenerateDocumentationFile> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<Compile Include="Config.fs" /> | ||
<Compile Include="Types.fs" /> | ||
<Compile Include="PricedProductItem.fs" /> | ||
<Compile Include="IProductPriceCalculator.fs" /> | ||
<Compile Include="ShoppingCart.fs" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Equinox.CosmosStore" Version="3.0.5" /> | ||
<PackageReference Include="Equinox.MemoryStore" Version="3.0.5" /> | ||
<PackageReference Include="FsCodec.NewtonsoftJson" Version="2.2.2" /> | ||
<PackageReference Include="Propulsion" Version="2.11.0" /> | ||
</ItemGroup> | ||
|
||
</Project> |
16 changes: 16 additions & 0 deletions
16
Sample/ECommerce.Equinox/ECommerce.Domain/IProductPriceCalculator.fs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
namespace ECommerce.Domain | ||
|
||
type IProductPriceCalculator = | ||
|
||
abstract Calculate : ProductItem -> PricedProductItem | ||
|
||
type RandomProductPriceCalculator() = | ||
|
||
let productPrices = System.Collections.Concurrent.ConcurrentDictionary<ProductId, decimal>() | ||
|
||
interface IProductPriceCalculator with | ||
override _.Calculate(productItem : ProductItem) = | ||
let r = System.Random() | ||
let calc _ = (r.NextDouble() |> decimal) * 100m | ||
let price : decimal = productPrices.GetOrAdd(productItem.productId, calc) | ||
PricedProductItem.From(productItem, price) |
26 changes: 26 additions & 0 deletions
26
Sample/ECommerce.Equinox/ECommerce.Domain/PricedProductItem.fs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
namespace ECommerce.Domain | ||
|
||
open System | ||
|
||
type ProductItem = { productId : ProductId; quantity : int } | ||
|
||
type PricedProductItem = { productItem : ProductItem; unitPrice : decimal } with | ||
|
||
member x.ProductId = x.productItem.productId | ||
member x.Quantity = x.productItem.quantity | ||
member x.TotalPrice = decimal x.Quantity * x.unitPrice | ||
|
||
static member From(productItem, ?unitPrice : decimal) = | ||
match unitPrice with | ||
| None -> nullArg (nameof(unitPrice)) | ||
| Some price when price <= 0m -> raise <| ArgumentOutOfRangeException(nameof unitPrice, "Unit price has to be positive number") | ||
| Some price -> { productItem = productItem; unitPrice = price } | ||
|
||
member x.MatchesProductAndUnitPrice(pricedProductItem : PricedProductItem) = | ||
x.ProductId = pricedProductItem.ProductId && x.unitPrice = pricedProductItem.unitPrice | ||
|
||
member x.MergeWith(productItem : PricedProductItem) = | ||
if x.ProductId <> productItem.ProductId then raise <| ArgumentException "Product ids do not match." | ||
if x.unitPrice <> productItem.unitPrice then raise <| ArgumentException "Product unit prices do not match." | ||
// TODO fix bug in source: new ProductItem(ProductId, productItem.Quantity + productItem.Quantity), | ||
{ productItem = { productId = x.ProductId; quantity = x.Quantity + productItem.Quantity }; unitPrice = x.unitPrice } |
103 changes: 103 additions & 0 deletions
103
Sample/ECommerce.Equinox/ECommerce.Domain/ShoppingCart.fs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
module ECommerce.Domain.ShoppingCart | ||
|
||
let [<Literal>] Category = "ShoppingCart" | ||
|
||
let streamName = CartId.toString >> FsCodec.StreamName.create Category | ||
|
||
module Events = | ||
|
||
type Event = | ||
| Initialized of {| clientId : ClientId |} | ||
| ItemAdded of {| productId : ProductId; quantity : int; unitPrice : decimal |} | ||
| ItemRemoved of {| productId : ProductId; (*; quantity : int;*) unitPrice : decimal |} | ||
| Confirmed of {| confirmedAt : System.DateTimeOffset |} | ||
interface TypeShape.UnionContract.IUnionContract | ||
let codec = FsCodec.NewtonsoftJson.Codec.Create<Event>() | ||
|
||
module Fold = | ||
|
||
type Status = Pending | Confirmed | Cancelled | ||
type State = | ||
{ clientId : ClientId option | ||
status : Status; items : PricedProductItem array | ||
confirmedAt : System.DateTimeOffset option } | ||
let initial = { clientId = None; status = Status.Pending; items = Array.empty; confirmedAt = None } | ||
let isClosed (s : State) = match s.status with Confirmed | Cancelled -> true | Pending -> false | ||
module ItemList = | ||
let keys (x : PricedProductItem) = x.productItem.productId, x.unitPrice | ||
let add (productId, price, quantity) (current : PricedProductItem seq) = | ||
let newItemKeys = productId, price | ||
let mkItem (productId, price, quantity) = { productItem = { productId = productId; quantity = quantity }; unitPrice = price } | ||
let mutable merged = false | ||
[| for x in current do | ||
if newItemKeys = keys x then | ||
mkItem (productId, price, x.productItem.quantity + quantity) | ||
merged <- true | ||
else | ||
x | ||
if not merged then | ||
mkItem (productId, price, quantity) |] | ||
let remove (productId, price) (current : PricedProductItem[]) = | ||
current |> Array.where (fun x -> keys x <> (productId, price)) | ||
let private evolve s = function | ||
| Events.Initialized e -> { s with clientId = Some e.clientId } | ||
| Events.ItemAdded e -> { s with items = s.items |> ItemList.add (e.productId, e.unitPrice, e.quantity) } | ||
| Events.ItemRemoved e -> { s with items = s.items |> ItemList.remove (e.productId, e.unitPrice) } | ||
| Events.Confirmed e -> { s with confirmedAt = Some e.confirmedAt } | ||
let fold = Seq.fold evolve | ||
|
||
let decideInitialize clientId (s : Fold.State) = | ||
if s.clientId <> None then [] | ||
else [ Events.Initialized {| clientId = clientId |}] | ||
|
||
let decideAdd calculatePrice productId quantity = function | ||
| s when Fold.isClosed s -> invalidOp $"Adding product item for cart in '%A{s.status}' status is not allowed." | ||
| _ -> | ||
let price = calculatePrice (productId, quantity) | ||
[ Events.ItemAdded {| productId = productId; unitPrice = price; quantity = quantity |} ] | ||
|
||
let decideRemove (productId, price) = function | ||
| s when Fold.isClosed s -> invalidOp $"Removing product item for cart in '%A{s.status}' status is not allowed." | ||
| _ -> | ||
[ Events.ItemRemoved {| productId = productId; unitPrice = price |} ] | ||
|
||
let decideConfirm at = function | ||
| s when Fold.isClosed s -> [] | ||
| _ -> [ Events.Confirmed {| confirmedAt = at |} ] | ||
|
||
type Service(resolve : CartId -> Equinox.Decider<Events.Event, Fold.State>, calculatePrice : ProductId * int -> decimal) = | ||
|
||
member _.Initialize(cartId, clientId) = | ||
let decider = resolve cartId | ||
decider.Transact(decideInitialize clientId) | ||
|
||
member _.Add(cartId, productId, quantity) = | ||
let decider = resolve cartId | ||
decider.Transact(decideAdd calculatePrice productId quantity) | ||
|
||
// TODO fix in Remove: throw new InvalidOperationException($"Adding product item for cart in '{shoppingCart.Status}' status is not allowed."); | ||
|
||
member _.Remove(cartId, productId, price) = | ||
let decider = resolve cartId | ||
decider.Transact(decideRemove (productId, price)) | ||
|
||
member _.Confirm(cartId, at) = | ||
let decider = resolve cartId | ||
decider.Transact(decideConfirm at) | ||
|
||
module Config = | ||
|
||
let calculatePrice (pricer : IProductPriceCalculator) (productId, quantity) = | ||
let priced = pricer.Calculate( { productId = productId; quantity = quantity }) | ||
priced.unitPrice | ||
|
||
let private resolveStream = function | ||
| Config.Store.Memory store -> | ||
let cat = Config.Memory.create Events.codec Fold.initial Fold.fold store | ||
cat.Resolve | ||
| Config.Store.Cosmos (context, cache) -> | ||
let cat = Config.Cosmos.createUnoptimized Events.codec Fold.initial Fold.fold (context, cache) | ||
cat.Resolve | ||
let private resolveDecider store = streamName >> resolveStream store >> Config.createDecider | ||
let create (pricer : IProductPriceCalculator) store = | ||
Service(resolveDecider store, calculatePrice pricer) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
namespace ECommerce.Domain | ||
|
||
open FSharp.UMX | ||
open System | ||
|
||
type ProductId = Guid<productId> | ||
and [<Measure>] productId | ||
module ProductId = | ||
let toString (x : ProductId) : string = (UMX.untag x).ToString("N") | ||
let parse (value : Guid) : ProductId = %value | ||
|
||
type ClientId = Guid<clientId> | ||
and [<Measure>] clientId | ||
module ClientId = | ||
let toString (x : ClientId) : string = (UMX.untag x).ToString("N") | ||
let parse (value : Guid) : ClientId = %value | ||
|
||
type CartId = Guid<cartId> | ||
and [<Measure>] cartId | ||
module CartId = | ||
let toString (x : CartId) : string = (UMX.untag x).ToString("N") | ||
let parse (value : Guid Nullable) : CartId = | ||
if not value.HasValue || value.Value = Guid.Empty then raise <| ArgumentOutOfRangeException(nameof value) | ||
%value.Value | ||
|