I am the author of a small C ++ library that implements dependency injection (I think someone would call it an “IoC container”).
I always thought that finding a good metaphor for a library could help in many ways:
- simplifies the use of the library
- he helps the library author find the right abstractions
- This guide is for design validation.
- this is a way to find meaningful names for classes
- etc.
Now in my library I used the metaphor device / plug: your classes are “devices”, their dependencies are “plugs”, and you can connect the plug to another device. This is a sample code:
REGISTERED_CLASS( Foo ), public Device { ... private: Plug< Bar > bar; ... }; ... catalog.Create( "myFoo", "Foo" ); catalog.Create( "myBar", "Bar" ); ... catalog[ "myFoo" ].Plug( "bar" ).Into( catalog[ "myBar" ] );
Well, I'm not completely satisfied with this metaphor, because:
- in the real world, you insert stubs into sockets, so the source class must have a plug-in and the target class is a socket, but in the "code world" I have a class pointer pointing to another class;
- My metaphor doesn’t work very well when you have a connection with power> 1. I tried with
MultiplePlug< T > (this is basically a std::list< Plug< T > > ), but it doesn’t sound very good: what is it "multipleplug" in the real world?
Here you can find my library. Do you have a metaphor suggestion that works best for my code?
(However, if you have other good suggestions about the library, they will be welcome!)
Many thanks.
Note I know that there is another question with the topic “What a good metaphor for dependency injection,” but this is not a duplicate.
Edit : This is a discussion on this subject in a comment on a famous blog post.
Edit2 . Finally, I decided to change the syntax to this more convenient and simple syntax:
// explicit catalog use( myCatalog["myBar"] ).as( "bar" ).of( myCatalog["myFoo"] ); // implicit catalog: within( myCatalog ) { use( "myBar" ).as( "bar" ).of( "myFoo" ); ... }