MVU or MVVM with MAUI App ?

This is a rather sensitive topic in the Xamarin community at the moment. MVU versus MVVM… Let’s see what we can say about it.

By the way , what is MAUI ?

It’s THE main topic of the community since last year… Microsoft App UI, shortened to MAUI, is a UI system to build Apps. MAUI “takes” the basics of Xamarin.Forms and makes it evolve greatly! Here is a “very simplified” summary of what MAUI is.

I will obviously have the opportunity to talk about it again during this year. The topics will be multiple and the discovery is vast 🙂

Ok now that we have understood the objectives of MAUI, MVVM and MVU what is it?

MVVM

MVVM is refined from MVC; it supports two-way data binding between View and ViewModel. This allows automatic propagation of changes inside the state of ViewModel to the View.

The MVVM pattern includes three key parts:

  • Model: Represents the data with validation and business logic.
  • View: View is active with behaviors, events, and data binding. View synchronizes with the ViewModel, which enables the separation of the presentation and exposes methods and commands to manage and manipulate the Model.
  • ViewModel: Separates the View from the Model, and exposes methods and commands to manipulate the data (Model). ViewModel does not need a reference to a view. The view binds its control value to properties on a ViewModel.

But for this article let’s just rest the basics in the form of a diagram:

MVVM Design Pattern

There’s a lot of project that let you implement MVVM in your app :

MVU

One of the UI patterns that has gained popularity is the Model View Update (MVU) pattern (also called the Elm Architecture based on where it first gained popularity), and variations on this.

Since I’m really just starting out getting familiar with this pattern, I am not going to try to explain it, there are much better places to learn about it 💬  from people who have been immersed in it. My goal here is more to make you aware of it, and I’ll post more in the future about it as I learn more.The C# implementation is based on Comet (here is a presentation by the creator).

As for MVVM, here is a diagram to talk about MVU

Managing State in Comet - DEV Community

We seem to have similar things, views, models and … Update. But it’s not !

The basic principle of MVU is immutability. Nothing can have an edge effect except a command. The View is written in C# or whatever in a “fluent” mode where everything follows each other with declination points. So already this is a big difference, no XAML all the UI is written by hand in code…

The View written in C# code displays the states exposed by the Model. So far so good. But when a command is executed it is the Update block that is invoked. What it will do is execute the command, which may change the state of the Model, and as the Model is not modifiable Update will return a new copy modified by the command. The Model being a new instance the View will display the changes but only what is different, a bit like the Hot Reload Xamarin.Forms mode in design time, but here we are talking about the runtime…

So MVU is a philosophical choice, that of immutability so dear to languages like F#.

So MVVM or MVU ?

If you want to reuse your Xamarin.Forms, WPF … code , and use your knowledge , you will want to stay in XAML mode and MVVM pattern

If you are an adventurer , a believer in the benefits of immutability and XAML is not your cup of tea (or you have no strong experience ) in that case go for MVU. It’s beautiful, clean and clear and totally adapted to the functional mindset.

In fact, it seems that there is no real choice or question to be asked, the two patterns are two different worlds , claiming an adhesion and a habit to their foundations. XAML on one side, C# code only on the other, strong decoupling logic with binding on one side, messages and immutability on the other, etc. The differences are such that the comparison becomes difficult to justify. We don’t compare cabbages and rabbits, as we learn in primary school maths… Here we are dealing with two things that are so different that comparing them doesn’t make sense. It just doesn’t make sense.

I will conclude by answering in an unclear way with a “Do as you wish, experiment, learn, discover, it is the most important to be able to decide”.

Leave a Reply

Your email address will not be published.