Reputation: 2280
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.
ViewModels should be mediators between Models and Views, focusing on data transformation and state management. Yet ICommands in ViewModels often end up handling:
await Shell.Current.GoToAsync("../")
)This seems to violate the Single Responsibility Principle by making ViewModels do too much.
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.
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.
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?
I'm considering an alternative where:
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