Last active
December 8, 2020 13:46
-
-
Save isaacabraham/14991536ca55bf3c548eed40bd308178 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #r "nuget:WindowsAzure.Storage" | |
| open Microsoft.WindowsAzure.Storage.Table | |
| type Isaac = { Name : string; Age : int } | |
| let typeCache = System.Collections.Generic.Dictionary() | |
| let addToCache<'T>() = | |
| let theType = typeof<'T> | |
| let ctor = theType.GetConstructors().[0] | |
| let parameters = ctor.GetParameters() | |
| typeCache.Add(theType, {| Ctor = ctor; Parameters = parameters |}) | |
| let getOrAddFromCache<'T>() = | |
| match typeCache.TryGetValue typeof<'T> with | |
| | true, cache -> cache | |
| | false, _ -> addToCache<'T>(); typeCache.[typeof<'T>] | |
| let buildRecordFromEntity<'T> (entity:DynamicTableEntity) = | |
| let cache = getOrAddFromCache<'T>() | |
| let args = [| | |
| for p in cache.Parameters do | |
| entity.Properties.[p.Name].PropertyAsObject | |
| |] | |
| cache.Ctor.Invoke(args) :?> 'T | |
| let buildRecordFromEntityNoCache<'T> (entity:DynamicTableEntity) = | |
| let theType = typeof<'T> | |
| let ctor = theType.GetConstructors().[0] | |
| let parameters = ctor.GetParameters() | |
| let args = [| | |
| for p in parameters do | |
| entity.Properties.[p.Name].PropertyAsObject | |
| |] | |
| ctor.Invoke(args) :?> 'T | |
| let isaacTable = | |
| DynamicTableEntity("PK1", "RK1", "", dict [ "name", EntityProperty.GeneratePropertyForString "Isaac"; "age", EntityProperty.GeneratePropertyForInt 41 ]) | |
| isaacTable |> buildRecordFromEntityNoCache<Isaac> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment