inject icon indicating copy to clipboard operation
inject copied to clipboard

Replacing already defined provider

Open yoyofx opened this issue 5 years ago • 4 comments

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 .

yoyofx avatar Dec 18 '19 12:12 yoyofx

@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)

defval avatar Dec 18 '19 12:12 defval

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.

yoyofx avatar Dec 18 '19 14:12 yoyofx

Whether it can replace the old type with the new type. In your code at (c *Container) Provide function , "if c.exists(k) ... panicf() " .

yoyofx avatar Dec 18 '19 14:12 yoyofx

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.

defval avatar Dec 18 '19 16:12 defval