DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» is a community of 966,904 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
Cover image for Web development in F#
Samuele Resca
Samuele Resca

Posted on

Web development in F#

Originally posted on https://samueleresca.net

Last year I started to go in deep of functional programming world. Indeed, for the majority of my time I build web stuff: web apps and APIs, mostly on the .NET stack and sometimes on Node.js stack.

So, since I started to face with functional, my desire was to apply functional programming on the web architectures. This article groups someΒ information, frameworks and best practices regards web development in F#.

Since this article is only an introduction, I also suggest:Β Β Build web service using F# and ASP.NET Core

Why functional programming in web world ?

Why use functional approach in web applications?Β  Functional comes with some benefits.

First of all, the concise and declarative syntax may be useful to solve problems and implement complex problems. On opposite of a imperative language, it has a declarative style focused on results, in consequence,Β you write code that describes what you want.

F# also reduces accidental complexity. The accidental complexity is the complexity that we inflict upon ourselves through the tools that we use. On the other side, the essential complexity is the actual complexity which affects a particular problem. That software engineering theory is called No Silver bullet.

Finally, it has also an excellent integration with C# and .NET ecosystem.

Functional web frameworks

Let's talk about some different functional web frameworks in F#.

Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  We have good experience with @SuaveIO (on .NET Core), but Giraffe is also great alternative. For SQL server, definitely try SQLProvider library.

β€” Roman ProvaznΓ­k (@RProvaznik) 27 marzo 2018

Evergreen names are: SuaveIO and Giraffe.

Both of them can run on .NET Core, and both provides some minimal feature and support over HTTP protocol and they provide full control over the flow of the program because both of them act on HTTP using a low-level approach.

Saturn

On the other side, some times an high-level approach on F# stack is preferred. Krzysztof Cieslack has recently introduced Saturn, a new F# web framework which stands on top ofΒ Giraffe.

It implements MVC pattern which is absolutely more familiar in web development.

ASP.NET project

As said before, F# can work efficiently with the .NET ecosystem.

fsharp.orgΒ offers some template projects in order to build web application in F#. You can find them on the Online templates section ofΒ  Visual studio "New project" dialog.

In addition, we may also consider to mix C# and F# projects in a single solution. The C# projects may contain all the client-side stuff, plus the Razor views. On the other side, the F# project contains all the models, controller and logics of the web application. That approach could be useful in order to gradually introduce F# inside your development team.

What about client-side?

FableΒ brings F# on Javascript ecosystem.

It uses popular technologies under the hood like Babel and Webpack, therefore you don't loose all capabilities and advanced features derived from them.

SAFE stack

Recently, I also heard about SAFE stack:

SAFE is a technology stack that brings together several technologies into a single, coherent stack for type-safe, flexible, web-enabled applications that are written wherever possible in F#, thus allowing you to reuse your existing skill set as much as possible.

You can listen a cool presentation of the stack on the Scott Hanselman podcast: F# and the functional SAFE Stack with Krzysztof CieΕ›lak, SAFE stack combines different technologies:

  • Suave.IO covers the web server functionalities;
  • Fable brings all the power of F# to the JavaScript ecosystem;
  • ElmishΒ implements core abstractions that can be used to build Fable applications following the β€œmodel view update”;

You can find an concrete sample of the power of SAFE stack on github.

Final thoughts

In conclusion, the following article exposes the current situation of web development in F#. Since F# was born in an open ecosystem, also before of ASP.NET Core, it offers a lot of choices in terms of framework and tools. If you are interested to build web service using Giraffe, I suggest you the following article:Β Β Build web service using F# and ASP.NET Core

Cover photo:Β Kunsthaus Graz art museum

Top comments (0)

🌚 Life is too short to browse without dark mode