[ANN] Component: dependency injection and state management

664 views
Skip to first unread message

Stuart Sierra

unread,
Nov 20, 2013, 9:01:19 PM11/20/13
to clo...@googlegroups.com
This is a small library/framework I've been working on for a few months.

https://github.com/stuartsierra/component

I use this to manage runtime state in combination with my "reloaded" workflow using tools.namespace.[1]

I've started using this on some personal and professional projects and it seems to be working fairly well.



Jan Herich

unread,
Nov 21, 2013, 6:16:09 AM11/21/13
to clo...@googlegroups.com
This is simple brilliant... The approach proposed and the component "framework" implementing it finally solves the issue with the "necessary evil" (start/stop interactions with statefull components in any bigger Clojure app) by cleverly taking only the best ideas (like using inferred dependency graph to automatically start/stop components in the correct order) from Java DI frameworks like Spring, which i have a lot of experience with. Thank you very much for this work.

Dňa štvrtok, 21. novembra 2013 3:01:19 UTC+1 Stuart Sierra napísal(-a):

abp

unread,
Nov 21, 2013, 7:22:10 AM11/21/13
to clo...@googlegroups.com
Hi, great work indeed.

One question though: Why do you prefer declaring dependencies between components of a system explicitly instead of using prismatics Graph?

Stuart Sierra

unread,
Nov 21, 2013, 9:14:16 AM11/21/13
to clo...@googlegroups.com
On Thursday, November 21, 2013 7:22:10 AM UTC-5, abp wrote:
> Why do you prefer declaring dependencies between
> components of a system explicitly instead of using
> prismatics Graph?

'Graph' by itself does not preserve the dependency
relationships after constructing the map. But the two
approaches are not incompatible: you can use 'Graph' to
construct the system map, then use 'Component' to manage it.

-S

Ben Mabey

unread,
Nov 21, 2013, 11:44:23 AM11/21/13
to clo...@googlegroups.com
If you are interesting in taking that approach you can use my
system-graph which does just that:

https://github.com/RedBrainLabs/system-graph

I spoke with Stuart at the conj about this library and I realized that
system-graph it is currently relying on an implementation detail in
order to work. I haven't had any issues with it but I can see places
where it will not work. I plan on fixing this issue so the proper
'Component' metadata is attached to the system-graph. When I do that
I'll release v0.2.0.

-Ben

Korny Sietsma

unread,
Jan 5, 2014, 1:23:43 PM1/5/14
to clo...@googlegroups.com
Hi - I've been playing with this and I'm a little confused.

I can understand how you use the library to pass around stateful components, and to start/stop them and wire them up etc.

But I'm not sure I see how it should be used for more general dependency injection.

I'll pick a concrete example - in the readme you have an ExampleComponent which calls "(get-user database :admin)"
the "get-user" function then gets the connection from the Database component - but it's still coupled to the particular implementation of "execute-query":

(defn get-user [database username]
  (execute-query (:connection database)
    "SELECT * FROM users WHERE username = ?"
    username))

If you wanted to unit test this function, you could pass whatever database connection thing you'd like - but you couldn't stub out the whole database, as execute-query is still coupled to the implementation of your database.  More generally, anything that calls "get-user" will need to provide some sort of working database, or mock/stub out the call to execute-query, or the call to get-user itself.

Is there something I'm missing?  Is there some way you could/would do this with the component library?  Or is this not the point of the library?

- Korny



--
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--
Kornelis Sietsma  korny at my surname dot com http://korny.info
.fnord { display: none !important; }

Stuart Sierra

unread,
Jan 5, 2014, 2:04:01 PM1/5/14
to clo...@googlegroups.com
Hi Korny,

Components implementing protocols can provide different implementations. In the Database example, both the "real" component and the "stub" component would have to implement some common protocol that defines the "primitive" capabilities needed by the application.

Traditional update-in-place databases are hard to mock, but I sometimes stub a remote Datomic database with a local in-memory version.

-S



You received this message because you are subscribed to a topic in the Google Groups "Clojure" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/clojure/jXnxVZaP2K4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to clojure+u...@googlegroups.com.

Korny Sietsma

unread,
Jan 5, 2014, 2:59:41 PM1/5/14
to clo...@googlegroups.com

Thanks - I'd kind-of worked that out after posting (with the aid of a walk outside, and a beer!) but it's nice to know I'm on the right track.

Reply all
Reply to author
Forward
0 new messages