[web-devel] An idea for Yesod, feedback requested

Michael Snoyman michael at snoyman.com
Wed Jun 30 15:06:01 EDT 2010

I'm adding some support right now to Yesod (for version 0.4.0) to support
some operations at the beginning of each handler. In particular, the
onRequest and isAuthorized functions will now both run inside the Handler
monad for each request, giving them full access to all data available there.
In addition, the defaultLayout and errorHandler functions both live in the
Handler monad.

I have an idea to add a per-request piece of data which would be application
defined. As a motivating example, a common task would be to check if a user
is logged in (by checking the user session), and if he/she is logged in,
grab some extra data from the database, such as display name. My idea would
use type families to store that kind of information inside the Handler.

One benefit is encapsulating this database loading code in one location;
however, that could easily be accomplished by just writing a function to do
it for you, and  calling that function from all your handlers. However,
there's a much larger benefit: let's say that you want information on the
display name in both the applyLayout function and your handler function;
currently, you'd need to do two database lookups. Now, you'd just need one.

There are two downsides I can see. The first is that this kind of loading
code will be performed at each request, even if it's not used. I'm not as
worried about that issue, since most of the time it's data that will be used
in the applyLayout function, which is called on almost every request.

The second issue is a code style issue: since there seems to be no way to
declare default type synonym instances, I'd need to add to more required
entries into the Yesod typeclass: the type synonym instances, and the loader
function. Currently, the only required function in the Yesod typeclass is
approot, and I'm particularly worried about adding some lines requiring the
usage of type families, as many users are unfamiliar with them.

A possible method for mitigating this issue is to include a scaffolding tool
with Yesod that will string up a default site. That's probably something
I'll end up doing anyway.

Appreciate any user feedback!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/web-devel/attachments/20100630/79cfec46/attachment.html

More information about the web-devel mailing list