Skip to content

Instantly share code, notes, and snippets.

@dmitry-a-morozov
Last active August 3, 2016 21:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmitry-a-morozov/fd50e5f65b2da3fa49257b7ee4a4fca2 to your computer and use it in GitHub Desktop.
Save dmitry-a-morozov/fd50e5f65b2da3fa49257b7ee4a4fca2 to your computer and use it in GitHub Desktop.
type provider in 20 lines
namespace FSharp.IO.DesignTime
open System.Reflection
open System.IO
open Microsoft.FSharp.Core.CompilerServices
open ProviderImplementation.ProvidedTypes
[<assembly:TypeProviderAssembly()>]
do()
#nowarn "0025"
[<TypeProvider>]
type public FileReaderProvider(config : TypeProviderConfig) as this =
inherit TypeProviderForNamespaces()
let nameSpace = "FSharp.IO"
let assembly = Assembly.LoadFrom( config.RuntimeAssembly)
let providerType = ProvidedTypeDefinition(assembly, nameSpace, "FileReader", baseType = None, HideObjectMethods = true)
do
providerType.DefineStaticParameters(
parameters = [ ProvidedStaticParameter("Path", typeof<string>) ],
instantiationFunction = fun typeName [| :? string as path |] ->
let t = ProvidedTypeDefinition(assembly, nameSpace, typeName, baseType = Some typeof<obj>, HideObjectMethods = true)
let fullPath = if Path.IsPathRooted(path) then path else Path.Combine(config.ResolutionFolder, path)
let content = File.ReadAllText( fullPath)
t.AddMember <| ProvidedLiteralField("Text", typeof<string>, content)
t
)
this.AddNamespace( nameSpace, [ providerType ])
#r @"bin\Debug\SqlFileTypeProvider.dll"
#r @"packages\FSharp.Data.SqlClient.1.8.2\lib\net40\FSharp.Data.SqlClient.dll"
open FSharp.IO
open FSharp.Data
[<Literal>]
let sourceDirectory = __SOURCE_DIRECTORY__
type Get42Query = FileReader<"Get42.sql">
Get42Query.Text
[<Literal>]
let connection = "server=.;trusted_connection=true"
do
use cmd = new SqlCommandProvider< Get42Query.Text, ConnectionStringOrName = connection, SingleRow = true>(connection)
cmd.Execute() |> printfn "%A"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment