Created
April 28, 2013 17:51
-
-
Save eulerfx/5477724 to your computer and use it in GitHub Desktop.
Validation with exceptions
This file contains hidden or 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
| 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." | |
| 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