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.

Add the ability to retrieve all the instances without non initialised lazy singletons

See original GitHub issue

Kodein-DI version 6.4.1

Hi, I have a use case where I want to retrieve all AutoCloseable from the container, however, without creating lazy instances which haven’t been created yet.

My current code looks like it:

direct.allInstances<AutoCloseable>().forEach {
  it.close()
} 

The problem here is that it triggers creation of lazy instances, here, only to close them…

If there is a way to do it with current API then I would ask for help. If not then, please, consider it as feature request.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
romainbslcommented, Nov 4, 2019

Hi, no need, we will use this issue to follow the possible feature request.

0reactions
Koriitcommented, Dec 4, 2019

@romainbsl I disagree. I believe there is value in this and I have 2 issues with using Scopes here:

  • Scoping your definitions introduce additional scope-induced complexity to your definitions
  • Built-in scopes require values to implement ScopeCloseable (src, doc)

Second problem can be resolved by creating my own Scope but yet again this introduces additional complexity.

My use case is just cleaning up on application close. I think this is a good practice(…) which library such as Kodein-DI would do good in helping spreading. It feels unreasonable to introduce “Scopes” to my application just to do proper cleanup. 😦

It can be generalised to retrieving initialised instances. I think there should be more use cases for this in the wild.

I ask you to reconsider.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Add the ability to retrieve all the instances without non ...
Hi, I have a use case where I want to retrieve all AutoCloseable from the container, however, without creating lazy instances which haven't...
Read more >
Java: Lazy Initializing Singleton - Stack Overflow
You can use an enum as a Singleton enum Singleton { INSTANCE; }. Say your singleton does something undesirable in unit tests, you...
Read more >
Implementing the Singleton Pattern in C#
This article deals only with the situation where no parameters are required. Typically a requirement of singletons is that they are created lazily...
Read more >
Java Singleton Design Pattern Best Practices with Examples
Lazy initialization method to implement the singleton pattern creates the instance in the global access method. Here is the sample code for ...
Read more >
MSC07-J. Prevent multiple instantiations of singleton objects
The instance can be created using lazy initialization, which means that the instance is not created when the class loads but when it...
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