-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
MVC: get the incorrect services in controller #1342
Comments
Alright, I will check that when I am back at my office @zheeeng, stay tuned. |
Thx, if it is confirmed, is it possible to release a quick fix cos my project is just upgraded to the latest version but these undesired behaviors stunned me. |
Actually there is no framework-error here, it just binds one type to one value, both fields you want to set are actually empty |
There is no way that framework knows what you want to bind if all fields types accept any value. except those two:
@zheeeng these all are not real-world scenarios though, it looks more like "find an issue on an extreme scenario -- possible user's miss use of the feature" to me. |
Sadly, this behavior seems to be a burden to developers. Fields are matching by the structure instead of its nominal type.
This happens on feature refactoring. I deleted all the implementation and kept the empty service shell for future filling. |
But binding is not wrong, it matches one value per one type by order, how a program can detect what you want to bind if all fields are bindable(compatible types) with the binding value, the only way is order or naming. Do you want to add an API for naming match too? |
You may name your interface as |
Sorry, I'm not pro at Golang, I'm just using the experience from Angular annotation DI and TypeScript decorator DI. Cos in JavaScript the |
in Go, take for example that you have that interface: type Something interface { }
type Anything interface{ } ^ They are exactly the same as empty interface{}, they can be anything. And you have two values that are
And you have a value that you want to bind to, remember those fields can accept a So even if you do something like: func newService() Something { return &customStruct{} }
func newOtherService() Anything { return &customStruct{} or anything } Those both can be bind-ed to both fields because they both ( Did I helped or..? |
Thx, I figured out your points. You mean that if two services have the same interface methods but with different implementations, the order matters to runtime, am I right? |
Yes, in Iris only one value can be bind-ed to one field (see In these cases like this issue and the next one you posted, the order is only way that a go program can understand, it's logical if you think of it. However, we can add a register by field name if you wish so, this can fix your issue - (still I recommend not to use empty interfaces without reason) |
Generally, I don't expect extra APIs are added for hacking or used for instructing the program going. Ideal APIs are all about business instead of the language. Thx for your replies. |
Try this code and test the output.
The register sequence is: fortyTwo, twentyFour
In the controller, we declare the injected services as the sequence: TwentyFour, FortyTwo
I try to output as the sequence: FortyTwo, TwentyFour --> '&{42}|&{24}'
Actually, the output keeps the same sequence of registering sequence: '&{24}|&{42}'
If change the controller to:
We get the right output!
It's very unsafe and brings some unexpected bugs
The text was updated successfully, but these errors were encountered: