Keyed Services#
Multiple services with the same implementation can be registered using keyed services with addKeyed… ServiceCollection methods, similarly to normal services ServiceProvider has special Keyed methods to retrieve these services. Each keyed service is created independently and has a different address, the default service can be retrieved or registered with an empty serviceKey = “”
Examples/Guides/KeyedServices#
#include <SevenBit/DI.hpp>
#include <cassert>
#include <iostream>
using namespace sb::di;
struct Service
{
std::string action() { return "action"; }
};
struct IServiceExecutor
{
[[nodiscard]] virtual std::string execute() const = 0;
virtual ~IServiceExecutor() = default;
};
class ServiceExecutor final : public IServiceExecutor
{
Service &_serviceA;
Service &_serviceB;
Service &_serviceC;
public:
explicit ServiceExecutor(ServiceProvider &provider)
: _serviceA(provider.getService<Service>()), // equivalent of provider.getKeyedService<Service>("")
_serviceB(provider.getKeyedService<Service>("serviceB")),
_serviceC(provider.getKeyedService<Service>("serviceC"))
{
assert(&_serviceA != &_serviceB);
assert(&_serviceB != &_serviceC);
assert(&_serviceA != &_serviceC);
}
[[nodiscard]] std::string execute() const override
{
return _serviceA.action() + ", " + _serviceB.action() + ", " + _serviceC.action() + " executed.";
}
};
int main()
{
ServiceProvider provider = ServiceCollection{}
.addKeyedSingleton<Service>("") // equivalent of addSingleton<Service>()
.addKeyedSingleton<Service>("serviceB")
.addKeyedSingleton<Service>("serviceC")
.addScoped<IServiceExecutor, ServiceExecutor>()
.buildServiceProvider();
const auto &executor = provider.getService<IServiceExecutor>();
std::cout << executor.execute();
return 0;
}
Output#
action, action, action executed.