You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Inspired by this discussion.
Extension methods for registering Views and their associated ViewModels in a IServiceCollection. Shell routes can also be specified.
Motivation
Implementing the MVVM pattern for MAUI and Xamarin can be abstracted away with extension methods. This would save time on the developer's end while keeping MauiProgram declarative without the need to create a separate class for bootstrapping. It can also guide developers into using best practices; for example: transient service lifetimes for Views.
/// <summary>/// Adds a View and associated ViewModel to a <see cref="IServiceCollection"/>./// </summary>/// <typeparam name="TView">The View of type <see cref="BindableObject"/>.</typeparam>/// <typeparam name="TViewModel">The ViewModel which implements <see cref="INotifyPropertyChanged"/>.</typeparam>/// <param name="services">The <see cref="IServiceCollection"/> to add the View and ViewModel to.</param>/// <param name="lifetime">The <see cref="ServiceLifetime"/> of the View and ViewModel.</param>/// <returns>A reference to this instance after the operation has completed.</returns>publicstatic IServiceCollection AddViewAndViewModel<TView,TViewModel>(thisIServiceCollectionservices,ServiceLifetimelifetime= ServiceLifetime.Transient)whereTView:BindableObjectwhereTViewModel:class,INotifyPropertyChanged{switch(lifetime){case ServiceLifetime.Singleton:
services.AddSingleton<TView>().AddSingleton<TViewModel>();break;case ServiceLifetime.Scoped:
services.AddScoped<TView>().AddScoped<TViewModel>();break;case ServiceLifetime.Transient:
services.AddTransient<TView>().AddTransient<TViewModel>();break;default:throw!Enum.IsDefined<ServiceLifetime>(lifetime)?new InvalidEnumArgumentException(nameof(lifetime),(int)lifetime,typeof(ServiceLifetime)):new NotSupportedException($"{lifetime} not supported");}returnservices;}/// <summary>/// Adds a View and ViewModel to <see cref="IServiceCollection"/> and registers it's route./// </summary>/// <typeparam name="TView">The View of type <see cref="BindableObject"/>.</typeparam>/// <typeparam name="TViewModel">The ViewModel which implements <see cref="INotifyPropertyChanged"/>.</typeparam>/// <param name="services">The <see cref="IServiceCollection"/> to add the View and ViewModel to.</param>/// <param name="route">The View's route in <see cref="Shell"/>.</param>/// <param name="lifetime">The <see cref="ServiceLifetime"/> of the View and ViewModel.</param>/// <returns>A reference to this instance after the operation has completed.</returns>publicstatic IServiceCollection AddViewAndViewModelWithRoute<TView,TViewModel>(thisIServiceCollectionservices,stringroute,ServiceLifetimelifetime= ServiceLifetime.Transient)whereTView:BindableObjectwhereTViewModel:class,INotifyPropertyChanged{
Routing.RegisterRoute(route,typeof(TView));
services.AddViewAndViewModel<TView,TViewModel>(lifetime);returnservices;}
The extensions give developers less control over the service lifetime of individual Views and ViewModels; I can't have a transient View with a singleton ViewModel. It may lead to developers not understanding service lifetimes; for example: why Views typically have a transient lifetime.
The idea itself was discussed for the MAUI Community Tookit, if they were to go through with its implementation there may not be a need to add it here.
The text was updated successfully, but these errors were encountered:
Summary
Inspired by this discussion.
Extension methods for registering Views and their associated ViewModels in a
IServiceCollection
. Shell routes can also be specified.Motivation
Implementing the MVVM pattern for MAUI and Xamarin can be abstracted away with extension methods. This would save time on the developer's end while keeping
MauiProgram
declarative without the need to create a separate class for bootstrapping. It can also guide developers into using best practices; for example: transient service lifetimes for Views.Code Example
Repo containing sample can be found here.
Implementation
Usage
Drawbacks
The extensions give developers less control over the service lifetime of individual Views and ViewModels; I can't have a transient View with a singleton ViewModel. It may lead to developers not understanding service lifetimes; for example: why Views typically have a transient lifetime.
The idea itself was discussed for the MAUI Community Tookit, if they were to go through with its implementation there may not be a need to add it here.
The text was updated successfully, but these errors were encountered: