3

I'm working on a demo where I can show how easy it is to use the SqlClient type provider in a F# library and then consume it from C#, I think this might be one way to get things into projects. Since many people using C# want to use interfaces etc. I thought I show them that it is possible to combine everything, but I don't get the expected result. I have the following F# code which works as expected in F#:

module Orders = 
    [<Literal>]
    let connStr = """XXXXXXXXXXXX"""
    type OrdersPerEmployee = SqlCommandProvider<"OrdersPerEmployee.sql", connStr>

open Orders
type IOrdersRepo =
    abstract member getOrdersPerEmployee : int -> OrdersPerEmployee.Record seq
type OrdersRepo() =
    interface IOrdersRepo with
        member this.getOrdersPerEmployee(employeeId) = 
            let query = new OrdersPerEmployee()
            query.Execute(employeeId)

But when I try to use it from my C# app I don't get that the getOrdersPerEmployee returns an enumerable of records, instead it returns an enumerable of objects:

IOrdersRepo repo = new OrdersRepo();
var data = repo.getOrdersPerEmployee(4).ToList();
data.ForEach(y => Console.WriteLine(y));
Console.ReadLine();

In the code above I expceted to get intellisense on y in the lambda, but nothing. Any ideas?

ildjarn
  • 62,044
  • 9
  • 127
  • 211
Tomas Jansson
  • 22,767
  • 13
  • 83
  • 137

1 Answers1

3

Because SqlCommandProvider is erasing types kind. It means types it generates can be consumed only from F#. Contrary, SqlEnumProvider from the same FSharp.Data.SqlClient library is generated types kind and can be consumed by C# (via proxy project). It works especially nice when CLIEnum=true. For a deeper discussion on erased vs generated types see How do I create an F# Type Provider that can be used from C#?

Community
  • 1
  • 1
  • 1
    See a more detailed answer from [Tomas Petricek](http://stackoverflow.com/users/33518/tomas-petricek) here: http://stackoverflow.com/a/12118966/433393 – Chester Husk Jun 09 '15 at 21:43
  • Thank you. I thought I had an earlier example where I did consume it directly, but I guess my memory is failing me. However, that would have been a nice feature since it would make it even easier to use in a C# project. That way one can sneak F# in :). Of course you can do it now as well, but then you have to do an extra mapping before using it in C#. That is fine, but a little more work. – Tomas Jansson Jun 10 '15 at 06:01
  • @TomasJansson Unfortunately this is a LOT more work to change from erased types to generated types. – Dmitry Morozov Jun 10 '15 at 16:04
  • That's what I read too and totally understand it hasn't been prioritized, with open source stuff you only have so much time. – Tomas Jansson Jun 11 '15 at 05:27