Created
December 18, 2013 11:09
-
-
Save liammclennan/8020631 to your computer and use it in GitHub Desktop.
Hexagonal Architecture. No ORM.
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
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
As pointed out by Mr Harcourt, domain events cause problems in this model.