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.
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.
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?
It uses popular technologies under the hood like Babel and Webpack, therefore you don’t loose all capabilities and advanced features derived from them.
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;
- 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.
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