Comments (6)
Wire intentionally does not have a notion of subcomponents at the moment. In talking with the Dagger team, we discovered that subcomponents and scopes introduce a fair amount of complexity.
As you say, you can get much the same behavior by returning the singletons from the first injector that creates them and then pass them to later injectors. This has the benefit of making the dataflow explicit, which for the examples we came up with, seemed like a net win. That said, we're very curious to see how folks will use Wire in real-world applications: if this does not scale, we might have to revisit.
EDIT: I realized after looking more closely at your sample that the component itself is stateful (a detail I had forgotten about Dagger). My explanation above is still largely applicable: we want state to be explicit.
from wire.
FWIW, in the meantime I'm using a technique similar to your Options
example to inject a struct with all the values I need. The disadvantage of this is that each value is eagerly injected regardless of when it is needed, but it certainly works for now.
Thanks!
from wire.
For reference, here is a minimal example that shows the behavior I'm describing in a dagger 2 project:
https://github.com/drewolson/dagger_maven_example/tree/singleton_across_providers
This is the test that asserts the behavior.
from wire.
EDIT: I realized after looking more closely at your sample that the component itself is stateful (a detail I had forgotten about Dagger).
Yes, this is where I was going with this conversation. Perhaps, in the future, there could be some kind of alternative injectors that are attached to a struct where state is tracked. I definitely agree that starting out simple and waiting for real-world experience reports is the right approach.
Thanks again for this great library, I look forward to using it in my projects.
from wire.
@drewolson I found this issue 5 years later while looking up the same thing - how to share dependencies across injectors (eg reusing one DB connection-pool among multiple DAOs). Unfortunately, the links you posted are dead now and I'm not sure what you're referring to when you mention the "Options
example".
Could you please share some updated links (or better, sample code) to explain how you managed to share dependencies between injectors? Also, what is the "Options
example" you were referring to?
from wire.
After some doing some more research, I found issue #260 where they basically resolve this issue by declaring an App
super-container struct that has all the top-level dependencies for the application. Then there's only a single injector declared in the wire.go
file which will naturally instantiate all dependencies once and then reuse them.
Sample code:
wire.go
type App struct {
*services.AuthService
*services.FooService
}
var daoSet = wire.NewSet(
dao.NewAuthDAO,
dao.NewFooDAO,
)
var svcSet = wire.NewSet(
services.NewAuthService,
services.NewFooService,
)
func InitializeApp() *App {
wire.Build(
dao.InitDB,
daoSet,
svcSet,
wire.Struct(new(App), "*"),
)
return nil
}
Then after generating the wire_gen.go
file by invoking wire
in the terminal, we can invoke InitializeApp()
to get the dependencies we need.
For the Options
stuff, I found this doc page about using an Options struct which is what I assume you were referring to: https://github.com/google/wire/blob/main/docs/best-practices.md#options-structs
// A provider function that includes many dependencies can pair the function with an options struct.
type Options struct {
// Messages is the set of recommended greetings.
Messages []Message
// Writer is the location to send greetings. nil goes to stdout.
Writer io.Writer
}
func NewGreeter(ctx context.Context, opts *Options) (*Greeter, error) {
// ...
}
var GreeterSet = wire.NewSet(wire.Struct(new(Options), "*"), NewGreeter)
from wire.
Related Issues (20)
- Missed receiver in generated inject method
- Support for concurrent initialization
- function generated by Wire_gen.go "Cannot find declaration to go to" by Golang JetBrains but still run success HOT 5
- Calling wire.Build with a provider set from a structure variable (For modular buildings)
- wire `sync.Map` throw error HOT 2
- I has bean tried for two days, but failed. I define four struct and it's constructor, I define a init function in wire.go, but I can
- I can't use the init function which from my wire.go in my main.go HOT 3
- wire.NewSet support generics HOT 1
- internal/wire: update the path of wire which go generate run wire cmd
- Wire: not enough arguments in call to xxx
- is wire.FieldsOf support any field ? HOT 1
- Support for request scoped DI code generation HOT 2
- Injecting the subpackage into the parent package results in the error "no provider found for invalid type". HOT 2
- How to use wire.Bind() if concrete implementation is resolved at runtime? HOT 2
- please create new tag for 0675cdc
- Crashing with Go 1.22 HOT 3
- go generate ./... no longer works with go 1.22.0 and wire 0.6.0 HOT 3
- go build -mod=vendor ./cmd/wire doesn't work after update to 0.6.0
- go: -mod may only be set to readonly or vendor when in workspace mode, but it is set to "mod" HOT 2
- Sometimes it succeeds, sometimes it fails HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from wire.