-
Notifications
You must be signed in to change notification settings - Fork 11
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
Add Support for Keyed Registration #11
Conversation
Codecov Report
@@ Coverage Diff @@
## main #11 +/- ##
=======================================
Coverage 78.89% 78.89%
=======================================
Files 19 19
Lines 706 706
=======================================
Hits 557 557
Misses 149 149
Continue to review full report at Codecov.
|
public class KeyTypePair | ||
{ | ||
public Type Type { get; set; } | ||
public object Key { get; set; } |
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.
what is the decision behind using Object key
and not use String key
? is there any use case for non-string key? so we can avoid boxing unboxing.
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.
It's a good question, the original framework done in unit was using object as a key, so in order to make it work with the original code i needed to use object, we should probably move to string. But unsure what the impact is. I'll have a look
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.
I've updated it to string, its a breaking change to the unity implementation, but i doubt anyone is using it, the unity implementation was mainly published for historical reasons.
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.
I had my doubt about passing the dictionary while doing the autowiring and doing the registration of the key-values afterwards. Instead of just doing the key/type registration immediately. I understand we do need to check for duplicate key/type registrations so this seems to be necessary. If there is a way where we don't need the dictionary but instead use the container registration for the same checks could be more elegant
@joruaz that is how it was done in the original implementation. Because Unity the container is active while you are registering. So you can do things like this container.RegisterSingleton<IMyObject>(myObject);
/// later on
var tmp = container.GetService<IMyObject>();
// mutate tmp
tmp.MyList.Add(stuff);
// put tmp back in the container and override the singleton
container.RegisterSingleton<IMyObject>(tmp); With dotnet DI it has two stages
You cannot call the "GetService" until its registered and you are at stage 2, this is actually better because you can do nice validation (its almost as good as simpleinjector), but it lacks some of the flexibility. So we need someway to maintain state. Or I think another way to implement it could be to query with linq at the end might be a bit more elegant the doing it in the foreach, but I was trying to replicate code from the old implementation which is written like this, rather than thinking through too much.. I forget to think sometimes :) |
public bool IsRegistered(string key) | ||
{ | ||
return !_registrations.TryGetValue(key, out var implementationType); | ||
} |
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.
public bool IsRegistered(string key) => _registrations.TryGetValue(key, out var implementationType);
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.
there's a !
that shouldn't be there
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.
nice catch, htf did that pass the unit test is what i need to check to
resolves #3