BabyDI: Simple Dependency Injection for C++11

Stupid-simple, header-only, zero-dependency DI framework for C++11.

BabyDI on Github

Pull in ya dependencies cleanly, instead of writing code that looks like this:

auto service = new ShippingService(new ProductLocator(),  
   new PricingService(), new InventoryService(), 
   new TrackingRepository(new ConfigProvider()), 
   new Logger(new EmailLogger(new ConfigProvider())));

ouch!!!

Example:

// Define a basic class which depends on IExampleInterface
class BasicDependent {  
public:  
  void CallImplementation () {
    m_implemented->SomeMethod();
  };

private:  
  // Inject the dependency
  BabyDI::Injected<IExampleInterface> m_implemented;
};

int main(int argc, char** argv) {  
  // Create a new IoC container and activate it
  BabyDI::Container container;
  container.Activate();

  // Bind the example implementation to the example interface
  // The container owns the implementation and will delete it
  // when the container's destructor is called.
  container.Bind<IExampleInterface>(new ExampleImplementation());

  // Create a new dependent
  BasicDependent dependent;

  // The injected implementation is now called
  dependent.CallImplementation();

  // Deactivate the container
  container.Deactivate();
}

BabyDI on Github

comments powered by Disqus