-
-
Notifications
You must be signed in to change notification settings - Fork 107
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
Allow custom service providers like autofac #1233
Allow custom service providers like autofac #1233
Conversation
@dotnet-policy-service agree |
Revert code changes that were done by automatic clean up.
Hi @inf9144. interesting, thanks. Can you explain why the fallback service provider feature isn't good enough for what you need? |
Hi @egil, //EDIT: Dont know if it get's clear enough: Fallback is a oneway street which doesnt work if you have dependencies in both containers that rely on something that is in the other container ;-) //EDIT2: Copy the registrations is also not possible because autofac has a bigger feature set which can't be projected onto a simple registration in an MS DI container. |
Thanks for making the use case clear. I am cautious about adding new public API surfaces to bUnit since I have to support it now and in the future. This is an excellent point though. The fallback provider does not have access to the primary provider. This feature would, to some extend, make the fallback service provider feature obsolete though. That is OK though.
It is definitely one of the goals!
Ill take a look at the code as soon as possible get back to you. @linkdotnet, please also add your two cents. |
Co-authored-by: Egil Hansen <[email protected]>
This change does require an update to the docs. A new section on https://bunit.dev/docs/providing-input/inject-services-into-components.html should do it. Do you feel up to contributing to that too @inf9144? It would ideally be a description of the usage of the new methods and an example of their usage. It is important to point out that bUnit also uses the provider internally, so any replacement providers must behave according to the IServiceProvider contract. |
The feature looks solid in itself and I do understand the reasoning. My only concern is the API itself, which becomes more convoluted. We introduce more and more ways to use DI - where this is nice, my look wanders to the future where we might go ahead and move that responsibility solely to the user, and bUnit just offers anchor points to let its service be registered in said container. |
Adding generic notnull constraint to UseServiceProviderFactory
…thub.com/inf9144/bUnit into support-for-autofac-dependency-injection
InitializeProvider public => private
That sounds very reasonable, but would require a rework of TestContextBase/TestServiceProvider, which I can't do at the moment because I can't estimate all the required code changes and their impact on the public API. |
Documentation
Commited. I tried to follow the style of your paragraph about the fallback mechanism :-) |
Thanks, appreciate that. We may look at this as an option in v2. It would be a breaking change for v1, so its not on the table right now. |
docs/site/docs/providing-input/inject-services-into-components.md
Outdated
Show resolved
Hide resolved
Sorry, bringing up the topic about changes of the DI container was not meant in your direction to implement something, more like a consideration. We onboard new API's that might be dropped in the future. |
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.
Overall very good job. You pretty much nailed the code style.
Here are some suggested changes to the code which I think makes it a little easier to read.
After RC.2 has been released, a few of our tests stopped working. Have pushed a fix to main that you need to rebase into your branch though. |
Co-authored-by: Egil Hansen <[email protected]>
…utofac-dependency-injection
Fix code from code review applied changes
Fix code from code review applied changes
Documentation
I applied your recommendations, updated the branch from bUnit/main, documentation has now an section with Autofac sample and the tests (TestServiceProviderTest) are green ;-) |
#if !NETSTANDARD2_1 | ||
[MemberNotNull(nameof(serviceProvider))] | ||
#endif |
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.
Note to self: copy and make https://source.dot.net/#System.Private.CoreLib/src/libraries/System.Private.CoreLib/src/System/Diagnostics/CodeAnalysis/NullableAttributes.cs internal and scoped to NETSTANDARD2_1
to bUnit so we can avoid having #if !NETSTANDARD2_1
in the code. That way we can drop the serviceProvider >>!<< .GetService(serviceType)
below, even in netstandard2_1.
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 we are there, more or less. Any things you want to add @linkdotnet ?
Hmm the Ubuntu runner is running again for hours. I am not sure what is going on or causing this, but we didn’t see that behavior on main. Would like to run it again a few times or even locally to understand that we didn’t introduce something here. edit: I saw multiple builds were stuck. |
It is stuck again. edit: there is also a stuck build in another dependabot PR run. So it seems we might have that issue on main. I suspect our recent DisposeAsync change. that said the pr is fine for me and can be merged. |
I don't see how these changes should cause that. There have been some weird behaviors of the runners before. |
It is. Let’s take that discussion offline as it isn’t caused by the PR. |
ok, ill merge this in and we can take a look at the disposeasync stuff tomorrow. @inf9144 thanks for the effort on this! |
Pull request description
ASP.NET Core / Blazor allows for custom containers via IServiceProviderFactory.
This pull request adds this ability to the TestServiceProvider of bUnit.
Two times requested, I also need this and you told that you would like to have that as PR ;-)
See:
#268
#213
PR meta checklist
main
branch for codeor targeted at
stable
branch for documentation that is live on bunit.dev.Code PR specific checklist