Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add generic counterparts for TryResolveNamed, TryResolveKeyed and TryResolveService #1263

Closed
lukoerfer opened this issue Mar 24, 2021 · 6 comments · Fixed by #1287
Closed

Comments

@lukoerfer
Copy link

Problem Statement

The static class ResolutionExtensions provides a set of extension methods to manually resolve instances from a container. Some of these methods (e.g. Resolve and TryResolve) have a generic counterpart (Resolve<T> and TryResolve<T>), but others, namely TryResolveNamed, TryResolveKeyed and TryResolveService, dont.

Desired Solution

The generic counterparts could just call the non-generic extension methods and then cast the out parameter in the same way as the generic version TryResolve.

Alternatives You've Considered

Right know, one can either write their own extension methods or use other workarounds (e.g. resolve IIndex<string, T> and use TryGetValue as an alternative to TryResolveNamed).

Additional Context

None

@lukoerfer
Copy link
Author

I can offer to prepare a pull-request with the changes I described above. Right now, it seems like there are no specific tests for many of the extension methods in ResolutionExtensions, so please give advice if there would be a need for additional tests.

@alsami
Copy link
Member

alsami commented Mar 26, 2021

I can offer to prepare a pull-request with the changes I described above. Right now, it seems like there are no specific tests for many of the extension methods in ResolutionExtensions, so please give advice if there would be a need for additional tests.

If that's a valid use case and you feel like sending a PR our way that would be great. My feeling is that whenever there is something new added, adding tests for those specific bits makes sense.

@alistairjevans
Copy link
Member

I actually got a little confused myself the other day by the absence of generic forms of TryResolveNamed, so I strongly agree with @alsami; if you're willing to add the methods and associated tests, that would would be great.

As a related note, you can just about use if (ResolveOptionalNamed<IService>("name") is IService service) to work-around this right now, but it's not as nice as TryResolveNamed.

I'm not sure of the benefit of TryResolveService<> over TryResolve<>?

@v0idzz
Copy link
Contributor

v0idzz commented Aug 29, 2021

Since there has been no activity here for a long period of time, I decided to work on it and I already have some progress. However, as mentioned above, is there any benefit of TryResolveService<> over TryResolve<>? Or should I skip this one?

@alistairjevans
Copy link
Member

Thanks for picking this up @v0idzz.

I think TryResolveService would just be a duplicate of TryResolve, so yes, skip it.

@maulik-modi
Copy link

@lukoerfer , Do you have anything else to add?

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants