sergeshustoff/dikt

Provide api for mapping implementation to interface

Closed this issue · 2 comments

Currently for creating implementation and providing it everywhere by it's interface we need to do something like that:

class ImplDep : BaseDep
interface BaseDep
class Target(dep: BaseDep)

class Module {
    @Create fun target(): Target

    @Create private fun impl(): ImplDep
    private fun baseDep(): BaseDep = impl()
}

or

    @UseConstructors(ImplDep::class)
    @Create private fun dep(): BaseDep 
    private fun baseDep(impl: ImplDep): BaseDep = impl

Seems too boilerplate either way. Need to find an api that would allow to do that in one line while supporting different cases: 1. implementation is provided by property or function and 2. implementation is created using constructor

When implementation is provided somewhere it seems fairly simple to use this:

fun baseDep(): BaseDep = impl()

But when we want to use constructor and implementation isn't used anywhere directly it might be a good idea to add alternative versions of @create and @CreateSingle like this

@CreateImpl(ImplDep::class) fun dep(): BaseDep

Not sure if this api is straitforward enough

With new API it's easier to map implementations using fun something(): Base = resolve<Impl>()