component

2016-04-01T21:02:42.000013Z

Hi, I'm a little confused about component. How do you actually write business logic with this? I mean functions that are not start or stop. I can see two ways, one is requiring the global system god map from every place that does business logic and then deconstructing the map so you can access the components. The other way to access a component is to put it into a protocol and implement that in the record. But is there no way to do it via a simple defn that you export from your file? Yet in the [documentation](https://github.com/stuartsierra/component#creating-components) there is this (defn get-user [database username] ...) example, as if you just had access to database. What am I missing?

roberto 2016-04-01T21:18:06.000019Z

Component lives in the edges, it is basically a wrapper for your … well…. components

roberto 2016-04-01T21:18:14.000020Z

everything else should be normal clojure functions

roberto 2016-04-01T21:18:46.000021Z

so, your business logic, should not be in a component’s function

stuartsierra 2016-04-01T21:32:05.000024Z

In that documentation example, database is an instance of a component.

2016-04-01T21:32:15.000025Z

Thanks @roberto and @sveri. I appreciate the examples especially, that is what I need to study.

stuartsierra 2016-04-01T21:34:08.000026Z

Components are just there to help you set up and tear down stateful things. After that, you're just passing arguments to functions.

stuartsierra 2016-04-01T21:38:04.000027Z

Typically there's an "entry point" somewhere — a main or a request handler — that kicks off your business logic with a function call, passing it a component that has dependencies on the things it needs.

2016-04-01T21:38:11.000028Z

@stuartsierra: I understand that database is a component, but after you put everything together in your system map, how do you get things out again? I mean the references to the various components are only accessible from the system map (where they were created), so if you want to call e.g. get-user, you have to do (get-user (:db my-system-map) username) and you have to pass the system-map around from your core file where you created it correct?

stuartsierra 2016-04-01T21:38:21.000029Z

no

stuartsierra 2016-04-01T21:38:53.000030Z

The 'component' library doesn't dictate how you store or access the system. This is a common point of confusion, I've found.

stuartsierra 2016-04-01T21:40:38.000031Z

Typically, I have a main function that creates the system, starts it, then hands off control to one or more components, like a web server or an input loop.

stuartsierra 2016-04-01T21:41:06.000032Z

For interactive development using the REPL, I store the system map in a Var in a dev namespace so I can examine it.

stuartsierra 2016-04-01T21:41:47.000033Z

I never pass the entire system map as an argument to any function (except for debugging at the REPL).

2
2016-04-01T21:49:06.000034Z

Ok, thanks for all the suggestions, and it's good to know I'm not the only one, I'm starting to feel a little embarrassed. It's getting late here now, so that's probably not helping. I hope to get the hang of it tomorrow. Thanks again!

roberto 2016-04-01T21:53:40.000035Z

Don’t feel too bad. I personally had to carve out an afternoon to sit down and grasp it. I’m glad I took the time to do that.

stuartsierra 2016-04-01T21:55:39.000036Z

I've seen a lot of people struggle with this. Is there something fundamental missing from the documentation?

2016-04-01T21:57:04.000037Z

Thanks @roberto! Umm... see you probably tomorrow. 😉

roberto 2016-04-01T21:57:26.000038Z

It is hard to articulate once I’ve grasped it. :simple_smile:

roberto 2016-04-01T21:57:59.000039Z

I think one of the issues I had was that I tried to learn it early on, while I was still learning clojure, and a bunch of libraries like ring

2016-04-01T21:58:19.000040Z

@stuartsierra: I really don't know, tomorrow once I understand it I think I should draw up a map of my (non-)understanding before and after.

roberto 2016-04-01T21:58:40.000041Z

I have to say, that just by looking at the vide from Clojure West 2014, and following the readme, I was able to understand it. (With a scrapbook at hand and a repl)

roberto 2016-04-01T21:59:59.000043Z

Something I’ve been thinking about that might help to onboard newcomers to a library is to do some node school type tutorials.

roberto 2016-04-01T22:00:46.000044Z

I’ve been thinking about doing something like this for keechma, but again, I’m that situation where I’m learning the framework and trying to write a tool for learning it …… at some point one of those takes a backseat

stuartsierra 2016-04-01T22:02:24.000045Z

Most Ring tutorials make it very hard, by assuming that the main "handler" is statically defined.

roberto 2016-04-01T22:02:58.000046Z

yeah, I remember struggling with that