Added TryAs in RegistrationBuilder - a version of As that doesn't throw#775
Open
jonah1nine wants to merge 1 commit intohadashiA:masterfrom
Open
Added TryAs in RegistrationBuilder - a version of As that doesn't throw#775jonah1nine wants to merge 1 commit intohadashiA:masterfrom
jonah1nine wants to merge 1 commit intohadashiA:masterfrom
Conversation
|
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Sometimes a concrete type could implement multiple interfaces and the instance of the concrete type needs to be registered against those interface.
Currently, we have the options below:
AsImplementedInterfaces()Straightforward and neat! However, I try to avoid using this as it hides away registrations of types. My IDE can't help me find where a certain type is registered.
Still relatively straightforward and neat! In addition, I can now easily see what the instance is registered against. My IDE can easily find these registrations.
Now, there are cases where it is not guaranteed that an instance implements a certain interface, but still needed to be registered if it is. Option 1 can handle this easily as it will only register against implemented interfaces. However, option 2 will fail and rightly throw an exception.
A way around this is to manually check the instance against the interfaces:
This is cumbersome the more interfaces you need to check against.
This PR adds a new version of
Asthat doesn't throw an exception if the instance does not implement the interface it's being registered against:TryAsWith this, we can write above similar to option 2:
Back to straightforward and neat!
Note: I went for a very simple implementation and just added a boolean flag to the
AddInterfaceTypemethod. Feel free to come up with a better implementation.