-
-
Notifications
You must be signed in to change notification settings - Fork 89
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
feat: add autowire/autodiscovery/auto-initialization of interfaces to classes #501
Conversation
Oooh, I like this! I'll probably review the PR tomorrow, but I'm pretty confident I'll merge it :) |
Pull Request Test Coverage Report for Build 11112271316Warning: This coverage report may be inaccurate.This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.
Details
💛 - Coveralls |
I appreciate your contribution @Jeroen-G! However, Tempest already has a Singleton attribute that handles service injection directly into the container. Could you clarify how this autowiring feature complements or improves upon that, and whether it addresses scenarios where the existing attribute falls short? |
Did you try out the example in the PR description? That is something the Singleton attribute cannot do. Automatically wiring interfaces to implementation is different from registering a singleton implementation. So that is where this feature complements upon :) |
Could you give a code example? |
This PR allows to tell the container: when I want a GreetingInterface give me this implementation. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is great!
I've got one idea though I wanted to pitch though, but I'm not sure I like it myself.
I don't really like too many attributes on a class. I was wondering if it would make sense if we combined the autowiring attribute and singleton attribute into one, something like:
#[Singleton(for: Interface::class)]
I don't think it's better than this, but I wanted to pitch the idea just in case.
Pragmatically I could go with this. Conceptually however a singleton is meant to restrict instantiation of a class to one instance, which I think is not necessarily the case here. If you would like to merge them, I would propose: #[Autowire(singleton: true)] Semi-related, if this gets merged I would also look into opening a new PR for a Configure attribute. |
I actually think just making the combination of both attributes Not sure yet how I feel about Btw: are you on Discord? |
I think we're ready to merge? Can you rebase the branch on main? |
Super nice! I'll write some docs for this as well :) |
Cool! Thanks, and I see you already found my discord handle :) |
Rationale
First of all, it is exciting to play with this new framework! I like a lot of it already, but I hit one snag when building some services. I usually have an interface for them (for example to make mocking easier). But with Tempest it looks like this would require a initializer class. And I am too lazy to make those for all my services 😜
A while ago I made an Autowire implementation for Laravel (link) so I thought I would start porting that a little bit to Tempest. So this first PR adds the Autowire attribute which allows autodiscovery of classes (or maybe auto initialization is a name better fitting to Tempest).
Let me know what you think!
How to use it
GreetingController:
GreetingInterface:
GreetingService: