question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to bind multiple interfaces to same singleton instance ?

See original GitHub issue

Hi, 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:closed
  • Created 6 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
SalomonBryscommented, Sep 25, 2017

Another trick would be to bind both instances with the same singleton binding:

val bazSingleton = singleton { Baz(instance(), instance(), instance()) }
bind<Foo> with bazSingleton
bind<Bar> with bazSingleton

This solution may be closer to what you are trying to achieve 😉

0reactions
rocketramancommented, Jan 21, 2018

A nice readable syntax for this uses let as follows:

singleton { Baz(instance(), instance(), instance()) }.let {
  bind<Foo> with it
  bind<Bar> with it
}
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found