Shell navigation and ViewModel instantiation - Do we need Dependency Injection and/or Query properties #7293
Unanswered
JosHuybrighs
asked this question in
Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
The proposal I would like to make regarding ViewModel assignment to a BindingContext in any type of Page, whether it is the MainPage, or a Tab page, or a Page that must go on the navigation stack, is probably controversial but in my experience any currently available solution for me looks overly complicated.
I am not an advocate of Dependency Injection or the Forms derived method of Routing and passing data in AppShell using query properties because I think that in almost 90% of the situations it is much simpler to use the following approach:
No Dependency Injection, no fancy data parameters. If everyone talks about Dependency Injection then that doesn't necessarily means that it is a good concept for every app design. As I mentioned above I think it almost never is. With a ViewModelFactory the ViewModel that triggers a new page almost invariably is the one who knows about properties that will be important for the yet to be created ViewModel. What is then the advantage of having a seperate factory service when the triggering ViewModel can immediately create the new ViewModel. Except maybe in development situations where different people or working on different ViewModels and not all of them or available during test. Does that happen often? It seems that not allowing simpler ways to instantiate pages is hurting a lot of one-developer organisations.
In order for the above to work, AppShell navigation, and in particular
Routing
andShell.Current.GoToAsync()
must be replaced by something that simply allows to push a Page on the stack and pass ViewModel as argument to the Page constructor.The latter can easily be accomplished using:
(Page)Activator.CreateInstance(PageType, ViewModel);
Any thoughts on this?
Beta Was this translation helpful? Give feedback.
All reactions