-
Notifications
You must be signed in to change notification settings - Fork 5
Basic concepts
How to represent queries in code?
Of course, as functions. Query parameters should be reflected as function parameters, their results as function results.
E.g. the query:
select id, name, title, description, owner, createdAt, modifiedAt, modifiedBy
from Blog
where id = @id
could be implemented as a function of type:
int -> DataContext-> Blog
SqlFun alows to generate such a function:
let getBlog: int -> DataContext -> Blog =
sql "select id, name, title, description, owner, createdAt, modifiedAt, modifiedBy
from Blog
where id = @id"
where sql
is a function responsible for building query functions using runtime code generation.
It generates:
- mappings between function parameters and query parameters
- mappings between function result and query result
- all needed type conversions
- command execution
But it doesn't cache generated function. Assigning to a variable should be enough.
A side-effect of code generation is a validation of sql commands and all necessary type checking (i.e. if function parameter types match query parameter types and if function and query results are compatible).
Couple of function can be grouped in a module:
module Blogging =
let getBlog: int -> DataContext -> Blog = ...
let getPosts: int -> DataContext -> Post list = ...
let getComments: int -> DataContext -> Comment list = ...
Since all module variables are evaluated during the first access to its contents, we some level of type safety for free.