How to bind multiple interfaces to same singleton instance ?
See original GitHub issueHi, I’m trying to use Kodein for building web application.
After using Kodein for a while, I noticed that I don’t know how to bind the same singleton instance to multiple interfaces.
Here is an example.
interface Foo {
fun foo(): String
}
interface Bar {
fun bar(): String
}
class Baz(
private val dependency1: Dependency1,
private val dependency2: Dependency2,
private val dependency3: Dependency3
): Foo, Bar {
...
}
How can I bind the same singleton instance (baz) to both foo and bar in such a case?
Kodein {
bind<Foo> with singleton { Baz(instance(), instance(), instance()) }
bind<Bar> with singleton { Baz(instance(), instance(), instance()) } // <-- This will instantiate different instance !
}
I would be pleased if you could give me an advice. Thanks you for your awesome library !
Issue Analytics
- State:
- Created 6 years ago
- Comments:5 (4 by maintainers)
Top Results From Across the Web
Is it possible to bind different interfaces to the same ...
Now I bind the same class to both interfaces using Ninject. Since I want the same instance of IAmAnImplementation beeing used for IAmAnInterface...
Read more >How to bind one implementation to multiple interfaces?
Sam, if you wanted just one instance, you could do this: bind(MassiveCallbackImpl.class).in(Scopes.SINGLETON); bind(SimpleCallback.class).to(MassiveCallbackImpl ...
Read more >How to bind a singleton for two different interfaces #1001
Hi. I have a class, implementing multiple interfaces: export class InMemoryFoodLogRepository implements IAddFoodLog, IGetFoodLogs { } This ...
Read more >How to register a service with multiple interfaces in ASP. ...
1. Provide an instance of the service (Singleton only) The simplest approach is to provide an instance of Foo when you're registering your ......
Read more >IoC Challenge – Multiple interfaces to the same singleton ...
There are two easy ways to do this now. First is bind the interfaces directly to a pre-created instance. However, this means that...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Another trick would be to bind both instances with the same singleton binding:
This solution may be closer to what you are trying to achieve 😉
A nice readable syntax for this uses
let
as follows: