How to find a girl or dependency injection pattern in action. Part II
Tags: cpp, beginners, opensource
Published: 2020-01-07 15:50:44

###Implementation###

In the previous [part](https://dev.to/vpereverzev/how-to-find-your-love-or-dependency-injection-pattern-in-action-part-i-3ba7), we introduced the basic concepts of the dependency injection pattern.

Now it is time to make out an example implementation (in this example, I use the Qt / C ++ bundle). Let's imagine a basic simple case.

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/5wxg4474g442ezai2uyx.PNG)

What can we see here?

**Module A**

* Interface **IDummyInterface**

* Service **DummyService** that implements interface **IDummyInterface**

**Module B**

* **ServiceInjector** - the "dating agency".
Picks up metadata (contacts) of the necessary service from ServiceResolver (agency database) and injects it in the client

* **ServiceResolver** - the database of our "dating agency". Able to register services and submit their metadata (contacts) for subsequent injection

**Module C**

* **DummyClient** - client that needs the functionality declared in **IDummyInterface**. At the same time, he doesn't want to know anything about the details of the implementation of **DummyService**

####Step 1####

Add to the IDummyInterface interface the ability to represent ourselves through the unique identifier QUuid.

```c++
#include <servicesresolver.h>
 
class IDummyInterface
{
public:
    INTERFACE_ID
 
    virtual void doSomething() = 0;
};
```

The implementation of the macro is extremely simple - just creating a unique identifier.

```c++
#define INTERFACE_ID                             \
    public:                                      \
    static const QUuid interfaceId() {           \
        static QUuid id = QUuid::createUuid();   \
        return id;                               \
    }                                            \
```

####Step 2####

Register the DummyService, the implementation of the IDummyInterface.

```c++
ServicesResolver::registerService<IDummyInterface, DummyService>(
[]() -> DummyService* {
    return new DummyService();
});
```

**OR**

```c++
// in case of singleton

ServicesResolver::registerService<IDummyInterface, DummyService>(
&DummyService::instance
);
```

####Step 3####

Inherit our client from **ServiceInjector**:

```c++
#include "serviceinjector.h"
 
#include "interfaces/idummyinterface.h"
 
class DummyClient : public ServiceInjector<IDummyInterface>
{
...
};
```

Inject dependency to the client:

```c++
#include "serviceinjector.h"
 
#include "interfaces/idummyinterface.h"
 
class DummyClient : public ServiceInjector<IDummyInterface>
{
    INJECT(IDummyInterface, dummyService)
 
    .....
};
```

The INJECT macro implementation is a simple setter and getter.
The first parameter is the type of interface.
The second parameter - an alias for the service in the scope of the client, needed for simplify calls of a service.

```c++
#define INJECT(INTERFACE_NAME, ALIAS)                              \
public:                                                            \
    INTERFACE_NAME* ALIAS() {                                      \
        return ServiceInjector<INTERFACE_NAME>::getService();      \
    }                                                              \
                                                                   \
    void set##ALIAS(INTERFACE_NAME* impl) {                        \  
        ServiceInjector<INTERFACE_NAME>::setService(impl);         \
    }                                                              \
```

The full implementation details and code-snippets can be found at our open-source project:
{% github musescore/MuseScore %}