What is suggested way of creating singletons that ...
# multiplatform
k
What is suggested way of creating singletons that will work on ios/jvm?
s
Doesnt this work ?
Copy code
object Bar {
    
}
k
How do I inject dependencies to it? Separate method?
s
If you are using a dependency framework I assume you should get it from that (I only have experience with Guice). If not then just make a global property and initialize it from the main method/application entry point
k
It feels that I'm building DI ๐Ÿ˜„
Thx, object may work with lateinit
m
Why do you need a singleton for this? Just make a class and give the dependencies through its primary constructor.
๐Ÿ‘ 1
โž• 1
k
Object doesn't work, as it failed to be mutated. I managed to do it with factory, that provide plain class as singleton.
If there should be only one instance of the object during runtime, singleton sounds suitable.
So class is using primary constructors, and factory handling init and singleton functionality.
In my case, it is a service and could be called first from different classes (or not called at all), so there is need to separate config, init and actual use.
I'll try to publish example this week (if I succeed) ๐Ÿ™‚
s
Generally I would say its an anti pattern to have objects exposed as singletons like I think you mean (in the same vein as global variables etc.) We consider singletonness as a scope (in the DI sense) and just inject them like any other dependency.
๐Ÿ‘ 1
m
Using a normal class is also better for testing. You can easily mock it for example. Using a singleton
object
would force it to be the same in every environment.
i
#kodein and #koin solve this, no?
m
Those could inject some mock into the object's dependencies, but they can not mock the object itself.
i
I was referring to the original question that started this thread. The author even states
It feels that I'm building DI
. Don't do that ๐Ÿ˜›, we already have great DI options.
m
Ah, ok.
k
@ian.shaun.thomas koin doesn't work on ios/native. Kodein requires specific order of initialization.
They maybe good to use in single app, but i don't see (and there is no examples) how they could be used as part of libraries.
@spand you absolutely correct that what i did is not a singleton. My original intention was to use singleton, but because of complex initialization and dependencies, I found other way to do it.
i
As part of a library I think you would want to seriously avoid shipping with that kind of dependency however with kodein, and I assume koin, you would simply use them as you would any other dependency such as the jdk and js dependencies commonly used in MPP libs. I would recommend having your core lib and then having another MPP lib that wraps your core lib with the DI choice such that in the future you could theoretically drop it or switch to something else without a lot of hassle.
for kodein, the sample shows using kodein as a common lib, via modules, and then using
application
modules https://github.com/Kodein-Framework/Kodein-Samples/tree/master/di/coffee-maker I would recommend joining the #kodein channel if you have questions as the maintainer is active on here ๐Ÿ™‚