Skip to content

Instantly share code, notes, and snippets.

@swlaschin
Created May 13, 2019 13:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save swlaschin/c153c0c68d894f6e9542fb2a2cfb746e to your computer and use it in GitHub Desktop.
Save swlaschin/c153c0c68d894f6e9542fb2a2cfb746e to your computer and use it in GitHub Desktop.
Workflow Template example
#load "Result.fs"
// ==================================
// Example of workflows with Onion architecture
// ==================================
let notImplemented() = failwith "not implemented"
// ==================================
// pure workflow
// ==================================
/// Optional: in assembly "Cores"
module Domain =
type EverythingINeedForThisWorkflow = {
Value1: int
Value2: string
}
type Customer = {
Name : string
}
type WorkflowOutput = {
Something : string
}
/// Optional: in assembly "Core"
module Workflow =
open Domain
/// All the errors that can happen in this workflow
type WorkflowError =
| BadValidation
| BadData
let validate (dto:EverythingINeedForThisWorkflow) :Result<Customer,WorkflowError> =
notImplemented()
let businessLogic (customer:Customer) :Result<WorkflowOutput,WorkflowError> =
notImplemented()
let innerWorkflow dto = result {
// validate
let! customer = validate dto
// do stuff
let! workflowOutput = businessLogic customer
// return
return workflowOutput
}
// ==================================
// IO workflow
// ==================================
/// Optional: in assembly "DB"
module Database =
open Domain
open Workflow
let loadWorkflowDataNormal key :EverythingINeedForThisWorkflow =
notImplemented()
let loadWorkflowDataAsync key :Async<EverythingINeedForThisWorkflow> =
notImplemented()
let loadWorkflowDataAR key :AsyncResult<EverythingINeedForThisWorkflow,WorkflowError> =
notImplemented()
let saveWorkflowOutput (data:WorkflowOutput) :unit =
notImplemented()
let saveWorkflowOutputAR (data:WorkflowOutput) :AsyncResult<unit,WorkflowError> =
notImplemented()
/// Optional: in assembly "WebApp"
module Program =
open Workflow
open Database
let workflowWithIO = asyncResult {
// beginning -- load data from outside world
let data = loadWorkflowDataNormal "myKey" // no bang needed
let! data2 = loadWorkflowDataAsync "myKey" |> AsyncResult.ofAsync
let! data3 = loadWorkflowDataAR "myKey" // no lifting needed
// do the core business logic
let! workflowOutput =
innerWorkflow data // this returns a Result, so we have to lift it
|> AsyncResult.ofResult
// save the data back
let _ = saveWorkflowOutput workflowOutput // no bang needed
do saveWorkflowOutput workflowOutput // same as above
let! _ = saveWorkflowOutputAR workflowOutput // bang needed
do! saveWorkflowOutputAR workflowOutput // same as above
return ()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment