Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Rework default Server.fs - move to a module #465

Merged
merged 3 commits into from
Nov 22, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 19 additions & 25 deletions Content/default/src/Server/Server.fs
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,32 @@ open Saturn

open Shared

type Storage() =
let todos = ResizeArray<_>()

member __.GetTodos() = List.ofSeq todos

member __.AddTodo(todo: Todo) =
module Storage =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it looks more succinct and maybe even more idiomatic. I'm not convinced though - prefer to use explicit class/object notation when dealing with internal state. Probably a matter of preference though - curious what other think

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@theimowski in absence of any feedback (happy for you to loop in some people explicitly) - I would suggest we go with this one and add a recipe for the class-based (with DI) approach.

let todos = ResizeArray ()
let addTodo (todo: Todo) =
if Todo.isValid todo.Description then
todos.Add todo
Ok()
else
Error "Invalid todo"

let storage = Storage()

storage.AddTodo(Todo.create "Create new SAFE project")
|> ignore

storage.AddTodo(Todo.create "Write your app")
|> ignore

storage.AddTodo(Todo.create "Ship it !!!")
|> ignore
do
addTodo (Todo.create "Create new SAFE project") |> ignore
addTodo (Todo.create "Write your app") |> ignore
addTodo (Todo.create "Ship it !!!") |> ignore

let todosApi =
{ getTodos = fun () -> async { return storage.GetTodos() }
addTodo =
fun todo ->
async {
match storage.AddTodo todo with
| Ok () -> return todo
| Error e -> return failwith e
} }
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this fantomas formatting?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably not.

getTodos = fun () -> async {
return Storage.todos |> List.ofSeq
}
addTodo = fun todo -> async {
return
match Storage.addTodo todo with
| Ok () -> todo
| Error e -> failwith e
}
}

let webApp =
Remoting.createApi ()
Expand All @@ -54,4 +48,4 @@ let app =
use_gzip
}

run app
run app