Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tegral DI: a more flexible approach for injection aliases #22

Closed
utybo opened this issue Jun 19, 2022 · 1 comment
Closed

Tegral DI: a more flexible approach for injection aliases #22

utybo opened this issue Jun 19, 2022 · 1 comment
Assignees

Comments

@utybo
Copy link
Owner

utybo commented Jun 19, 2022

Currently, Tegral DI uses a simple system for storing components: just a map from Identifier to anything they need (currently, all environments use a simple Map<Identifier<*>, Any>, where Any is the actual instance of the object we are interested in).

While this is alright, it poses a problem for advanced scenarios. For example:

  • Factories, or anything that is wrapped via some injected helper object. This makes it impossible to use the by scope() mechanism to retrieve them, and alternatives must be used (e.g. by scope.factory())
  • Services cannot really rely on the actual type of the object, the only "stable", non-hacky typing information is contained in the identifier. Yet, the identifier is not always accurate w.r.t. the actual type of the object. For example, the following structure will not actually trigger the start() function:
interface MyService {
    // ...
}

class MyServiceImpl : MyService, TegralDiService {
    // ...
}

val env = tegralDi {
    put<MyService>(::MyServiceImpl)
}

An alternative is supporting a standardized and more complete way to store identifiers, such as:

typealias Declarations = Map<Identifier<*>, IdentifierResolver<*>>

interface IdentifierResolver<T> {
    fun resolve(allDeclarations: Declarations): T
}

class SimpleIdentifierResolver<T>(
    val instance: T
) : IdentifierResolver<T> {
    override fun resolve(allDeclarations: Declarations): T {
        return instance
    }
}

class AliasIdentifierResolver<T>(
    val actualIdentifier: Identifier<T>
) : IdentifierResolver<T> {
    override fun resolve(allDeclarations: Declarations): T {
        return allDeclarations[actualIdentifier].resolve(allDeclarations)
    }
}

This is more flexible in pretty much every way.

⚠️ This would mean declarations would be part of the public API of all environments (including not extendable ones)

@utybo utybo self-assigned this Jun 19, 2022
@utybo
Copy link
Owner Author

utybo commented Jul 16, 2022

Fixed in #23

@utybo utybo closed this as completed Jul 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant