Skip to content

Commit

Permalink
Fix return type in DbContextHelpers.fs
Browse files Browse the repository at this point in the history
  • Loading branch information
simon-reynolds committed Jun 5, 2022
1 parent 01a62e9 commit bd06b49
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions src/EFCore.FSharp/DbContextHelpers.fs
Original file line number Diff line number Diff line change
Expand Up @@ -20,43 +20,56 @@ let private transform (a: obj) =
|> Composite
| _ -> Single a

let findEntity<'a when 'a: not struct> (ctx: DbContext) (key: obj) =
let findEntity<'a when 'a: not struct> (ctx: DbContext) (key: obj) : 'a =

match transform key with
| Composite k -> ctx.Set<'a>().Find(k)
| Single k -> ctx.Set<'a>().Find(k)

let tryFindEntity<'a when 'a: not struct> (ctx: DbContext) (key: obj) =
let tryFindEntity<'a when 'a: not struct> (ctx: DbContext) (key: obj) : 'a option =
let result = findEntity<'a> ctx key

Option.ofObj (box result)
if isNull (box result) then
None
else
Some result

let findEntityAsync<'a when 'a: not struct> (ctx: DbContext) (key: obj) =
let findEntityAsync<'a when 'a: not struct> (ctx: DbContext) (key: obj) : Async<'a> =
let f =
match transform key with
| Composite k -> ctx.Set<'a>().FindAsync(k)
| Single k -> ctx.Set<'a>().FindAsync(k)

async { return! awaitValueTask f }

let findEntityTaskAsync<'a when 'a: not struct> (ctx: DbContext) (key: obj) =
let findEntityTaskAsync<'a when 'a: not struct> (ctx: DbContext) (key: obj) : ValueTask<'a> =
match transform key with
| Composite k -> ctx.Set<'a>().FindAsync(k)
| Single k -> ctx.Set<'a>().FindAsync(k)

let tryFindEntityAsync<'a when 'a: not struct> (ctx: DbContext) (key: obj) =
let tryFindEntityAsync<'a when 'a: not struct> (ctx: DbContext) (key: obj) : Async<'a option> =
async {
let! result = findEntityAsync<'a> ctx key

return Option.ofObj (box result)
let result' =
if isNull (box result) then
None
else
Some result

return result'
}

let tryFindEntityTaskAsync<'a when 'a: not struct> (ctx: DbContext) (key: obj) =
let tryFindEntityTaskAsync<'a when 'a: not struct> (ctx: DbContext) (key: obj) : Task<'a option> =
let result = findEntityTaskAsync<'a> ctx key

result
.AsTask()
.ContinueWith(fun (t: Task<'a>) -> Option.ofObj (box t.Result))
.ContinueWith(fun (t: Task<'a>) ->
if isNull (box t.Result) then
None
else
Some t.Result)


/// Helper method for saving an updated record type
Expand Down

0 comments on commit bd06b49

Please sign in to comment.