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

Dependency injection support #71

Open
WojciechMigda opened this issue May 23, 2022 · 2 comments
Open

Dependency injection support #71

WojciechMigda opened this issue May 23, 2022 · 2 comments
Labels
enhancement New feature or request

Comments

@WojciechMigda
Copy link

Hi,

it occured to me yesterday that it would be a nice feature to have stateful nexus, which, in spirit of DI, would then be able to inject extra arguments to handlers registered through features.

What I have in mind is something that Kris Jusiak's library SML already has. https://github.com/boost-ext/sml
Specifically, in Kris' library state machine instance can be instantiated with extra dependencies. Then, through 'smart' resolution it is able to pass these dependencies to actions which are part of state machine's transitions.
Such basic example is presented in SML's documentation, specifically, Section 5: Create a state machine.

In case of CIB, it would allow to pass user's dependencies to created nexus:

cib::nexus<nexus_project> nexus{Type1: dependency1, Type2: dependency2, ...};

Then, CIB when it matches handlers registered against service signature, it would also allow handler's signatures which arguments are superset of those specified in service's definition, extended with types which can be matched against dependencies passed to instantiated nexus. For example:

struct service : public cib::callback_meta<Payload const &>{};`

struct feature
{
    static auto constexpr config = cib::config(
        cib::extend<service>(
            [](double x, Payload const & p)
            {
                // do sth with injected x and passed p
            }
        )
    );
};

cib::nexus<nexus_project> nexus{87.0};

In the above service is declared to accept const reference to Payload, but then we'd attempt to declare feature which is attached to a handler which accepts Payload and a double. Since nexus is instantiated with a double as one of dependencies passed by a user it would allow such feature and pass both arguments - one originating from the point of invocation of the service through the nexus, and the other from the pool of dependencies which are part of nexus' state.

I believe this would be huge undertaking to introduce such enhancement, but given that Kris' SML library already has it, maybe it could be used as a guidance?

Thanks,
Wojciech

@lukevalenty
Copy link
Contributor

This is a fantastic feature suggestion! I've looked at @krzysztof-jusiak's DI library and found that it didn't work in a constexpr context for myself. Maybe there are some changes that could be made.

I would want to be able to inject subclasses of a base interface (virtual function overriding.)

I would also want to inject arbitrary types into template functions.

Thanks for this suggestion @WojciechMigda.

@kris-jusiak
Copy link
Contributor

I think that would be nice. I'm happy to experiment and see what can be done.

@lukevalenty lukevalenty added the enhancement New feature or request label May 26, 2022
@lukevalenty lukevalenty changed the title Stateful nexus with dependencies Dependency injection support May 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants