Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
DDDInventoryItemFSharp/DDDInventoryItemFSharp/InventoryItem.fs
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
43 lines (36 sloc)
1.4 KB
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
[<RequireQualifiedAccess>] | |
module InventoryItem | |
type State = { | |
isActive : bool; | |
} | |
with static member Zero = { isActive = false } | |
type Command = | |
| Create of string | |
| Deactivate | |
| Rename of string | |
| CheckInItems of int | |
| RemoveItems of int | |
type Event = | |
| Created of string | |
| Deactivated | |
| Renamed of string | |
| ItemsCheckedIn of int | |
| ItemsRemoved of int | |
let apply item = function | |
| Created _ -> { item with State.isActive = true; } | |
| Deactivated _ -> { item with State.isActive = false; } | |
| Renamed _ -> item | |
| ItemsCheckedIn _ -> item | |
| ItemsRemoved _ -> item | |
open Validator | |
module private Assert = | |
let validName name = notNull ["The name must not be null."] name <* notEmptyString ["The name must not be empty"] name | |
let validCount count = validator (fun c -> c > 0) ["The item count must be positive."] count | |
let inactive state = validator (fun i -> i.isActive = false) ["The item is already deactivated."] state | |
let exec state = | |
function | |
| Create name -> Assert.validName name <?> Created(name) | |
| Deactivate -> Assert.inactive state <?> Deactivated | |
| Rename name -> Assert.validName name <?> Renamed(name) | |
| CheckInItems count -> Assert.validCount count <?> ItemsCheckedIn(count) | |
| RemoveItems count -> Assert.validCount count <?> ItemsRemoved(count) |