You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
As you can see, it depends purely on the interface. What is the expected output? [:db1 :x] or [:db2 :x]? I don't know. Who knows? (I ran the code above and it returned [:db1 :x], however I don't know why. Without :local/root definitions it returned [:db2 :x])
So, my question is: How is this kind of "dependency injection" handled in polylith (in general and in clojure polilyth)?
Additional: How to decide which interface implementation is used? Where are the decisions being written?
Sorry if this is already described in the documentation. If it is, then I must have missed it.
The text was updated successfully, but these errors were encountered:
filipencopav
changed the title
How to do dependency injection of a component?
How to do "dependency injection" of a component?
Sep 25, 2024
In a nutshell, Polylith provides swappable components by utilising aliases in tools.deps. As you noticed, you can not have two different implementations of the same interface in your development time classpath at the same time. They have the exact same namespaces, and one will override the other. Instead, you pick one of the implementations as default and create a Polylith profile, corresponding to an alias in your deps.edn containing the other implementation. You can then start a REPL either with the default profile or the other alias you added with the additional implementation.
By the way, you made a mistake in your example. Both db1 and db2 components must use the example.db.* prefix, as that is the prefix for the interface.
In the future, you can ask your questions in the Clojurians Slack to get a faster reply.
Let's say we have the following setup:
Therefore, the following component directories (noise omitted):
The contents of
db1/src/example/db/interface.clj
:The contents of
db2/src/example/db/interface.clj
:The contents of core files:
As you can hopefully see, the interface is defined 2 times.
poly check
saysOK
. Now, I add a development filepavel.clj
with the contents:As you can see, it depends purely on the interface. What is the expected output?
[:db1 :x]
or[:db2 :x]
? I don't know. Who knows? (I ran the code above and it returned[:db1 :x]
, however I don't know why. Without:local/root
definitions it returned[:db2 :x]
)So, my question is: How is this kind of "dependency injection" handled in polylith (in general and in clojure polilyth)?
Additional: How to decide which interface implementation is used? Where are the decisions being written?
Sorry if this is already described in the documentation. If it is, then I must have missed it.
The text was updated successfully, but these errors were encountered: