Exemplo n.º 1
0
func (p FBInjectInstanceInitializer) Initialize(instance interface{}, typeName string) (interface{}, error) {

	var g inject.Graph

	if err := g.Provide(&inject.Object{Value: instance}); err != nil {
		return nil, err
	}

	if entry := p.initializers[typeName]; entry != nil {
		for _, v := range entry.dependencies {
			resolved, e1 := godi.Resolve(v)
			if e1 != nil {
				return nil, e1
			}
			obj := inject.Object{Value: resolved}
			e2 := g.Provide(&obj)
			if e2 != nil {
				return nil, e2
			}
		}
	}

	// construct the instance.
	//
	if e3 := g.Populate(); e3 != nil {
		return nil, e3
	}
	return instance, nil
}
Exemplo n.º 2
0
func TestFbInject(t *testing.T) {

	// setup the initializer
	var inject = NewFBInjectInstanceInitializer()
	deps := []interface{}{(*D2)(nil)}

	inject.AddInitializer(T1{}, deps)

	godi.RegisterInstanceInitializer(inject)

	// register the dependency
	godi.RegisterTypeImplementor((*I1)(nil), T1{}, false, nil)
	godi.RegisterTypeImplementor((*D2)(nil), TD2{}, false, nil)

	instance, err := godi.Resolve((*I1)(nil))

	if err != nil {
		t.Error(err.Error())
	}

	i1 := instance.(I1)

	if i1.CheckCheck() != "hodor" {
		t.Error("Expected hodor")
	}

}