An F# type provider is a component that provides types, properties, and methods for use in your program. Type Providers generate what are known as Provided Types, which are generated by the F# compiler and are based on an external data source.
For example, an F# Type Provider for SQL can generate types representing tables and columns in a relational database. In fact, this is what the SQLProvider Type Provider does.
Provided Types depend on input parameters to a Type Provider. Such input can be a sample data source (such as a JSON schema file), a URL pointing directly to an external service, or a connection string to a data source. A Type Provider can also ensure that groups of types are only expanded on demand; that is, they are expanded if the types are actually referenced by your program. This allows for the direct, on-demand integration of large-scale information spaces such as online data markets in a strongly typed way.
Generative and Erased Type Providers
Type Providers come in two forms: Generative and Erased.
Generative Type Providers produce types that can be written as .NET types into the assembly in which they are produced. This allows them to be consumed from code in other assemblies. This means that the typed representation of the data source must generally be one that is feasible to represent with .NET types.
Erasing Type Providers produce types that can only be consumed in the assembly or project they are generated from. The types are ephemeral; that is, they are not written into an assembly and cannot be consumed by code in other assemblies. They can contain delayed members, allowing you to use provided types from a potentially infinite information space. They are useful for using a small subset of a large and interconnected data source.
Commonly used Type Providers
The following widely-used libraries contain Type Providers for different uses:
- FSharp.Data includes Type Providers for JSON, XML, CSV, and HTML document formats and resources.
- SQLProvider provides strongly typed access to relation databases through object mapping and F# LINQ queries against these data sources.
- FSharp.Data.SqlClient has a set of type providers for compile-time checked embedding of T-SQL in F#.
- Azure Storage Type provider provides types for Azure Blobs, Tables, and Queues, allowing you to access these resources without needing to specify resource names as strings throughout your program.
- FSharp.Data.GraphQL contains the GraphQLProvider, which provides types based on a GraphQL server specified by URL.
Where necessary, you can create your own custom type providers, or reference type providers that have been created by others. For example, assume your organization has a data service providing a large and growing number of named data sets, each with its own stable data schema. You may choose to create a type provider that reads the schemas and presents the latest available data sets to the programmer in a strongly typed way.