Page 29 - MSDN Magazine, October 2019
P. 29
F#
SAFE Stack: Functional
Web Programming for
.NET Core
Isaac Abraham
As a longtime .NET developer who’s spent years in the C# world, I was used to working with shrink-wrapped Web frameworks designed for C#, such as the ubiquitous ASP.NET MVC and Web API frameworks.
When I started using F# a few years ago, I gained access to a flexible, powerful and pragmatic language that ran on a framework I already knew, and opened the door to a much more open and dynamic ecosystem than I was used to. However, working with Web applications was an uncomfortable experience: The server side meant either using ASP.NET, and trying to shoehorn F# into an object-oriented (OO) framework—or giving up ASP.NET completely and using a totally different Web server. Meanwhile, working on the client invariably meant using JavaScript, throwing the type system away completely and needing to upskill to deal
with its unique “features.” Moreover, deploying F# applications into Azure had no support from Microsoft and little or no docu- mentation on how to do this yourself.
The SAFE Stack changes all that. This open source stack allows you to write F# Web apps without needing to fit your code into an OO framework or library or require you to be an expert in CSS or HTML to create rich, compelling client-side Web applications. The default SAFE stack is made up of the following four components:
• Saturn: A lightweight F# library that sits on top of ASP.NET Core to provide a high-performance and reliable Web server.
• Azure: Microsoft’s cloud platform offering, used for hosting and platform services.
• Fable: A library that allows seamless interop between F# and JavaScript in the browser.
• Elmish: A library that allows you to write UIs that fit into functional programming (FP) best practices rather than, for example, MVVM or MVC.
Let's start by looking at the SAFE stack from a programming model point of view, looking at the three core libraries.
Why F#?
Your first reaction on reading this might be: “Why bother with F#?” Or, perhaps, “F# for Web programming? I thought F# was just for math and science!” The truth is, F# is a general-purpose pro- gramming language that runs on .NET and .NET Core—just like C#—and can work with the entire NuGet ecosystem of libraries, with excellent interop with C#.
This article discusses:
• Functional programming basics • SAFE Stack components
• Web development
• Full stack applications Technologies discussed:
F#, SAFE Stack, .NET Core
Code download available at:
safe-stack.github.io/docs
October 2019 25