samber / do

⚙️ A dependency injection toolkit based on Go 1.18+ Generics.
https://pkg.go.dev/github.com/samber/do
MIT License
1.82k stars 75 forks source link

feature request: auto injection with struct tags #9

Open darluc opened 2 years ago

darluc commented 2 years ago

I used some other framework that could auto inject with struct tags.

Something like this:

type Bar interface {
  DoSomething()
}

type Foo struct {
  Bar `inject:""` // or we can use named value here `inject:"name"`
}

do.Provide(injector,  func() ( Bar, err){...})

f, err := do.Invoke[Foo]() // f.Bar should be injected automatically
f.Bar.DoSomething()
sillydong commented 2 years ago

This is an interesting feature, but will requires reflect to parse the structure and get its tags.

kcmvp commented 2 years ago

another idea is that might a dsl is a good idea even better than struct tag

1: instead of spreading of all the dependencies around the code base, all the dependencies are defined in a dsl. it's very clear from the point of architecture. ( java ecosystem springboot supports xml configuration)

2: dsl is more expressive

3: building a dsl from scratch is not simple task, we can build it base on https://github.com/hashicorp/hcl

samber commented 1 year ago

The DSL option is nice when you have existing tooling. This library should be dead simple, with no IDE plugin or CLI.

I will create a small PR with a proposal.