Skip to content

Introduce asGroup()#59

Open
kimushu wants to merge 4 commits intoybainier:masterfrom
kimushu:feature_asGroup
Open

Introduce asGroup()#59
kimushu wants to merge 4 commits intoybainier:masterfrom
kimushu:feature_asGroup

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