Skip to content
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

Dynamic ViewModelLookup #30

Open
IeuanWalker opened this issue Apr 17, 2024 · 1 comment
Open

Dynamic ViewModelLookup #30

IeuanWalker opened this issue Apr 17, 2024 · 1 comment

Comments

@IeuanWalker
Copy link
Contributor

Is your feature request related to a problem? Please describe.
Ability to add to Dictionary<Type, Type> ViewModelLookup in multiple places
https://github.com/matt-goldman/Maui.Plugins.PageResolver/blob/ff594dae8a38a20d365ca9748214a17df90aa052/src/Maui.Plugins.PageResolver/Resolver.cs#L13C5-L13C77

My Use case: We have an app with 100s possibly even a thousand pages, so to manage all the registration and configs, each area/ routes of the app has there own app builder to manage their section, f.e.
image

Currently in order to add the manual mappings they all need to be initialised at the same time, but in order to keep the code as maintainable as possible i'd like to keep the mapping next to the registrations.

Describe the solution you'd like
I see 3 possible solutions using the current code -

@IeuanWalker
Copy link
Contributor Author

IeuanWalker commented Apr 26, 2024

For now, I've added 2 new public methods to Resolver -

public static void AddViewModelLookup<T1, T2>()
{
    ViewModelLookup.Add(typeof(T1), typeof(T2));
}
public static void AddViewModelLookup(Dictionary<Type, Type> ViewModelMappings)
{
    foreach (var mapping in ViewModelMappings)
    {
        ViewModelLookup.Add(mapping.Key, mapping.Value);
    }
}

So now I can keep the mappings next to each of the registrations and in different files -

static class AppBuilderExtensions
{
	public static MauiAppBuilder RegisterWasteCalendar(this MauiAppBuilder builder)
	{
		builder.Services.AddTransient<AddressSearchPage>();
		builder.Services.AddTransient<AddressSearchViewModel>();

		builder.Services.AddTransient<DisplayPage>();
		builder.Services.AddTransient<DisplayViewModel>();
		Resolver.AddViewModelLookup<DisplayPage, DisplayViewModel>();

		builder.Services.AddTransient<RemindersPage>();
		builder.Services.AddTransient<RemindersViewModel>();
		Resolver.AddViewModelLookup<RemindersPage, RemindersViewModel>();

		builder.Services.AddTransient<AddPopup>();
		builder.Services.AddTransient<AddReminderViewModel>();
		Resolver.AddViewModelLookup<AddPopup, AddReminderViewModel>();

		builder.Services.AddTransient<EditPopup>();
		builder.Services.AddTransient<EditReminderViewModel>();
		Resolver.AddViewModelLookup<EditPopup, EditReminderViewModel>();

		return builder;
	}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant