Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overriding a value from a parent di container #50

Open
matdurand opened this issue Dec 8, 2023 · 0 comments
Open

Overriding a value from a parent di container #50

matdurand opened this issue Dec 8, 2023 · 0 comments

Comments

@matdurand
Copy link
Contributor

Is it possible to override a value that is already defined in a parent container?

For example, let's say I want to override the Logger for a specific DI container like this

type Logger interface {
}

type Logger1 struct{}
type Logger2 struct{}

func main() {
	root, err := di.New(
		di.ProvideValue(Logger1{}, di.As(new(Logger))),
	)
	if err != nil {
		panic(err)
	}

	child, err := di.New(
		di.ProvideValue(Logger2{}, di.As(new(Logger))),
	)
	if err != nil {
		panic(err)
	}

	err = child.AddParent(root)
	if err != nil {
		panic(err)
	}

	var lg Logger
	err = child.Resolve(&lg)
	if err != nil {
		panic(err)
	}
}

I get

 multiple definitions of main.Logger, maybe you need to use group type: []main.Logger
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant