Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide an example explaining the ?essence? of the components idea #70

Open
fhalde opened this issue Dec 30, 2021 · 0 comments
Open

Provide an example explaining the ?essence? of the components idea #70

fhalde opened this issue Dec 30, 2021 · 0 comments
Labels

Comments

@fhalde
Copy link

fhalde commented Dec 30, 2021

Almost every usage of component in projects I've inherited got one important detail wrong about its usage

e.g.

Here's a trivial database component that usually gets created

(defrecord Database [host port connection]
  component/Lifecycle

  (start [component]
    (println ";; Starting database")
    (let [conn (connect-to-database host port)]
      (assoc component :connection conn)))

  (stop [component]
    (println ";; Stopping database")
    (.close connection)
    (assoc component :connection nil)))

And then, the entire codebase is littered with getting the :connection key out of the Database component

e.g.

(some-database-library/query (:connection db-comp) query)

This feels wrong. You can never mock components this way

IMO the intention of the component library was to do the following

(defrecord Database [host port connection]
  component/Lifecycle

  (start [component]
    (println ";; Starting database")
    (let [conn (connect-to-database host port)]
      (assoc component :connection conn)))

  (stop [component]
    (println ";; Stopping database")
    (.close connection)
    (assoc component :connection nil))

  proto/IDatabase

  (query [this] ...))

Now the rest of the code does the following

(db/query db-comp)

The approach I mentioned can be an extreme one too i.e. now, I have to provide the same interface/api in proto/IDatabase as the library/database that I'm building a component for

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant