MAUI Patterns API

Explore the architectural patterns and design principles recommended for building robust and maintainable .NET MAUI applications.

Common Architectural Patterns

These patterns help structure your MAUI application for scalability, testability, and maintainability.

Model-View-ViewModel (MVVM)

MVVM is a UI design pattern that separates application development in three distinct components:

Key features of MVVM in MAUI:

Model-View-Presenter (MVP)

MVP is another UI architectural pattern, similar to MVVM but with a slightly different interaction model.

While MVVM is more prevalent in MAUI due to its strong binding support, MVP can be a viable alternative in certain scenarios.

Design Patterns

These are general design patterns that are highly beneficial when developing with .NET MAUI:

Dependency Injection (DI)

DI is crucial for building loosely coupled, testable, and maintainable applications. .NET MAUI has built-in support for DI, making it easy to manage services and dependencies.

Key concepts:

// Example: Registering a service
builder.Services.AddSingleton<IMessagingService, MessagingService>();

// Example: Injecting a service into a ViewModel
public class MyViewModel : INotifyPropertyChanged
{
    private readonly IMessagingService _messagingService;

    public MyViewModel(IMessagingService messagingService)
    {
        _messagingService = messagingService;
    }

    // ...
}

Command Pattern

The Command pattern encapsulates a request as an object, allowing for parameterization of clients with different requests, queuing or logging of requests, and support for undoable operations.

In MAUI, this is primarily implemented using ICommand, often via RelayCommand (from Community Toolkit) or custom implementations.

public ICommand SaveCommand { get; }

public MyViewModel()
{
    SaveCommand = new Command(async () => await SaveAsync());
}

private async Task SaveAsync()
{
    // Save logic here
}

Singleton Pattern

Ensures that a class has only one instance and provides a global point of access to it. Useful for services that should only have one instance throughout the application's lifetime.

Observer Pattern

Also known as the Publisher-Subscriber pattern. Useful for event handling and communication between objects where one object (the subject) maintains a list of its dependents (observers) and notifies them automatically of any state changes.

MAUI's INotifyPropertyChanged interface is a prime example of the Observer pattern in action, used extensively with data binding.

Service Location vs. Dependency Injection

While both patterns help access services, Dependency Injection is generally preferred in modern application development for its benefits in testability and decoupling.

.NET MAUI's built-in DI container promotes the latter.

Further Reading