-
-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
A more functional way of including related documents #31
Comments
Maybe something like this? let includeSingle<'entity, 'included> (selector: Quotations.Expr<'entity -> obj>) (q: IQueryable<'entity>) =
let mutable single: 'included = Unchecked.defaultof<_>
let q = q.Include(selector |> Lambda.ofArity1, (fun x -> single <- x))
q, single // ???
// includeList
// includeDictionary |
I think that's fine. However you do have to force the query to start somehow (their example uses If this is required for strictly building our a query pipeline, maybe |
My original idea was after you call EDIT: Another option is to simply create wrapper functions for let includeSingle<'a, 'b> (selector: Quotations.Expr<'a -> obj>) (action: 'b -> unit) (q: IQueryable<'a>) =
q.Include(selector |> Lambda.ofArity1, action)
let includeList<'a, 'b>
(selector: Quotations.Expr<'a -> obj>)
(seq: Collections.Generic.IList<'b>)
(q: IQueryable<'a>)
=
q.Include(selector |> Lambda.ofArity1, seq)
let includeDict<'a, 'b, 'c>
(selector: Quotations.Expr<'a -> obj>)
(dict: Collections.Generic.Dictionary<'b, 'c>)
(q: IQueryable<'a>)
=
q.Include(selector |> Lambda.ofArity1, dict)
// example (not tested)
let authors = Collections.Generic.Dictionary<Guid, Author>()
let! books =
session
|> Session.query
|> includeDict (<@ fun book -> book.AuthorId @>) authors
|> orderByDescending <@ fun book -> book.Published @>
|> toListTask A pretty wild option would be to have a let includeSingle<'a, 'b, 'c>
(selector: Quotations.Expr<'a -> obj>)
(continueWith: IQueryable<'a> -> 'c)
(q: IQueryable<'a>)
=
let mutable value: 'b = Unchecked.defaultof<_>
let q = q.Include(selector |> Lambda.ofArity1, (fun x -> value <- x))
let result = continueWith q
result, value
// example (not tested)
let queryPost queryable =
queryable |> Queryable.exactlyOne
let (post, author) : Post * Author =
session.Query<Post>
|> includeSingle <@ fun post -> post.AuthorId @> queryPost ^^ The ergonomics of continueWith is rough especially when you consider multiple includes. |
As you can see here including related documents requires a separate mutable binding.
I wonder if there's a way to accomplish the same effect while giving the consumer a tuple value of
'entity * 'included
(or something similar)
The text was updated successfully, but these errors were encountered: