简体   繁体   English

F#类型提供者用例

[英]F# type provider use case

I have a bit of hard time to get a grip on type providers. 我有点难以掌握类型提供商。 I would like to get some feedback on the usefulness of type provider approach for following use case. 我希望得到一些关于类型提供者方法对以下用例的有用性的反馈。

Quite unfortunately our telemetry API returns objects as List<Dictionary<string, object>> . 很遗憾,我们的遥测API将对象返回为List<Dictionary<string, object>> Dictionary has strings for keys (meaning column names) and value can be any object (though, usually this is some value type in System namespace). Dictionary包含键的字符串(表示列名),value可以是任何对象(但通常这是System命名空间中的某些值类型)。 Also, there is an method that returns Dictionary<string, Type> collection that represents the schema (column name -> type binding). 此外,还有一个方法返回表示架构的Dictionary<string, Type>集合(列名称 - >类型绑定)。 We usually use this data for ad-hoc/exploratory analysis and reporting. 我们通常将此数据用于临时/探索性分析和报告。

F# with R type provider seems to be a great weapon for aforementioned analysis. 带有R型提供者的F#似乎是上述分析的一个很好的武器。 Just accessing data by typing column names as strings and casting objects really stands in the way. 只是通过键入字符串作为字符串和转换对象来访问数据实际上就是阻碍了。 I would like to create a type provider that will from given dictionary (connection string that creates the dictionary) create strong types. 我想创建一个类型提供程序,它将从给定的字典(创建字典的连接字符串)创建强类型。 Eg from 例如来自

new List<Dictionary<string, object>> 
{ 
    new Dictionary<string, object> { {"a", 1}, {"b","data"}, {"c", DateTime.UtcNow } },
    new Dictionary<string, object> { {"a", 3}, {"b","data2"}, {"c", DateTime.UtcNow } }
}

I would like to get 我想得到

type MyObject
    member a : int
    member b : string
    member c : DateTime

List<MyObject> ...

Is this an appropriate use of type providers? 这是类型提供者的适当使用吗? As I said I am quite new to the field so any feedback would be appreciated. 正如我所说,我对这个领域很陌生,所以任何反馈都会受到赞赏。 Furthermore if you have any examples of similar problems I could use for quickly getting from the ground please share (Csv type provider seems to be good starting point). 此外,如果您有任何类似问题的例子,我可以用来快速从地面开始请分享(Csv类型提供商似乎是一个很好的起点)。

If typed access to the telemetry API is useful, there are 2 easy approaches: 如果对遥测API的类型访问很有用,有两种简单的方法:

  1. Code generation 代码生成
  2. Type Providers 类型提供商

A Type Provider is particularly appropriate if: 在以下情况下,类型提供商特别合适

  • the data changes frequently 数据经常变化
  • the telemetry data is large 遥测数据很大

With a Type Provider the API can be checked at a specific interval, eg each time an application is compiled. 使用类型提供程序,可以以特定间隔检查API,例如,每次编译应用程序时。 Type Providers can be lazy so that only code is generated for the data that is consumed, so if the data is large it would also be a more appropriate choice. 类型提供程序可以是惰性的,因此只为所使用的数据生成代码,因此如果数据很大,那么它也是更合适的选择。

Michael Newton has a great introduction to writing Type Providers: Type Providers from the Ground Up 迈克尔·牛顿(Michael Newton)对编写类型提供者: 类型提供者从一开始就有很好的介绍

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM