-
Notifications
You must be signed in to change notification settings - Fork 206
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
Add ability to directly call a constructor through dig #21
Comments
Interesting. A few questions -
|
|
Ok, We can do this in 3 ways - Option 1: return result := c.Call(func(o1 Object1, o2 Object2) (Ret1, Ret2, error) {
return Ret1{}, Ret2{}, nil
})
c1 := result[0].(*Ret1)
c2 := result[1].(*Ret2) Option 2: variadic arguments for Call function allows objects to be resolved in the call itself. var c1 Ret1
var c2 Ret2
result := c.Call(func(o1 Object1, o2 Object2) (Ret1, Ret2, error) {
return Ret1{}, Ret2{}, nil
}, c1, c2) Option 3: The Call function resolves and executes provided function, that assigns the resolved objects to the defined vars. var c1 Ret1
var c2 Ret2
c.Call(func(o1 Object1, o2 Object2) error {
c1 = Ret1{}
c2 = Ret2{}
return nil
}) |
I'm really confused about Option 3. Lets say I wanted to use it with the and Call How would my usecase look? I may not always be able to control the innards of the function to extern values from it. |
I cleaned up a bit since there is no need of result. but something like this? - var d Dispatcher
c.Call(func(cfg Config, l Logger) error {
// Initialize dispathcher based on configuration and logging
initialized_dispatcher := NewDispatcher(cfg, l)
// assign initialized dispatcher to `d`
d = initialized_dispatcher
return nil
})
// use `d` constructed but not registered in dig. |
I see. The really interesting thing for me for Option 3 is that we can completely remove Consider the following setup: c := dig.New()
// type1 -> type2 -> type3
c.Provide(func(*type1) *type2{})
c.Provide(func(*type2) *type3{}) In the current API, to get an instance of both type2 and type3 the following is required:
If we obsolete the two and rely more on the call/invoke functionality, we can determine which objects of type need to be extracted from the graph based on the function provided, i.e.:
I think there is something interesting in here... |
I am wary of using Invoke to replace Resolve because of locking. We need to remove locking for Invoke/Resolve if we want to use it extensively for callback. |
* Add hook to register custom YARPC dispatcher This will be used internally to override the YARPC dispatcher. GFM-112
Let's say that I have a constructor
func (A, B) (C, error) { // codez }
.In order to partake in dig, I'd have to first Provide said constructor it to the graph, then declare a new variable of type C, and ask the graph to resolve:
I propose to add the
Call
function toContainer
interface, to make this process a lot smoother.Call
would execute the function, inject the dependencies (without permanently adding it to the graph) and return the interface{} result (ready for casting).The new experience would be:
Not only this improves the API, but also allows users to create multiple objects of the same type that should they choose to.
The text was updated successfully, but these errors were encountered: