Dependency Injection in C++ isn’t possible?

Starting up a new project called Cube a Libre I was confronted with no cool dependency injection framework in C++ like the famous Spring Framework in Java. I tested some of them: pococapsule, autumnframework and qtioccontainer. No one was acceptable (dependency to java(!), 6 years old, too complicated.

So I started a new experiment: Is Dependency Injection in C++ possible?

Yes and no. No, because there is no introspection. Yes, if you had a little help from the Qt framework which provides the metaobject system and a preprocessor called moc. So your project has the dependency to Qt. Also your classes can’t be implemented as plain old POJOs POCOs. They has to inherit from QObject. In addition, they has to implement some methods and has to register themselfs.

Lets take a look into the xml file:



This looks much like a xml file you would use in Java/Spring. You can define objects from a classname (a string!), give them names, sets properties and inject other objects using the ref attribute.

Now lets take a look on a class definition:

class JabberClient : public QObject
{
// For Qt Meta system
Q_OBJECT

Q_PROPERTY(QString name WRITE setName)
Q_PROPERTY(QString password WRITE setPassword)
Q_PROPERTY(QString server WRITE setServer)
Q_PROPERTY(QString resource WRITE setResource)

public:
JabberClient();
JabberClient(const JabberClient& jabberClient) {};
virtual ~JabberClient();

void connect();
void disconnect();
void sendMessage(QString jid, QString text);

void onConnect(void);

void setName(QString name);
void setPassword(QString name);
void setServer(QString server);
void setResource(QString resource);

public slots:
void init();
void destroy();

private:
QString name;
QString password;
QString server;
QString resource;
};

REGISTER_CLASS(namespace::JabberClient, JabberClient);

Q_DECLARE_METATYPE(namespace::JabberClient*)

You can see, we need a constructor, a destructor and a copy constructor. The copy constructor doesn’t have to be implemented, but must exist. The class itself inherits from QObject, so we can use the Qt meta object system. Also we define the some properties and their setters. Using Qt’s PROPERTY macro we make the meta object system know them. At the bottom we can see, that we register the metatype using Q_DECLARE_METATYPE. Also, we register the class using the macro REGISTER_CLASS provided by cube-a-libre.

Finally we have to initialize the application context:

engine::XmlApplicationContext context;
context.createContextFromXmlFile("context.xml");
engine::JabberClient* jabberClient = (engine::JabberClient*) context.getObject("jabberClient");
jabberClient->init();

[...] more code [...]

context.destroyContext();

First, we create a XmlApplicationContext object. Then we let it create an application context from a xml file. The application context will be automagically created. Then we can make use of the getObject Method to get the singleton object instance. Finally, we destroy the context.