Last active
January 4, 2018 16:39
-
-
Save eulerfx/4975035 to your computer and use it in GitHub Desktop.
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
/// An inventory item. | |
[<RequireQualifiedAccess>] | |
module InventoryItem | |
/// Represents the state of an inventory item. | |
type State = { | |
isActive : bool; | |
} | |
with static member Zero = { isActive = false } | |
/// An inventory item command. | |
type Command = | |
| Create of string | |
| Deactivate | |
| Rename of string | |
| CheckInItems of int | |
| RemoveItems of int | |
/// An inventory item event. | |
type Event = | |
| Created of string | |
| Deactivated | |
| Renamed of string | |
| ItemsCheckedIn of int | |
| ItemsRemoved of int | |
/// Applies a inventory item event to a state. | |
let apply item = function | |
| Created _ -> { item with State.isActive = true; } | |
| Deactivated _ -> { item with State.isActive = false; } | |
| Renamed _ -> item | |
| ItemsCheckedIn _ -> item | |
| ItemsRemoved _ -> item | |
/// Assertions used to maintain invariants upon command execution. | |
module private Assert = | |
let validName name = if System.String.IsNullOrEmpty(name) then invalidArg "name" "The name must not be null." else name | |
let validCount count = if count <= 0 then invalidArg "count" "Inventory count must be positive." else count | |
let inactive item = if item.isActive = true then failwith "The item is already deactivated." | |
/// Executes an inventory item command. | |
let exec item = | |
let apply event = | |
let newItem = apply item event | |
event | |
function | |
| Create(name) -> name |> Assert.validName |> Created |> apply | |
| Deactivate -> item |> Assert.inactive; Deactivated |> apply | |
| Rename(name) -> name |> Assert.validName |> Renamed |> apply | |
| CheckInItems(count) -> count |> Assert.validCount |> ItemsCheckedIn |> apply | |
| RemoveItems(count) -> count |> Assert.validCount |> ItemsRemoved |> apply |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
No because the Assert.inactive is a failwith, to help your intuition Deactivated doesn't contain anything so nothing can be passed into it.