-
-
Notifications
You must be signed in to change notification settings - Fork 214
Injection by ID #764
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
Injection by ID #764
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
@migus88 I encountered a problem, multiple registration for single interface does not work as expected. Inject IEnumerable will always contain zero elements if the method with ID was used. |
@softshadedev thanks, I will take a look at it! I also want to see how I can move the registration with ID to use the |
Well, on my side - it passed all checks I wanted it to pass and the PR is now ready. |
@migus88 I am positive about merging it. There are two reasons for the delayed response. First, I have less time to devote to this project. I apologize for keeping you waiting. Second, as I've mentioned in previous issues, I've been somewhat hesitant about this feature. I have been considering this PR and the feedback. Therefore, I plan to merge it in principle. |
Hey @hadashiA, first of all - huge thanks for the fantastic work you’re doing. I completely understand your reasons for taking the time on this PR. On a personal note: a few years back, I was in a similar position. Initially, I was skeptical too - but working in an environment where this approach was heavily used helped me grow to really like it. At the end of the day, almost any pattern can be abused, but that alone shouldn’t disqualify its potential when applied thoughtfully. 🙂 |
Apologize for the delayed response >< I would like to request two corrections regarding the public API.
Based on the above, my proposal is as follows. // Registration
builder.RegisterType<Weapon>().Keyed(SomeEnum.Primary); // Constructor Injection
public SomeClass(
[Key(SomeEntry.Primary)] Weapon primaryWeapon
)
{
// ...
} I welcome any other opinions or counterarguments. |
I’m not sure about the name Regarding |
@hadashiA anyway, I've pushed the change both with |
@migus88 Thanks for the quick response on this!
You're probably right that WithKey is more straightforward and easier to understand.
Fair point here too, but I personally prefer the conciseness of Anyway, thanks again for the quick fixes! I'm happy with the API as it is now. I'll do a quick check to make sure there aren't any major functional issues and then merge it. |
Merged. Thanks so much. |
Awesome news!!! Thanks! |
Description
As a long time fan of this DI framework, I use it constantly, but I was always missing one feature that I got really used to in Zenject - injection by ID.
I know we can use Factories and that's what I did up until recently, but since I had a little spare time, I wanted to implement the feature in VContainer as well.
My aim was to not introduce any regression and to do it as performant as possible.
Usage:
Benchmarks
Implementation Change
In order to support resolution by ID, I had to turn the
FixedTypeKeyHashTable
class intoFixedTypeObjectKeyHashtable
.I've created a quick benchmark (using BenchmarkDotNet) for the class (see the benchmark here) and here are the results for it:
We can see that the performance of
FixedTypeObjectKeyHashtable
is pretty comperable toFixedTypeKeyHashtable
. Slight slowdown is expected due to hash combination.Built in benchmarks
I've fixed and ran the built in benchmarks both on


master
and on my branch. There is a slight performance degradation during container building due to a bit more complex registration. Resolving is unchanged (the benchmarks show that in my branch the resolving is a bit faster, but I think it's inside a margin of error).