question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Shell page dependency resolution optimization

See original GitHub issue

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:

// ...
services.AddTransient<MainViewModel>();
// ...

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:

public TestPage(MainViewModel viewModel)
{
}

public TestPage(MainViewModel viewModel, OtherDependency arg2)
{
}

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, the MissingMethodException 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 by Activator.CreateInstance. If they are registered, they will be resolved by IServiceProvider.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.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:13 (13 by maintainers)

github_iconTop GitHub Comments

1reaction
PureWeencommented, Jan 28, 2022

@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.

https://github.com/dotnet/maui/issues/4318#issuecomment-1023732279

So, until the issue referenced in that comment is fixed your approach might be the best.

0reactions
jonathanpepperscommented, Feb 9, 2022

Yeah, I’m relying on MAUI team to know what to do, thanks.

Read more comments on GitHub >

github_iconTop Results From Across the Web

[Enhancement] Give developers full control over Shell view ...
The solution below would address the issues of there being a lack of dependency injection support for shell views as well as fixing...
Read more >
Dependency resolution in Xamarin.Forms
This article explains how to inject a dependency resolution method into Xamarin.Forms so that an application's dependency injection ...
Read more >
Dependency injection
Dependency injection (DI) in Optimizely Content Management System (CMS) is based on the DI framework in ASP.NET Core located in the Microsoft.Extensions.
Read more >
Configure Shell modules
Describes how to configure custom modules like gadgets and plug-ins, to be used in the Optimizely Content Management System (CMS) user interface.
Read more >
Python Dependencies - Everything You Need to Know
Dependency Resolution – automatically ensures that all dependencies pulled in by a package are compatible with the rest of your Python ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found