TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Scrap Your Cake Pattern Boilerplate: Dependency Injection Using the Reader Monad

21 pointsby handlerover 11 years ago

4 comments

noelwelshover 11 years ago
Clearest blog post I&#x27;ve read on the Reader monad in Scala. This is something I can see coming to our code base very soon.<p>The next step for us is monad transformers in Scalaz. We are going to end up with something like Future[Reader[Writer[Problem \&#x2F; Result]]] (a few type parameters are missing there; hopefully you get the idea). Monad transformers give a way to flatten this stack of monads into a single monad, which saves a lot of unnecessary wrapping and unwrapping in code. I haven&#x27;t yet worked out the all the details of using them, though. Should probably do a blog post when I do :-)
评论 #6629543 未加载
评论 #6629724 未加载
kilovoltaireover 11 years ago
Great use of the same example under three different dependency injection mechanisms.<p>One issue I see with the Reader pattern, however, is that anyone using one of these injected methods has to deal with the monad stuff of using for comprehensions to extract the values, et cetera, whereas with the other two patterns they get to use normal functions.<p>Put another way: having to distinguish between the &quot;normal&quot; functions in a class versus the injected Reader functions, and not be able to use them in the same way, feels like it would get old fast. I wonder if the author has found this annoying or not.<p>I do really like how purely functional the Reader approach is though, no state!
评论 #6629213 未加载
评论 #6629205 未加载
lmmover 11 years ago
It feels like this isn&#x27;t actually solving the dependency injection problem - we&#x27;ve moved all our dependencies into this global (ish) Config object, but that object is effectively acting as a service registry. Constructing the Config is still going to require a DI strategy (as the end of the post acknowledges), so what do we actually gain from this reader pattern? I don&#x27;t think it makes testing any easier (if anything it makes it harder, since we have to build up a whole dummy Config, rather than building a small test cake that only includes the dependencies a specific test needs).
评论 #6630303 未加载
paf31over 11 years ago
I&#x27;m not sure I understand the concern regarding the approach using implicits. Sure, you need to add an extra implicit method parameter to each method that requires the dependency, but in the reader approach, every same method needs to be written in the reader monad. In fact, they&#x27;re really the same concern, since the reader monad instance is just doing the work of threading what would have been implicit in the first place.
评论 #6630372 未加载