Skip to content

Commit 7bba118

Browse files
committed
Aggregate layout/naming consistency
1 parent df6cd8d commit 7bba118

File tree

6 files changed

+50
-52
lines changed

6 files changed

+50
-52
lines changed

equinox-fc/Domain.Tests/LocationEpochTests.fs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ let [<Property>] properties carriedForward delta1 closeImmediately delta2 close
2222
(* Starting with an empty stream, we'll need to supply the balance carried forward, optionally we apply a delta and potentially close *)
2323

2424
let initialShouldClose _state = closeImmediately
25-
let res,events = sync (Some carriedForward) (validateAndInterpret carriedForward delta1) initialShouldClose Folds.initial
25+
let res,events = sync (Some carriedForward) (validateAndInterpret carriedForward delta1) initialShouldClose Fold.initial
2626
let cfEvents events = events |> List.filter (function Events.CarriedForward _ -> true | _ -> false)
2727
let closeEvents events = events |> List.filter (function Events.Closed -> true | _ -> false)
28-
let state1 = Folds.fold Folds.initial events
28+
let state1 = Fold.fold Fold.initial events
2929
let expectedBalance = carriedForward + delta1
3030
// Only expect closing if it was requested
3131
let expectImmediateClose = closeImmediately

equinox-fc/Domain.Tests/LocationSeriesTests.fs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ open Swensen.Unquote
66
open Location.Series
77

88
let [<Property>] properties c1 c2 =
9-
let events = interpretActivateEpoch c1 Folds.initial
10-
let state1 = Folds.fold Folds.initial events
9+
let events = interpretActivateEpoch c1 Fold.initial
10+
let state1 = Fold.fold Fold.initial events
1111
let epoch0 = %0
1212
match c1, events, toActiveEpoch state1 with
1313
// Started events are not written for < 0
@@ -21,7 +21,7 @@ let [<Property>] properties c1 c2 =
2121
test <@ List.isEmpty l @>
2222

2323
let events = interpretActivateEpoch c2 state1
24-
let state2 = Folds.fold state1 events
24+
let state2 = Fold.fold state1 events
2525
match toActiveEpoch state1, c2, events, toActiveEpoch state2 with
2626
// Started events are not written for < 0
2727
| None, n, [], activeEpoch when n < epoch0 ->

equinox-fc/Domain.Tests/LocationTests.fs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@ open System
99
/// Helpers to match `module Cosmos` wrapping inside the impl
1010
module Location =
1111

12-
open Equinox.MemoryStore
12+
module MemoryStore =
1313

14-
module Series =
14+
open Equinox.MemoryStore
1515

16-
let resolve store = Resolver(store, Series.Events.codec, Series.Folds.fold, Series.Folds.initial).Resolve
16+
module Series =
1717

18-
module Epoch =
18+
let resolve store = Resolver(store, Series.Events.codec, Series.Fold.fold, Series.Fold.initial).Resolve
1919

20-
let resolve store = Resolver(store, Epoch.Events.codec, Epoch.Folds.fold, Epoch.Folds.initial).Resolve
20+
module Epoch =
2121

22-
module MemoryStore =
22+
let resolve store = Resolver(store, Epoch.Events.codec, Epoch.Fold.fold, Epoch.Fold.initial).Resolve
2323

2424
let createService (zeroBalance, shouldClose) store =
2525
let maxAttempts = Int32.MaxValue
@@ -35,7 +35,7 @@ let run (service : LocationService) (IdsAtLeastOne locations, deltas : _[]) = As
3535

3636
(* Apply random deltas *)
3737

38-
let adjust delta (bal : Epoch.Folds.Balance) =
38+
let adjust delta (bal : Epoch.Fold.Balance) =
3939
let value = max -bal delta
4040
if value = 0 then 0, []
4141
else value, [Location.Epoch.Events.Delta { value = value }]
@@ -51,7 +51,7 @@ let [<Property>] ``MemoryStore properties`` maxEvents args =
5151
let store = Equinox.MemoryStore.VolatileStore()
5252
let zeroBalance = 0
5353
let maxEvents = max 1 maxEvents
54-
let shouldClose (state : Epoch.Folds.OpenState) = state.count > maxEvents
54+
let shouldClose (state : Epoch.Fold.OpenState) = state.count > maxEvents
5555
let service = Location.MemoryStore.createService (zeroBalance, shouldClose) store
5656
run service args
5757

@@ -65,6 +65,6 @@ type Cosmos(testOutput) =
6565
let [<Property(MaxTest=10)>] properties maxEvents args =
6666
let zeroBalance = 0
6767
let maxEvents = max 1 maxEvents
68-
let shouldClose (state : Epoch.Folds.OpenState) = state.count > maxEvents
68+
let shouldClose (state : Epoch.Fold.OpenState) = state.count > maxEvents
6969
let service = Location.Cosmos.createService (zeroBalance, shouldClose) (context,cache,Int32.MaxValue)
7070
run service args

equinox-fc/Domain/Location.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ namespace Location
22

33
[<NoComparison; NoEquality>]
44
type Wip<'R> =
5-
| Pending of decide : (Epoch.Folds.Balance -> 'R*Epoch.Events.Event list)
5+
| Pending of decide : (Epoch.Fold.Balance -> 'R*Epoch.Events.Event list)
66
| Complete of 'R
77

88
/// Manages a Series of Epochs, with a running total being carried forward to the next Epoch when it's Closed

equinox-fc/Domain/LocationEpoch.fs

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ module Events =
1212
| Delta of Delta
1313
interface TypeShape.UnionContract.IUnionContract
1414
let codec = FsCodec.NewtonsoftJson.Codec.Create<Event>()
15-
let [<Literal>] categoryId = "LocationEpoch"
16-
let (|AggregateId|) (locationId, epochId) =
15+
let [<Literal>] category = "LocationEpoch"
16+
let (|For|) (locationId, epochId) =
1717
let id = sprintf "%s_%s" (LocationId.toString locationId) (LocationEpochId.toString epochId)
18-
Equinox.AggregateId(categoryId, id)
18+
Equinox.AggregateId(category, id)
1919

20-
module Folds =
20+
module Fold =
2121

2222
type Balance = int
2323
type OpenState = { count : int; value : Balance }
@@ -36,55 +36,54 @@ module Folds =
3636
/// Holds events accumulated from a series of decisions while also evolving the presented `state` to reflect the pended events
3737
type private Accumulator() =
3838
let acc = ResizeArray()
39-
member __.Ingest state : 'res * Events.Event list -> 'res * Folds.State = function
39+
member __.Ingest state : 'res * Events.Event list -> 'res * Fold.State = function
4040
| res, [] -> res, state
41-
| res, [e] -> acc.Add e; res, Folds.evolve state e
42-
| res, xs -> acc.AddRange xs; res, Folds.fold state (Seq.ofList xs)
41+
| res, [e] -> acc.Add e; res, Fold.evolve state e
42+
| res, xs -> acc.AddRange xs; res, Fold.fold state (Seq.ofList xs)
4343
member __.Accumulated = List.ofSeq acc
4444

45-
type Result<'t> = { balance : Folds.Balance; result : 't option; isOpen : bool }
45+
type Result<'t> = { balance : Fold.Balance; result : 't option; isOpen : bool }
4646

47-
let sync (balanceCarriedForward : Folds.Balance option) (decide : (Folds.Balance -> 't*Events.Event list)) shouldClose state : Result<'t>*Events.Event list =
47+
let sync (balanceCarriedForward : Fold.Balance option) (decide : (Fold.Balance -> 't*Events.Event list)) shouldClose state : Result<'t>*Events.Event list =
4848
let acc = Accumulator()
4949
// We always want to have a CarriedForward event at the start of any Epoch's event stream
5050
let (), state =
5151
acc.Ingest state <|
5252
match state with
53-
| Folds.Initial -> (), [Events.CarriedForward { initial = Option.get balanceCarriedForward }]
54-
| Folds.Open _ | Folds.Closed _ -> (), []
53+
| Fold.Initial -> (), [Events.CarriedForward { initial = Option.get balanceCarriedForward }]
54+
| Fold.Open _ | Fold.Closed _ -> (), []
5555
// Run, unless we determine we're in Closed state
5656
let result, state =
5757
acc.Ingest state <|
5858
match state with
59-
| Folds.Initial -> failwith "We've just guaranteed not Initial"
60-
| Folds.Open { value = bal } -> let r,es = decide bal in Some r,es
61-
| Folds.Closed _ -> None, []
59+
| Fold.Initial -> failwith "We've just guaranteed not Initial"
60+
| Fold.Open { value = bal } -> let r,es = decide bal in Some r,es
61+
| Fold.Closed _ -> None, []
6262
// Finally (iff we're `Open`, have run a `decide` and `shouldClose`), we generate a Closed event
6363
let (balance, isOpen), _ =
6464
acc.Ingest state <|
6565
match state with
66-
| Folds.Initial -> failwith "Can't be Initial"
67-
| Folds.Open ({ value = bal } as openState) when shouldClose openState -> (bal, false), [Events.Closed]
68-
| Folds.Open { value = bal } -> (bal, true), []
69-
| Folds.Closed bal -> (bal, false), []
66+
| Fold.Initial -> failwith "Can't be Initial"
67+
| Fold.Open ({ value = bal } as openState) when shouldClose openState -> (bal, false), [Events.Closed]
68+
| Fold.Open { value = bal } -> (bal, true), []
69+
| Fold.Closed bal -> (bal, false), []
7070
{ balance = balance; result = result; isOpen = isOpen }, acc.Accumulated
7171

72-
type Service internal (resolve, ?maxAttempts) =
72+
type Service internal (log, resolve, maxAttempts) =
7373

74-
let log = Serilog.Log.ForContext<Service>()
75-
let (|Stream|) (Events.AggregateId id) = Equinox.Stream<Events.Event,Folds.State>(log, resolve id, maxAttempts = defaultArg maxAttempts 2)
74+
let resolve (Events.For id) = Equinox.Stream<Events.Event, Fold.State>(log, resolve id, maxAttempts)
7675

7776
member __.Sync<'R>(locationId, epochId, prevEpochBalanceCarriedForward, decide, shouldClose) : Async<Result<'R>> =
78-
let (Stream stream) = (locationId, epochId)
77+
let stream = resolve (locationId, epochId)
7978
stream.Transact(sync prevEpochBalanceCarriedForward decide shouldClose)
8079

81-
let create resolve maxAttempts = Service(resolve, maxAttempts)
80+
let create resolve maxAttempts = Service(Serilog.Log.ForContext<Service>(), resolve, maxAttempts = maxAttempts)
8281

8382
module Cosmos =
8483

8584
open Equinox.Cosmos
8685
let resolve (context,cache) =
8786
let cacheStrategy = CachingStrategy.SlidingWindow (cache, System.TimeSpan.FromMinutes 20.)
88-
Resolver(context, Events.codec, Folds.fold, Folds.initial, cacheStrategy, AccessStrategy.Unoptimized).Resolve
87+
Resolver(context, Events.codec, Fold.fold, Fold.initial, cacheStrategy, AccessStrategy.Unoptimized).Resolve
8988
let createService (context,cache,maxAttempts) =
9089
create (resolve (context,cache)) maxAttempts

equinox-fc/Domain/LocationSeries.fs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,44 +9,43 @@ module Events =
99
| Started of Started
1010
interface TypeShape.UnionContract.IUnionContract
1111
let codec = FsCodec.NewtonsoftJson.Codec.Create<Event>()
12-
let [<Literal>] categoryId = "LocationSeries"
13-
let (|AggregateId|) id = Equinox.AggregateId(categoryId, LocationId.toString id)
12+
let [<Literal>] category = "LocationSeries"
13+
let (|For|) id = Equinox.AggregateId(category, LocationId.toString id)
1414

15-
module Folds =
15+
module Fold =
1616

1717
type State = LocationEpochId
1818
let initial = LocationEpochId.parse -1
1919
let evolve _state = function
2020
| Events.Started e -> e.epochId
2121
let fold = Seq.fold evolve
2222

23-
let interpretActivateEpoch epochId (state : Folds.State) =
23+
let interpretActivateEpoch epochId (state : Fold.State) =
2424
[if state < epochId then yield Events.Started { epochId = epochId }]
2525

2626
let toActiveEpoch state =
27-
if state = Folds.initial then None else Some state
27+
if state = Fold.initial then None else Some state
2828

29-
type Service internal (resolve, ?maxAttempts) =
29+
type Service internal (log, resolve, maxAttempts) =
3030

31-
let log = Serilog.Log.ForContext<Service>()
32-
let (|Stream|) (Events.AggregateId id) = Equinox.Stream<Events.Event,Folds.State>(log, resolve id, maxAttempts = defaultArg maxAttempts 2)
31+
let resolve (Events.For id) = Equinox.Stream<Events.Event, Fold.State>(log, resolve id, maxAttempts)
3332

3433
member __.Read(locationId) : Async<LocationEpochId option> =
35-
let (Stream stream) = locationId
34+
let stream = resolve locationId
3635
stream.Query toActiveEpoch
3736

38-
member __.ActivateEpoch(locationId,epochId) : Async<unit> =
39-
let (Stream stream) = locationId
37+
member __.ActivateEpoch(locationId, epochId) : Async<unit> =
38+
let stream = resolve locationId
4039
stream.Transact(interpretActivateEpoch epochId)
4140

42-
let create resolve maxAttempts = Service(resolve, maxAttempts)
41+
let create resolve maxAttempts = Service(Serilog.Log.ForContext<Service>(), resolve, maxAttempts)
4342

4443
module Cosmos =
4544

4645
open Equinox.Cosmos
4746
let resolve (context,cache) =
4847
let cacheStrategy = CachingStrategy.SlidingWindow (cache, System.TimeSpan.FromMinutes 20.)
4948
let opt = Equinox.ResolveOption.AllowStale
50-
fun id -> Resolver(context, Events.codec, Folds.fold, Folds.initial, cacheStrategy, AccessStrategy.LatestKnownEvent).Resolve(id,opt)
49+
fun id -> Resolver(context, Events.codec, Fold.fold, Fold.initial, cacheStrategy, AccessStrategy.LatestKnownEvent).Resolve(id,opt)
5150
let createService (context, cache, maxAttempts) =
5251
create (resolve (context,cache)) maxAttempts

0 commit comments

Comments
 (0)