F#类型提供用例 [英] F# type provider use case

查看:124
本文介绍了F#类型提供用例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一点很难得到的类型提供一握。我想获得的类型提供方法的有效性一些反馈为后续的用例。

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.

颇为遗憾的是我们的遥测API返回的对象为名单,其中,字典<字符串,对象>> 词典具有键(意为列名)字符串和值可以是任何对象(虽然,通常这是系统命名空间中一些价值型)。此外,还有一个方法,它返回词典<字符串类型> 的集合,再presents模式(列名 - >输入绑定)。我们通常使用此数据即席/探索性分析和报告。

Quite unfortunately our telemetry API returns objects as 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). Also, there is an method that returns Dictionary<string, Type> collection that represents the schema (column name -> type binding). We usually use this data for ad-hoc/exploratory analysis and reporting.

F#与R型供应商似乎是上述分析的有力武器。只需键入列名字符串和转换对象访问数据真正突出的方式。我想创建一个类型的供应商,将从给定字典(创建字典连接字符串)创建强类型。 例如。从

F# with R type provider seems to be a great weapon for aforementioned analysis. 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. E.g. 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 } }
}

我想获得

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

List<MyObject> ...

这是一个合适的利用方式供应商?正如我所说的,我很新的领域,因此任何反馈意见,将AP preciated。此外,如果你有类似的问题,任何的例子我可以使用从地面迅速获得欢迎交流(CSV类型的供应商似乎是很好的起点)。

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).

推荐答案

如果遥测API类型访问是非常有用的,有2个简单的方法:

If typed access to the telemetry API is useful, there are 2 easy approaches:

  1. code一代
  2. 类型提供

一个类型提供特别适合,如果:

A Type Provider is particularly appropriate if:

  • 数据变化频繁
  • 遥测数据量大

通过一个类型提供的API,可以在特定的时间间隔进行检查,例如:每次应用程序被编译。类型提供商可以偷懒,这样针对所使用的数据只产生code,因此如果数据量大,也将是一个比较合适的选择。

With a Type Provider the API can be checked at a specific interval, e.g. each time an application is compiled. 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

这篇关于F#类型提供用例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆