Dependency injection library for C++
cppdi is a header-only c++17 library that brings dependency injection to C++. It’s heavily inspired by DI provided by ASP.NET.
Simply drop the header file in your project or add the include
directory to your include paths.
Here’s a simple example of using the dependency injection container.
#include <iostream>
#include "Container.hpp"
class IGreeter
{
public:
virtual void Greet() = 0;
}
class HelloWorldGreeter : public IGreeter
{
public:
void Greet() override
{
std::cout << "Hello, world!\n";
}
}
int main()
{
cppdi::Container container;
container.AddSingleton<IGreeter, HelloWorldGreeter>();
auto greeter = container.GetRequiredService<IGreeter>();
greeter->Greet();
return 0;
}
This outputs Hello, world!
If your class doesn’t have a parameterless constructor (which is usually the case) you’ll have to create a template specialization for your types in cppdi namespace. An example specialization will look like this:
namespace cppdi
{
template <>
std::shared_ptr<ISerializer> CreateInstance<ISerializer, JsonSerializer>(const Container &container)
{
auto configuration = container.GetRequiredService<IConfiguration>();
auto instance = std::make_shared<JsonSerializer>(configuration); // Let's assume that JsonSerializer takes a shared_ptr<IConfiguration> as a parameter
return instance;
}
}
There are more ways you can register implementations of interfaces and they are documented in the Container.hpp
file.