Separate Implementation¶
This example shows how to register the service as an interface with implementation. Service will be accessible only by an interface, implementation is hidden for the client
Examples/Guides/SeparateImplementation¶
#include <SevenBit/DI.hpp>
#include <iostream>
using namespace sb::di;
struct IService
{
virtual std::string helloFromService() = 0;
virtual ~IService() = default;
};
class Service final : public IService
{
public:
std::string helloFromService() override { return "Hello from service."; }
};
int main()
{
ServiceProvider provider = ServiceCollection{}.addSingleton<IService, Service>().buildServiceProvider();
auto &service = provider.getService<IService>();
std::cout << service.helloFromService();
return 0;
}
Output¶
Hello from service.