Using Factories#
Factory functor can be provided to manually create a service. Functor should return unique_ptr and as an argument should optionally take reference to the service provider. Functor scheme (IServiceProvider &) -> std::unique_ptr or () -> std::unique_ptr
Examples/Guides/FactoryFunctions#
#include <SevenBit/DI.hpp>
#include <iostream>
#include <memory>
using namespace sb::di;
class Service
{
private:
std::string _message;
public:
Service(std::string message) { _message = message; }
std::string message() { return _message; }
};
int main()
{
IServiceProvider::Ptr provider = ServiceCollection{}
.addSingleton<Service>([](IServiceProvider &provider) {
return std::make_unique<Service>("Hello from service!");
})
.buildServiceProvider();
Service &service = provider->getService<Service>();
std::cout << service.message();
return 0;
}
Output#
Hello from service.