Skip to content
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

Proposal: allow multiple implementation for the same interface and being able to return all them #33

Open
GregPja opened this issue Mar 30, 2023 · 6 comments
Milestone

Comments

@GregPja
Copy link

GregPja commented Mar 30, 2023

Hi! I am doing some research on DI for Golang and I find this library to be the perfect middle ground of simplicity, efficiency and static stability. There's one missing feature that at the moment doesn't seem to be possible (correct me please if I'm wrong).

There's no way right now to register multiple implementation of the same interface and then being able to retrieve all of them.
I come from Java, where with Springboot, you can inject a Collection<IService>.

Would that make sense to do it here too?
If you wondering "ok, but how do you handle the case of having 2 implementation of the same interface but your struct only needs one?"
In Springboot in this case you would have an error, because the framework wouldn't know which one to inject.. And we could have the same here too.

@Jictyvoo
Copy link

Jictyvoo commented Apr 3, 2023

I think that it's possible, but can be really slow, because we gonna try to discover all elements type that fits with the given interface. I'm a mantainer in another DI library, later today I gonna check if I can do this there.

@Jictyvoo
Copy link

Jictyvoo commented Apr 4, 2023

Yeah, that is possible to do

@GregPja
Copy link
Author

GregPja commented Apr 4, 2023

nice! That would same me quite some manual work. At the end I found a solution where I provide the list of all dependencies, but I then need to list them manually.

@Jictyvoo
Copy link

Jictyvoo commented Apr 5, 2023

nice! That would same me quite some manual work. At the end I found a solution where I provide the list of all dependencies, but I then need to list them manually.

Looks nice, can you share a piece of code that represents it?

@Jictyvoo
Copy link

Jictyvoo commented Apr 5, 2023

I don't know if have any problem by linking this here, but this is the PR that I made on the DI that I work on

@GregPja
Copy link
Author

GregPja commented Apr 12, 2023

nice! That would same me quite some manual work. At the end I found a solution where I provide the list of all dependencies, but I then need to list them manually.

Looks nice, can you share a piece of code that represents it?

type ISomething interface {
	
}

func Inject(i *do.Injector) ([]ISomething, error){
	return []ISomething{
		Impl1{}, Impl2{}, Impl3{},
	}, nil
}

func InjectService(i *do.Injector) (IService, error) {
 	return ServiceImpl{
		 somethings : do.MustInvoke[[]ISomething](i),
	}, nil
}

This is it basically. But of course it would be nicer to be able to register those ISomething one by one...
But I see also the limitation of this request. Maybe instead of having do.Provide(Inject,i) we could have an explicit do.ProvideMany and a do.MustInvokeMany that don't panic if there is more than 1 implementation

@samber samber added this to the v2 milestone May 7, 2024
@samber samber mentioned this issue May 7, 2024
50 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants