Closed sobotkami closed 5 months ago
One possible solution:
instead of
@Provides
fun userReadInMemoryAdapter(userReadPort: UserReadPort): UserReadPort = userReadPort
@Provides
fun userWriteInMemoryAdapter(userWritePort: UserWritePort): UserWritePort = userWritePort
do the following:
protected val UserInMemoryAdapter.bindRead: UserReadPort
@Provides get() = this
protected val UserInMemoryAdapter.bindWrite: UserWritePort
@Provides get() = this
But, is there a solution to get rid of these definitions?
fun userReadInMemoryAdapter(userReadPort: UserReadPort): UserReadPort = userReadPort
You are requesting a dependency to provide itself. This should actually fail to compile with a cycle error, the fact it generates invalid code instead is a bug
As you note below,
protected val UserInMemoryAdapter.bindRead: UserReadPort
@Provides get() = this
Is the correct way to do this, providing the impl to the interface. The equivlent using the function syntax you used originally would be
fun userReadInMemoryAdapter(userReadPort: UserInMemoryAdapter): UserReadPort = userReadPort
But, is there a solution to get rid of these definitions?
No, you need a definition in your component for each interface you are providing. There have been some requests for an anvil-like feature to provide them with an annotation on the class itself instead of the component, https://github.com/evant/kotlin-inject/issues/212 if you are curious.
Adding the bug label for the invalid code gen. This is likely related to https://github.com/evant/kotlin-inject/issues/313
Hi all, I am struggling with this design: ONE adapter implements TWO interfaces. How do I define this correctly? Thanks!
produces
Generated code