One of the most interesting things about F# is their type providers. Don Syme, just a couple of years ago, did a post giving examples of using 12 type providers that is still quite relevant. Just taking a look at these examples you can see how easy it is to get started with them when you need to get data.

Is it really that simple, though? Definitely! In fact, it’s so simple I’ll even use an XML or SQL type provider to spin up a quick F# script to do some debugging on the data itself to make sure what I’m receiving is what I expect.

I actually used the CSV type provider back in the data science post. Let’s take a look again at how simple it was to use.

let deliveryData = CsvProvider<"delivery_data.csv">.GetSample()

Just that one line and you can start using your data right away.


That’s way easier and much quicker than doing a lot of code to open the file and parse it, right?

Now imagine you need to create a data access layer. You could use Entity Framework or another ORM but that generates a lot of extra code. Using the SQL data type provider you can skip all of that and get instant access to your database.


We can also query the database directly.

let q = query { for c in context.Categories do
select c.CategoryName }

q |> Seq.toList

And we have our results from the F# Interactive:

val it : string list =
["Beverages"; "Condiments"; "Confections"; "Dairy Products";
"Grains/Cereals"; "Meat/Poultry"; "Produce"; "Seafood"]

One of the problems with traditional data access layers, though, is if your database schema changes then you won’t know if any of your code breaks because of that change until runtime. However, as this MSDN post shows in more detail, these types of discrepancies can be found at compile time. This way the developer has instant notification of these issues and can instantly resolve them there.

So, if we have the previous query and our schema gets changed by changing the spelling or capitalization, then we would get a compile error telling us that. And with a quick change we can fix it up very easily.

Hopefully this post has helped show not only how powerful F# type providers can be, but how much more productive you can be just by using them. Whether you need data from a file, a database, or from a third party service getting that data becomes much easier with type providers.