Skip to content

Intercept provided values #408

Open
Open
@asmeikal

Description

@asmeikal

Describe the solution you'd like
We need to intercept the provided values after they are constructed, to check for implemented interfaces and possibly call some initialization methods.

We have an implementation ready that adds a WithProvidedCallback option to the container that serves this purpose. The callback will receive all values after they are constructed, along with name/group information and type information.

Describe alternatives you've considered
This could be implemented as a ProvideOption, but would need a list of values as input of the callback, since constructors can return multiple values, and would be duplicated each time Provide is called on the container.

The callback could be implemented also as a ScopeOption to override behaviour in nested scopes, but since it would be the first actual option of this kind, we skipped implementation for now.

Is this a breaking change?
No, even if the callback is implemented as a ScopeOption.

Additional context
We are open sourcing an extremly opinionated dependency injection framework we use to develop microservices in Go. This (small) framework handles automatically health check registration, calling initialization and destruction methods at service startup and destruction, registration of prometheus metrics. Instead of relying on lifecycle hooks like Fx does, our framework relies on interfaces implemented by the provided values, e.g. by looking for the Start method on provided values and invoking it at application startup. This reduces code duplication, as most of this shared logic can be implemented with single methods on each component.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions