Build an ASP.NET Core Web App with F#? You must be having a Giraffe!

A presentation at DDD South West 2019 in April 2019 in Bristol, UK by Stuart Lang

Slide 1

Slide 1

Build an ASP.NET Core Web App with F#? You must be having a Giraffe! https://stu.dev

Slide 2

Slide 2

Hello! https://stu.dev https://stu.dev 2

Slide 3

Slide 3

What I’ll cover ◉ ◉ ◉ ◉ ◉ https://stu.dev What is Giraffe? Where does it fit in? Why F#? How does Giraffe work? Questions 3

Slide 4

Slide 4

1 https://stu.dev What is it? 4

Slide 5

Slide 5

5 A functional ASP.NET Core micro web framework for building rich web applications. “ https://stu.dev

Slide 6

Slide 6

Idiomatic C# https://stu.dev Idiomatic F# 6

Slide 7

Slide 7

(Saturn) https://stu.dev 7

Slide 8

Slide 8

ThoughtWorks Technology Radar https://stu.dev 8

Slide 9

Slide 9

1 https://stu.dev Why F#? 9

Slide 10

Slide 10

Why F#? Like C# it’s ◉ General Purpose, so can be used for ○ ○ ○ ○ ○ ○ https://stu.dev Web apps Microservices Cloud apps Xamarin apps Desktop … 10

Slide 11

Slide 11

Why F#? Like C# it’s ◉ Multi-paradigm ○ Functional first ○ Imperative and mutability when you need it ○ OO when you need it https://stu.dev 11

Slide 12

Slide 12

Why F#? Like C# it’s ◉ Out-of-the-box with .NET Core SDK ○ It’s there, and project templates https://stu.dev 12

Slide 13

Slide 13

Why F#? Like C# it’s ◉ Got great tooling support https://stu.dev 13

Slide 14

Slide 14

Why F#? You don’t need to ask your boss Stu says it’s fine! https://stu.dev 14

Slide 15

Slide 15

Why F#? F# is concise and low ceremony https://stu.dev 15

Slide 16

Slide 16

Why F#? Correctness ◉ Solves the null problem ◉ Immutable by default ◉ Make invalidate state impossible to represent https://stu.dev 16

Slide 17

Slide 17

Why F#? - Forward Pipe Functions and values can have any name you chose https://stu.dev 17

Slide 18

Slide 18

Why F#? - Records https://stu.dev 18

Slide 19

Slide 19

Why F#? - Discriminated Unions Credit: Luke Merrett https://stu.dev 19

Slide 20

Slide 20

Why F#? It’s great for domain modelling 20

Slide 21

Slide 21

1 https://stu.dev How does it work? 21

Slide 22

Slide 22

An example Giraffe app (source) https://stu.dev 22

Slide 23

Slide 23

Let’s model it! type HttpServer = https://stu.dev 23

Slide 24

Slide 24

Let’s model it! type HttpHandler = https://stu.dev 24

Slide 25

Slide 25

Let’s model it! https://stu.dev 25

Slide 26

Slide 26

Let’s model it! Taking inspiration from Suave… https://stu.dev 26

Slide 27

Slide 27

Let’s model it! type HttpHandler = https://stu.dev 27

Slide 28

Slide 28

Let’s model it! https://stu.dev 28

Slide 29

Slide 29

Let’s model it! type HttpHandler = https://stu.dev 29

Slide 30

Slide 30

Let’s model it! (compose / >=>) https://stu.dev 30

Slide 31

Slide 31

Let’s model it! https://stu.dev 31

Slide 32

Slide 32

Let’s model it! https://stu.dev 32

Slide 33

Slide 33

Let’s model it! https://stu.dev 33

Slide 34

Slide 34

Let’s model it! https://stu.dev 34

Slide 35

Slide 35

Let’s model it! (btw, this is just the same as:) https://stu.dev 35

Slide 36

Slide 36

Let’s model it! type HttpHandler = https://stu.dev 36

Slide 37

Slide 37

Let’s model it! https://stu.dev 37

Slide 38

Slide 38

Let’s model it! https://stu.dev 38

Slide 39

Slide 39

Let’s model it! But it could be better… https://medium.com/@gerardtoconnor/carry-on-continuation -over-binding-pipelines-for-functional-web-58bd7e6ea009 https://stu.dev 39

Slide 40

Slide 40

Let’s model it! type HttpFunc = type HttpHandler = https://stu.dev 40

Slide 41

Slide 41

Let’s model it! https://stu.dev 41

Slide 42

Slide 42

Let’s model it! type HttpFunc = type HttpHandler = https://stu.dev 42

Slide 43

Slide 43

Let’s model it! Familiar… ASP.NET Core Middleware https://stu.dev 43

Slide 44

Slide 44

Performance! TechEmpower Benchmarks https://stu.dev 44

Slide 45

Slide 45

Performance! TechEmpower Benchmarks https://stu.dev 45

Slide 46

Slide 46

It’s a fullstack framework! ◉ View engines ○ DotLiquid ○ Razor ○ GiraffeViewEngine (HTML DSL) ◉ Much more https://stu.dev 46

Slide 47

Slide 47

A simple app https://stu.dev 47

Slide 48

Slide 48

Resources ◉ Documentation ◉ Dustin Moris Gorski - Functional ASP.NET Core ◉ Zaid Ajaj Giraffe helpers https://stu.dev 48

Slide 49

Slide 49

We use it! ◉ sse.co.uk https://stu.dev 49

Slide 50

Slide 50

What’s missing? ◉ Swagger / OpenAPI https://stu.dev 50

Slide 51

Slide 51

Summary ◉ Giraffe is middleware on steroids! ◉ F# is great https://stu.dev 51

Slide 52

Slide 52

Thanks! ◉ Find me at: stu.dev https://stu.dev 52