Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is dependency injection useful in C++

C# uses Dependency Injection (DI) a lot to have a lossless and testable platform. For this, I need an interface and maybe a DI or Inversion of Control (IoC) container for resolving my instances.

But how do you do this in C++? I've read a little bit about this, and it seems that dependency injection in C++ isn't as big a topic as in C#. In C++ you use a reference to an object - this is the way to use DI in C++, right?

If my theory with references are correct, is there something like a container where I can resolve all the references? In C# I have a "bad class/bad project/assembly" which registers all my instances into a static container at the program start. Then, in every class, I'm able to instance the static container and can resolving a specific instance, is this possible in C++?

Are you using Dependency Injection (or whatever it is called) in C++? If yes, how you're use it? Are there similarities to C#?

like image 829
Marcel Hoffmann Avatar asked Mar 31 '15 09:03

Marcel Hoffmann


People also ask

Is dependency injection really useful?

The dependency injection technique enables you to improve this even further. It provides a way to separate the creation of an object from its usage. By doing that, you can replace a dependency without changing any code and it also reduces the boilerplate code in your business logic.

What is the benefit of dependency injection in C sharp?

Dependency Injection (or inversion) is basically providing the objects that an object needs, instead of having it construct the objects themselves. It is a useful technique that makes testing easier, as it allows you to mock the dependencies.

When should dependency injection be used?

More specifically, dependency injection is effective in these situations: You need to inject configuration data into one or more components. You need to inject the same dependency into multiple components. You need to inject different implementations of the same dependency.

Is dependency injection useful in C++?

Yes, dependency injection is useful in C++ as well. There is no reason why it shouldn´t be, because it doesn´t require a specific language or syntax, but just an object-oriented class architecture (at least this is probably the most usual case).


2 Answers

For this, I need an interface and maybe a container for resolving my instances. But how you do this in C++?

In the same way. The difference is that where you "program to an interface" in C#, you "program to a base class" in C++. Additionally, you have extra tools in C++ that you do not have in C# (for example, policy-based templates implement dependency injection chosen at compilation time).

In C++ you're use a reference to an object, this is the way to use DI in C++, right?

No; this is not the way to use DI, this is a way to use DI in C++.

Also consider:

  • use a pointer to an object (or smart pointer, depending on the case)
  • use a template argument for a policy (for an example, see std::default_delete use in smart pointers)
  • use lambda calcullus with injected functors/predicates.

In C# I've a "bad class/bad project/assembly" which register all my instance into a static container at the program start.

If I understand correctly, you set all your data in this static container and use it all over the application. If this is the case, then you do not use dependency injection correctly, because this breaks Demeter's Law.

is this possible in C++?

Yes, it is perfectly possible (but you shouldn't do it, due to it breaking Demeter's law). Have a look at boost::any (this will allow you to store heterogenous objects in a container, similar to storing objects by object reference in C#).

Are you using dependency injection or whatever it is called in C++?

Yes (and it is called dependency injection :) ).

If yes, how you're use it?

As I described above (policy template arguments, injected functors and predicates as reusable components, injecting objects by reference, pointer smart pointer or value).

like image 112
utnapistim Avatar answered Oct 19 '22 08:10

utnapistim


Using dependency injection is quite straightforward in C++. Just define an interface (a pure abstract base class) that you use as reference or pointer (or smart pointer) argument to the constructor or init function of the class you want to dependency inject into.

Then, in the unit test, inject a mock object (an instance of a class inheriting from the abstract interface class), and in real code, inject an instance of the real class (also inheriting from the same interface class).

Easy-peasy.

like image 43
Erik Alapää Avatar answered Oct 19 '22 09:10

Erik Alapää