
Non Intrusive Static Recursive Dependency Injection for C++14 - matt42
https://github.com/matt-42/iod/blob/master/dependency_injection.md
======
cheez
My favourite topic! I am glad that people are still thinking about automated
DI in C++. I am sure the new standards make a lot of things easier. This
library looks like a cool experiment and I hope the author continues to move
in this direction.

Here is my version:
[http://bitbucket.org/cheez/dicpp](http://bitbucket.org/cheez/dicpp)

With the following caveats:

    
    
        * It is not very cool
        * It works really well for my uses
        * It is not as efficient as the compile-time ones
    

WRT efficiency, 99% of DI is done at application startup and you can easily
make that efficient enough by delaying instantiations until you actually need
them. The bottleneck at startup so far has never been the library itself.

I continue to look for one that allows me to create compiler firewalls so I
can use _it_ instead of my own.

Alternatively, if someone wants to give me tips to avoid the hideous
DI_CONSTRUCTOR (see
[https://bitbucket.org/cheez/dicpp/src/HEAD/lib/examples/stoc...](https://bitbucket.org/cheez/dicpp/src/HEAD/lib/examples/stockquote/StockQuoteApp.hpp#cl-15))
I'm all ears.

There was one posted on the Boost list not so long ago which was VERY
promising but I haven't managed to get around to using it yet.

------
malisper
Can someone explain what exactly the library does? All I got from the
description is something about a way of building an argument list, and then
how it is possible to fulfill some dependencies with factories or make methods
or something.

~~~
matt42
You can have a look at the Go implementation:
[https://github.com/facebookgo/inject/blob/master/inject.go](https://github.com/facebookgo/inject/blob/master/inject.go)

------
matt42
The source code is here, interesting if you enjoy c++14 meta programming:
[https://github.com/matt-42/iod/blob/master/iod/di.hh](https://github.com/matt-42/iod/blob/master/iod/di.hh)

~~~
_yosefk
IOD comes with example code using identifiers such as _Name. AFAIK all C and
C++ standards reserve all identifiers starting with _[A-Z] as well as those
starting with __ for compiler use (and I've seen people complain about it a
couple of times - about a decade ago someone was hit by a new gcc version
#defining _Y, and recently someone else got hit by a more recent gcc version
#defining _X, on some platforms but not others...)

So I think a different convention (Name_, _name, etc.) would be better to use
in example code, so that people don't learn a convention that might byte them
down the road.

~~~
matt42
Thanks. I was not aware of this rule. I will change this convention as soon as
possible. It is a bit strange that g++ did not report any warning...

------
amelius
What baffles me is this: Why does such a (conceptually) simple thing require
such a hairy implementation?

~~~
TillE
Is it conceptually simple? The examples demonstrate behavior that would be
pretty impressive even in a dynamic language.

Obviously the trivial form of dependency injection (just pass a parameter
manually) requires no special library.

