inject
inject copied to clipboard
Replacing already defined provider
I want to add duplicate type name within an container.providers,and then I can't found replace method of v.2.x yet. that it had in v1.x .
My intent was replace old type with new registered type. and then get new type with Container.Extract .
@maxzhang1985 Is this for testing purposes?
At the moment, I doubt the need for an inject.Replace()
method because the replacement can be carried out without it.
In v2
you can do it like this:
var options []inject.Option
// for example, you can get test flag from the environment
if test {
options = append(options, inject.Provide(NewTestDependency))
} else {
options = append(options, inject.Provide(NewDependency))
}
container := inject.New(options)
I want to design a framework, preset some interfaces and implementations, and then let users add their own implementations of these interfaces. Then there is still such a need for delay selection replacement.
Whether it can replace the old type with the new type. In your code at (c *Container) Provide function , "if c.exists(k) ... panicf() " .
I want to design a framework, preset some interfaces and implementations, and then let users add their own implementations of these interfaces. Then there is still such a need for delay selection replacement.
You can try to handle it at the framework level.
Whether it can replace the old type with the new type. In your code at (c *Container) Provide function , "if c.exists(k) ... panicf() " .
I may return this feature but it will take some time.
In addition that needs to replace the provider itself, needs to replace provider in a group and in interface alias if inject.As()
used.