1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
//! Dependency Injection (DI) //! //! # Goals //! //! * Focused, reusable, testable components //! * A dependency graph checked at compile time //! //! # What is Dependency Injection? //! //! [5-minute introduction.](https://youtu.be/IKD2-MAkXyQ) //! //! # Cyclic Dependency //! //! DI is not for circular dependency resolution: [Circular dependency is something that is to be //! avoided][1]. //! //! # Example //! //! ```rust //! #![feature(plugin)] //! #![plugin(hypospray_extensions)] //! extern crate hypospray; //! //! use hypospray::{Co, Construct, Graph}; //! // work around: https://github.com/rust-lang/cargo/issues/960 //! # fn main() { //! trait Engine { //! //! fn rev(&self) -> &'static str; //! } //! //! #[implements(Engine)] //! struct GranCabrioV8; //! //! impl Engine for GranCabrioV8 { //! //! fn rev(&self) -> &'static str { //! //! return "Vrooom! Vroom! Vroooom!!!"; //! } //! } //! //! impl<'dep> Construct<'dep> for GranCabrioV8 { //! //! type Dep = (); //! //! fn __construct(_: Self::Dep) -> GranCabrioV8 { //! GranCabrioV8 //! } //! } //! //! #[inject(Engine)] //! trait Deps { } //! //! struct SportsCar<M: ?Sized + Deps> { engine: Co<M, Engine> } //! //! impl<M: ?Sized + Deps> SportsCar<M> { //! //! fn gas(&self) { //! //! println!("{}", self.engine.rev()); //! } //! } //! //! impl<'dep, M> Construct<'dep> for SportsCar<M> where M: ?Sized + Deps { //! //! type Dep = Co<M, Engine>; //! //! fn __construct(engine: Self::Dep) -> SportsCar<M> { //! SportsCar { //! engine: engine, //! } //! } //! } //! //! #[bind(Engine = "GranCabrioV8#Prototype")] //! trait Module { } //! //! type ModuleDependencies = Graph<Module>; //! //! let m = ModuleDependencies::new(); //! //! let car: SportsCar<_> = m.construct(); //! //! car.gas(); //! # } //! ``` //! //! [1]: http://misko.hevery.com/2008/08/01/circular-dependency-in-constructors-and-dependency-injection/ pub use core::{Co, Component, ComponentImp, Construct}; pub use graph::Graph; pub use lifecycle::{Prototype, Singleton}; mod core; mod graph; mod lifecycle;