Exemplo n.º 1
0
func ExampleDependencyInjector_Create() {
	type Atype struct {
		MyVar int
	}
	type Btype struct {
		a Atype
	}

	atypeBuilder := func(myvar int) Atype {
		return Atype{MyVar: myvar}
	}

	di := depinject.NewDependencyInjector()

	di.MustRegister(func() Atype {
		return atypeBuilder(5)
	})

	di.MustRegister(func(myAtype Atype) Btype {
		return Btype{a: myAtype}
	})

	newBtypeInstance := di.Create(Btype{}).(Btype)

	fmt.Println(newBtypeInstance.a.MyVar) // returns 5
}
Exemplo n.º 2
0
func main() {
	di := depinject.NewDependencyInjector()

	di.MustRegister(func() CAR {
		return new(FordMondeo)
	})
	di.MustRegister(func(myCar CAR) FordFactory {
		return FordFactory{car: myCar}
	})

	myCarFactory := di.Create(FordFactory{}).(FordFactory)
	myCar := myCarFactory.makeCar()
	fmt.Println(myCar.getModel())
}