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.

Can I bind one singleton to two interfaces normally?

See original GitHub issue

I want to do something like that: bind(IFoo1.class).to(Foo.class).singletonInScope() bind(IFoo2.class).to(Foo.class).singletonInScope() but in that case will be created two instances of Foo. So are there any ways to bind Foo class to IFoo1 and IFoo2 interfaces by creating only one instance of Foo. And the other requirement - to initialize Foo.class automatically.

Thank you!

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
stephanenicolascommented, Mar 26, 2019

Toothpick doesn’t support aliases. But in this case you can do:

Foo foo = new Foo();
bind(IFoo1.class).toInstance(foo);
bind(IFoo2.class).toInstance(foo);

Re-open if needed.

0reactions
dlemurescommented, May 9, 2019

@Dimchel @morder @dimsuz, there are 2 reason why we didn’t implement it:

  1. On TP we are trying to keep it simple. Having such a feature, will allow to do something like the following code. It can be difficult to follow (error prone) and affect TP speed:
A -> B
B -> C
D -> B
  1. The way TP is implemented, bindings and scope annotations somehow provide a similar information: where and how they are created and for how long they are alive. (not exactly your scenario, but very similar)

However, it is possible to achieve the same by doing it more explicit: using a Provider.

class MyProvider<T>(val scope: Scope, val cls: Class<T>) : Provider<T> {

    override fun get(): T {
        return scope.getInstance(cls)
    }
}

Then you can use it:

bind(Foo.class).singletonInScope() 
bind(IFoo1.class).toProviderInstance(MyProvider(scope, Foo.class))
bind(IFoo2.class).toProviderInstance(MyProvider(scope, Foo.class))

This sample might not be optimal, but it is to show the idea.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Binding one class to several interfaces as singleton
With Ninject you can do this: var impl = new Impl(); container.Bind<IInt1>().ToMethod(c => impl); container.Bind<IInt2>().
Read more >
How to register a service with multiple interfaces in ASP.NET ...
1. Provide an instance of the service (Singleton only) ... The simplest approach is to provide an instance of Foo when you're registering...
Read more >
How to bind one implementation to multiple interfaces?
You need to bind them individually: bind(SimpleCallback.class).to(MassiveCallback.class ); bind(AnotherCallback.class).to(MassiveCallback.class);
Read more >
Contexts and Dependency Injection - Quarkus
2, Value is an artifact id for a dependency identified by name acme . ... In other words, injecting a normal scoped bean...
Read more >
Dependency Injection with Guice - Object Computing, Inc.
Often an application requires instances of multiple implementations of a given interface. For example, the OrderService may have need to submit orders to...
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