Skip to content

Instantly share code, notes, and snippets.

@liammclennan
Created December 18, 2013 11:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save liammclennan/8020631 to your computer and use it in GitHub Desktop.
Save liammclennan/8020631 to your computer and use it in GitHub Desktop.
Hexagonal Architecture. No ORM.
module domain =
type post = { id: Guid; title: string; content: string; author_id: Guid }
type author = { id: Guid; name: string; }
type crudAction = Create | Update | Delete
type change = { action: crudAction; entity: obj; id: Guid }
let addPost (p:post) : seq<change> =
// do stuff
seq { yield {action = Create; entity = box p; id = p.id} }
let countWordsByAuthor postsByAuthor =
Seq.map (fun p -> (p.content.Split [|' '|]).Length) postsByAuthor
|> Seq.sum
module persistence =
open domain
let private saveChange c =
match c with
| { action = Create; entity = e; id = id } -> printfn "creatin' %A" e
| { action = Update; entity = e; id = id } -> printfn "updatin' %A" e
| { action = Delete; entity = e; id = id } -> printfn "deletin' %A" e
let saveChanges cs =
Seq.iter saveChange cs
let query view id : seq<post> =
let p = {
id= Guid.NewGuid();
title = "F# syntax in 60 seconds";
content = "Here is a very quick overview on how to read F# code for newcomers unfamiliar with the syntax.";
author_id = Guid.NewGuid()
}
seq { yield p }
open domain
open persistence
let scott = { id = Guid.NewGuid(); name = "Scott Wlaschin" }
let whyUseF = {
id= Guid.NewGuid();
title = "Why Use F#";
content = "Because I like it";
author_id = scott.id
}
addPost whyUseF
|> saveChanges
// queries with logic like ...
query "PostsByAuthor" scott.id
|> countWordsByAuthor
|> Console.WriteLine
@liammclennan
Copy link
Author

As pointed out by Mr Harcourt, domain events cause problems in this model.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment