Rzassar
Rzassar

Reputation: 2280

Is .NET MAUI CommunityToolkit.Mvvm ICommand a bad practice in ViewModel in .Net MAUI?

I've been working on a .NET MAUI application using the MVVM pattern, and I'm starting to question the common practice of putting ICommand properties in ViewModels. While this seems to be the standard approach in many tutorials and examples, I'm concerned it violates clean architecture principles.

My concerns:

1. Mixing of responsibilities

ViewModels should be mediators between Models and Views, focusing on data transformation and state management. Yet ICommands in ViewModels often end up handling:

This seems to violate the Single Responsibility Principle by making ViewModels do too much.

2. Increased coupling

When ViewModels contain commands that perform navigation or UI-specific operations, they become tightly coupled to the UI framework, making them harder to reuse or port to different platforms.

3. Testing difficulties

Commands that perform navigation, show dialogs, or interact with framework services require extensive mocking in unit tests, which adds complexity and can make tests brittle.

4. Architectural clarity

In a clean architecture, shouldn't data operations be handled by services/repositories, and navigation by dedicated navigation services? Why should a ViewModel have commands that directly save data or navigate between pages?

Alternative approach

I'm considering an alternative where:

  1. ViewModels expose simple methods and properties without ICommand
  2. ViewModels raise events when operations complete or state changes
  3. View code-behind subscribes to these events and handles UI-specific concerns
  4. Services handle all data operations
  5. Navigation is managed by a dedicated service that the View interacts with

Am I overthinking this, or is there merit to removing ICommand properties from ViewModels in favor of a stricter separation of concerns? Are there significant drawbacks to this alternative approach that I'm missing?

I'd especially appreciate hearing from developers who have worked on larger, more complex MAUI/Xamarin applications where maintenance became an issue.

Upvotes: -1

Views: 44

Answers (0)

Related Questions