Get started
The Porygon library is made to provide a simple and efficient way to manage dependency injection in C++ projects.
Get started​
The single way to use the Porygon library is to create an instance of DependenciesHandler
and register the dependencies you want to use.
info
The Porygon library is available in the porygon
namespace.
Let's see how to use the Porygon library with a simple example.
First, include the necessary headers:
#include "libs/porygon/src/handler.hpp"
Then, we are going to declare a single service that we want to inject:
class MyService {
public:
void DoSomething() {
std::cout << "Doing something" << std::endl;
}
};
Now, we can create an instance of DependenciesHandler
and register the service:
using namespace porygon;
int main() {
auto handler = DependenciesHandler::Create();
handler->Add<MyService>();
auto service = handler->Get<MyService>();
service.DoSomething();
return 0;
}
Then let's create an other class in order to inject the MyService
:
class MyClass {
public:
explicit MyClass(DependenciesHandler::Ptr handler) : handler_(handler) {}
void DoSomething() {
auto service = handler_->Get<MyService>();
service.DoSomething();
}
private:
DependenciesHandler::Ptr handler_;
};
We can now create an instance of `MyClass` and call the `DoSomething` method:
```cpp
int main() {
auto handler = DependenciesHandler::Create();
handler->Add<MyService>();
auto myClass = handler->MakeWithDependencies<MyClass>();
myClass.DoSomething();
return 0;
}