Clarity on Stuart Sierra's component system

372 views
Skip to first unread message

Colin Yates

unread,
Nov 28, 2014, 5:28:45 AM11/28/14
to clo...@googlegroups.com
Hi all,

Am I right in thinking that in order to use https://github.com/stuartsierra/component every consumer of a component must also be a component?

For example, if I have a component DB and I want to use that DB in (defn blob-query [db criteria]...), do I pull the DB out of the system map and call it or am I expected to make a BlobQuery component which offers a (blob-query [criteria]) API?

Initially I thought the "system" was just the stateful components, but after watching https://www.youtube.com/watch?v=13cmHf_kt-Q and reading the doc, particularly the "all or nothing" warnings I think I might have missed something.

Thanks!

Max Countryman

unread,
Nov 28, 2014, 9:55:07 AM11/28/14
to clo...@googlegroups.com
Hi Colin,

I'm by no means an expert on Component but I have used it for my last several projects. What I've discovered is where I have query functions such as yours I pass the db component through to their call sites from a component which contains the db as state. For example, I have a component which contains various HTTP routing logic. Some of my routes result in db queries. To facilitate this I pass the db all the way through. It's sometimes a little clunky and there may be better ways of doing this, e.g. maybe all the routes which depended on db state could be partitioned in their own component. 

I hope that's helpful,


Max
--
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/d/optout.

James Reeves

unread,
Nov 28, 2014, 10:00:37 AM11/28/14
to clo...@googlegroups.com
On 28 November 2014 at 10:28, Colin Yates <colin...@gmail.com> wrote:
Am I right in thinking that in order to use https://github.com/stuartsierra/component every consumer of a component must also be a component?

Nope.
 
For example, if I have a component DB and I want to use that DB in (defn blob-query [db criteria]...), do I pull the DB out of the system map and call it or am I expected to make a BlobQuery component which offers a (blob-query [criteria]) API?

You only need to make something a component if it has a lifecycle. If you just want to use a database connection, pass the component as an argument to a function.

Look at it another way: do you need to keep blob-query around? Does it have state? If not, then it doesn't need to be a component.

- James

Colin Yates

unread,
Nov 28, 2014, 10:20:19 AM11/28/14
to clo...@googlegroups.com
Thanks both. James, that is what I was hoping.

I guess I got a bit misled with the combination of his "Customers"
example in the video, the "all or nothing" warnings and "I do not
intend that application functions should receive the top-level system
as an argument. Rather, functions are defined in terms of components.
" (from the front page on github).

I guess we can just (:interesting-component system) once the system has started?

(Without writing a single line of code though, I can see the
temptation to make stateless consumers Components just for the ease of
use :)).
> --
> 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 a topic in the
> Google Groups "Clojure" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/clojure/WC983q33Nes/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to

James Reeves

unread,
Nov 28, 2014, 10:55:14 AM11/28/14
to clo...@googlegroups.com
On 28 November 2014 at 15:20, Colin Yates <colin...@gmail.com> wrote:
I guess I got a bit misled with the combination of his "Customers"
example in the video, the "all or nothing" warnings and "I do not
intend that application functions should receive the top-level system
as an argument. Rather, functions are defined in terms of components.
" (from the front page on github).

I guess we can just (:interesting-component system) once the system has started?

Your system should call your functions, not the other way around.

You can have functions that are not components, but all functions should be called from a component. The direction of the program should flow down from the system to its components, then down into individual functions.

- James

Colin Yates

unread,
Nov 28, 2014, 11:03:53 AM11/28/14
to clo...@googlegroups.com
Ah OK. I was confused a little about what makes a component, it isn't
just state it is also about lifecycle.

Imagine a "health-check" which polls some service to see if it is
there, it needs the service and some ohDear notifier component but it
doesn't have any state as such (or at least no state that changes over
time), but it does have a notion of start/stopping. This would be a
good candidate because it has a lifecycle.

Thanks again.

James Reeves

unread,
Nov 28, 2014, 11:40:00 AM11/28/14
to clo...@googlegroups.com
On 28 November 2014 at 16:02, Colin Yates <colin...@gmail.com> wrote:
Ah OK. I was confused a little about what makes a component, it isn't
just state it is also about lifecycle.

Imagine a "health-check" which polls some service to see if it is
there, it needs the service and some ohDear notifier component but it
doesn't have any state as such (or at least no state that changes over
time), but it does have a notion of start/stopping. This would be a
good candidate because it has a lifecycle.

Exactly right.

- James

Daniel Szmulewicz

unread,
Dec 5, 2014, 7:18:00 PM12/5/14
to clo...@googlegroups.com
You may also want to have a look at the system library. It ships with readymade components, and it contains a full application example (if minimal) to show how things work. 

Colin Yates

unread,
Dec 5, 2014, 7:19:21 PM12/5/14
to clo...@googlegroups.com

Thanks Daniel.

--
Reply all
Reply to author
Forward
0 new messages