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

Introduce asGroup() #59

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

Conversation

kimushu
Copy link

@kimushu kimushu commented Sep 17, 2024

This PR introduces asGroup() to register multiple abstraction(s) at once.

For example, consider the following case:

template <class... TBases>
class ISomeGroup : public TBases... {};

class FooBar : public ISomeGroup<IBaseA, IBaseB>, public IBaseC {};
/*
  FooBar hierarchy:
    IBaseA ----+- ISomeGroup<IBaseA, IBaseB>----+- FooBar
    IBaseB ---/                                /
    IBaseC -----------------------------------/
*/

In this case, IBaseA and IBaseB can be registered together as follows:

Hypodermic::ContainerBuilder builder;
builder.registerType<FooBar>()
    .asGroup<ISomeGroup>();  // This works like: .as<IBaseA>().as<IBaseB>()

(IBaseC that are not included in an ISomeGroup<...> are not registered by asGroup(), but can be registered individually by as<IBaseC>().)

Additionally, we can change how the base class is resolved:

template <class... TItems>
class ISomeGroup2 : public ISomeBase<TItems>... {};

class FooBar2 : public ISomeGroup2<int, char> {};
/*
  FooBar2 hierarchy:
    ISomeBase<int> ----+- ISomeGroup2<int, char> --- FooBar2
    ISomeBase<char> --/
*/

In this case, ISomeBase<int> and ISomeBase<char> can be registered together with a custom resolver as follows:

template <class T>
struct SomeResolver
{
    using Type = ISomeBase<T>;
};

Hypodermic::ContainerBuilder builder;
builder.registerType<FooBar2>()
    .asGroup<ISomeGroup2, SomeResolver>();
    // ^This works like: .as<ISomeBase<int>>().as<ISomeBase<char>>()

Thanks,

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

Successfully merging this pull request may close these issues.

1 participant