-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Shell page dependency resolution optimization #4280
Comments
Can you explain this inflexibility a bit more? It may also help to have a small repro app showing the behavior you are not expecting. |
You're right; after a test I see that it does now work with multiple applicable constructors. My research found that this wasn't always the case, it used to throw an |
@brunck can we close this issue and the related PR? |
@PureWeen well, what do you think about the "opt-in" aspect, which would allow people to opt-out by not registering the page classes? Not being able to opt-out seems to have caused some problems for people. |
related |
@brunck if you can provide a repro of the problems people are seeing that would be useful. I posted a request for that on a related issue as well. The only problem I can come up with is if you had multiple constructors and you always want shell to pick the default ctor and never the constructor that has parameters. But it seems like people are hitting a different issue that I'm not having success at reproducing |
@PureWeen I added a test case to the PR that reproduces a problem described in the other issue, specifically where there is a parameterless constructor as well as a parametered constructor, and nothing is registered (no page classes and none of any classes used as parameters in the 2nd constructor). |
@brunck so it looks like this is this is the reason why we're all seeing a bit different results when testing this one out. So, until the issue referenced in that comment is fixed your approach might be the best. |
When I tested out dotnet/maui/maui today, that has 815ea34. I hit crashes on startup because this page has no default ctor: I had to add this:
@PureWeen do we need to fix this? file an issue? |
@jonathanpeppers this is specified above, in the description. The pages to be resolved by Shell with DI have to now be registered, otherwise they have to have a default constructor. |
@brunck right we need to at least document this in release notes or something? I was using MAUI Preview 13, then switching to main, the app started crashing. |
@jonathanpeppers yeah, I don't know that I'm authorized to do that/where best to do it, otherwise I'm happy to do what is needed. That makes sense; I believe this code change is in main but not in the P13 branch. |
Yeah, I'm relying on MAUI team to know what to do, thanks. |
Description
Currently, the Shell page dependency resolution mechanism uses
Extensions.DependencyInjection.ActivatorUtilities.GetServiceOrCreateInstance
. This allows Shell to create pages without having registered the pages with the service collection, and resolve any registered dependencies that the pages have in their constructor.This is fairly inflexible, however; the page cannot have multiple constructors that have dependencies that can be satisfied. It may also cause problems when the page's
BindingContext
is set in XAML, and possibly other issues.If this was changed to use
IServiceProvider.GetService
, it would allow for multiple constructors. For example, if you had dependencies registered such as:This would allow for a page constructed by Shell to have multiple constructors, with arguments that aren't resolved by the service provider, such as:
The first constructor will be selected by the resolution mechanism.
@PureWeen thoughts?
Public API Changes
The biggest impact is that for any code written using Preview 12, the pages to be constructed by Shell will, going forward, have to be registered with the service collection as part of the
HostBuilder
. If the dev doesn't do this, once this change was deployed, if they didn't have a parameterless constructor defined, theMissingMethodException
would be thrown. This would therefore be a breaking change, but only since Preview 12.This feature essentially then becomes "opt-in;" for any Shell pages defined with
DataTemplate
or constructed via Routing navigation, if the pages are not registered, they must have a parameterless constructor, as they will be created byActivator.CreateInstance
. If they are registered, they will be resolved byIServiceProvider.GetService()
, and any of the registeed dependencies defined in their constructor will be resolved.Intended Use-Case
This allows Shell to create pages without dependency resolution, and the pages can be constructed with dependency resolution if desired. It also allows multiple constructors to be defined, allowing more flexibility for the developer.
The text was updated successfully, but these errors were encountered: