-
-
Notifications
You must be signed in to change notification settings - Fork 842
Allow Keyed Composites #1458
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
base: develop
Are you sure you want to change the base?
Allow Keyed Composites #1458
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## develop #1458 +/- ##
===========================================
+ Coverage 78.94% 78.99% +0.04%
===========================================
Files 200 200
Lines 5681 5693 +12
Branches 1145 1147 +2
===========================================
+ Hits 4485 4497 +12
Misses 699 699
Partials 497 497 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
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 think there's some logic that can be pulled/adapted/reused from CollectionRegistrationSource
to aid in perf/caching as well as collection type detection logic.
if ((ctx.Registration.Options & Registration.RegistrationOptions.Composite) == Registration.RegistrationOptions.Composite | ||
&& ctx.Service is KeyedService keyedService | ||
&& pi.ParameterType.IsGenericType | ||
&& (pi.ParameterType.GetGenericTypeDefinition() == typeof(IEnumerable<>) |
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.
Check out CollectionRegistrationSource
to see how we detect generic collections. There's an extension method IsGenericListOrCollectionInterfaceType
that can check but also caches the result so we don't have to look it up every time.
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 may be worth looking at that for detection of things like array parameters or collection types that are derived from an open generic (like public class MyCollection : IEnumerable<MyType>
).
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.
Thank you for the info. I moved to using the extension method that included the IEnumerable<> lookup. I looked through the rest of them to see if any of them already did my recursive lookup, there was not. I moved my lookup into the extension class as well, which feels better. I didn't know there was caching as I looked at the code for the first time on Saturday. I wrapped my lookup into it's own cache as well. Will look into the derived types.
…neric of service type code to InternalTypeExtensions, created cache for result.
I encountered a situation where multiple implementations of a service were registered with the same keys. A composite currently only wraps all implementations of that service that do not have a key.
The workaround involves creating the composite manually, instead of resolving it.
Currently the above line resolves only the last instance of
I1
registered with"1"
. I needed this to resolve a composite containing just the registrations with a specific key.This PR allows for that functionality. All behavior remains the same unless the composite is additionally registered with
.Keyed<I1>(...)
, then the above line will resolve an appropriate composite.Example from added unit test.
Still only one non-keyed service registration is allowed. Keyed registrations must be added to enable this feature.
It works by determining: the resolving service is a composite, it is resolving a parameter of an
IEnumerable<>
,IList<>
, orICollection<>
of the same service type, or the service type is nested in relationship types. Then it does a keyed service resolution using the composites key, else a normal typed resolution.Will resolve Lazies of a Function that takes an int argument (for demonstration only in this example) that returns an Owned Meta object for
S3
orS4
only.