What is the right metaphor for a dependency injection library?

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" ] ); // this means myFoo.bar = 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" ); ... } 
+6
source share
1 answer

I assume this is for the configuration phase. If you insist on the terminology of Plug and Into :

 // this means myFoo.bar = myBar catalog[ "myFoo" ].Into( "bar" ).Plug( catalog[ "myBar" ] ); 

where Plug used as the verb ie "to plug". If you are creating a free interface, try formulating meaningful sentences. If I wanted to configure several dependencies of "myFoo", I would be very happy to write:

 catalog[ "myFoo" ]. .Into( "bar").Plug( "myBar" ) .Into( "some_other_member" ).Plug( "that_other_instance" ); 

Something unusual is that, usually when setting up, you are dealing with types not with instances ...

+2
source

Source: https://habr.com/ru/post/921965/


All Articles