Alec Muffett
11/01/2020, 8:36 PMAppConfig
instance — containing references to objects that implement interfaces like DB: IDatabase
, etc — and pass that appConfig
into the constructors that implemented [various interfaces] for my service.
All very typical factory-pattern stuff - the (for example) appConfig.DB.query()
methods would then be used by service methods (via a "retrieval"-type approach) and the appConfig reference itself would be blindly passed on to any child-factories which need to be created.
But I am learning Kodein and I am messing around with this blog post which suggests making the companion object for Foo : IFoo
into a FooFactory
by declaring a companion object { fun create() : IFoo = ... }
method.
With that context, QUESTION: What's the proper Kotlin & Kodein approach to solving the problem of passing an instantiated appConfig
reference into a FooFactory
that is the companion object of Foo
? In fact, would this be stupid to do? I am trying to avoid endless invocations of Foo(appConfig: DI)
by passing appConfig
into the FooFactory
when I create the FooFactory
,
I could maybe declare Foo.appConfig
as a lateinit
var in the Foo
factory/companion-object, and then set it via a static method call, but I feel sure that there must be a better, more typical way in Kodein - possibly modules?Alec Muffett
11/01/2020, 9:06 PMAlec Muffett
11/01/2020, 9:21 PMkodein
reference/field ... but if I want to push this into a static factory object
, then either I have to give up on the companion-object hack, or else I need to pass the kodein
reference into the object
as a lateinit
var, as described above?Alec Muffett
11/01/2020, 9:31 PMFooFactory
classes which implement KodeinAware
, and have them generate Foo
instances which know about / inherit the kodein
instance as a private constructor argument and/or implement KodeinAware
.Alec Muffett
11/01/2020, 10:16 PMKodeinAware
is now DIAware
?salomonbrys
11/02/2020, 11:46 AMbind<MyTypeFactory>() with singleton { MyTypeFactoryImpl(di) }
.salomonbrys
11/02/2020, 11:47 AMMyTypeFactoryImpl
DIAware
is completely optional.
The DIAware
only exists to make DI retrieval easier.Alec Muffett
11/02/2020, 11:52 AMsalomonbrys
11/02/2020, 11:54 AMAlec Muffett
11/02/2020, 4:22 PM