Skip to content

Commit

Permalink
Add Counter.fsx
Browse files Browse the repository at this point in the history
  • Loading branch information
bartelink committed Feb 17, 2019
1 parent 4d2fd9f commit 285e6b4
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 1 deletion.
1 change: 1 addition & 0 deletions DOCUMENTATION.md
Expand Up @@ -202,6 +202,7 @@ type Service(log, resolveStream) =
stream.Read
```

<a name="api"></a>
# Equinox Handler API Usage Guide

The most terse walkthrough of what's involved in using Equinox to do a Synchronous Query and/or Execute a Decision process is in the [Programming Model section](#programming-model). In this section we’ll walk through how one implements common usage patterns using the Equinox Handler API in more detail (also using slightly less F# tricks!).
Expand Down
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -457,7 +457,7 @@ Yes, you have decisions to make; Equinox is not a panacea - there is no one size

### Is there a guide to building the simplest possible hello world "counter" sample, that simply counts with an add and a subtract event?

There's a skeleton one in [#56](https://github.com/jet/equinox/issues/56), but your best choices are probably to look at the `Aggregate.fs` and `Todo.fs` files emitted by [`dotnet new equinoxweb`](https://github.com/jet/dotnet-templates)
See the [Handler API Guide in DOCUMENTATION.md](DOCUMENTATION.md#api). An alternate way is to look at the `Todo.fs` files emitted by [`dotnet new equinoxweb`](https://github.com/jet/dotnet-templates) in the [QuickStart](#quickstart).

### OK, but you didn't answer my question, you just talked about stuff you wanted to talk about!

Expand Down
62 changes: 62 additions & 0 deletions samples/Tutorial/Counter.fsx
@@ -0,0 +1,62 @@
// Compile Tutorial.fsproj by either a) right-clicking or b) typing
// dotnet build samples/Tutorial before attempting to send this to FSI with Alt-Enter
#r "bin/Debug/netstandard2.0/Serilog.dll"
#r "bin/Debug/netstandard2.0/Serilog.Sinks.Console.dll"
#r "bin/Debug/netstandard2.0/Equinox.dll"
#r "bin/Debug/netstandard2.0/Equinox.MemoryStore.dll"

// Contributed by @voronoipotato

(* Events are things that have already happened,
they always exist in the past, and should always be past tense verbs*)

type Event =
| Incremented
| Decremented
| Cleared of int
(* A counter going up might clear to 0,
but a counter going down might clear to 100. *)

type State = State of int
(*Evolve takes the present state and one event and figures out the next state*)
let evolve state event =
match event, state with
| Incremented, State s -> State(s + 1)
| Decremented, State s -> State(s - 1)
| Cleared x , _ -> State x

(*fold is just folding the evolve function over all events to get the current state
It's equivalent to Linq's Aggregate function *)
let fold state events = Seq.fold evolve state events

(*Commands are the things we intend to happen, though they may not*)
type Command =
| Increment
| Decrement
| Clear of int

(* Decide consumes a command and the current state to decide what events actually happened.
This particular counter allows numbers from 0 to 100.*)
let decide command state =
match command with
| Increment ->
if state > 100 then [] else [Incremented]
| Decrement ->
if state <= 0 then [] else [Decremented]
| Clear i ->
if state = i then [] else [Cleared i]

type Handler(log, stream, ?maxAttempts) =
let inner = Equinox.Handler(log, stream, defaultArg maxAttempts 3)
member __.Execute command : Async<unit> =
inner.Transact(decide command)
member __.Read : Async<int> =
inner.Query id

type Service(log, resolveStream) =
let (|AggregateId|) (id : string) = Equinox.AggregateId("Counter", id)
let (|Stream|) (AggregateId id) = Handler(log, resolveStream id)
member __.Execute(Stream stream, command) : Async<unit> =
stream.Execute command
member __.Read(Stream stream) : Async<int> =
stream.Read
1 change: 1 addition & 0 deletions samples/Tutorial/Tutorial.fsproj
Expand Up @@ -7,6 +7,7 @@

<ItemGroup>
<None Include="Favorites.fsx" />
<None Include="Counter.fsx" />
<None Include="Todo.fsx" />
</ItemGroup>

Expand Down

0 comments on commit 285e6b4

Please sign in to comment.