Fork me on GitHub
#component
<
2016-04-01
>
francoiswirion21:04:42

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?

roberto21:04:06

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

roberto21:04:14

everything else should be normal clojure functions

roberto21:04:46

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

stuartsierra21:04:05

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

francoiswirion21:04:15

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

stuartsierra21:04:08

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

stuartsierra21:04:04

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.

francoiswirion21:04:11

@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?

stuartsierra21:04:53

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

stuartsierra21:04:38

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.

stuartsierra21:04:06

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

stuartsierra21:04:47

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

francoiswirion21:04:06

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!

roberto21:04:40

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.

stuartsierra21:04:39

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

francoiswirion21:04:04

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

roberto21:04:26

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

roberto21:04:59

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

francoiswirion21:04:19

@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.

roberto21:04:40

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)

roberto21:04:59

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

roberto22:04:46

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

stuartsierra22:04:24

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

roberto22:04:58

yeah, I remember struggling with that