MVVM101

Scaerietale
2 min readNov 21, 2023

--

When I started learning programming two years ago, I actually already had some C# experience, so that was the first language I took any formal, structured classes in. Since then I dabbled in JavaScript (full stack), Python (scripting and back end), Go (backend), but when trying to decide on a stack to really focus on and drill down, I came back to my first love, C#.

With .NET 8 bringing some important bug fixes to MAUI it was time to finally learn to use it, and I quickly ran into the MVVM pattern. What the heck is the MVVM pattern? Rather than waste your time with anecdotes and analogies like was done to me, forcing me to spend an entire night trying to find a straight answer, I’m going to give it to y ou up front (You can thank me later).

MVVM stands for Model, View, ViewModel.

MODEL is your data, whether that’s a simple list, data you get from an API externally, or a database. All data is part of the Model.

VIEW is what’s written to the screen, AKA your “viewport”. Anything UI related, like how your text looks, what color your buttons are, etc. are all handled under View.

VIEWMODEL is the business logic of your application, or the “backend” (except it’s not really a backend, see my above complaint about bad analogies).

If you need your app to tell the user what 2+2 is, how a button should work, or what data from Model needs to be rendered when they push that button, this is where it’s written.

It’s separation of concerns. The idea is that if you’re having a problem with your logic not working, you should be able to go straight to the ViewModel file that’s related to that View page, and not have to wade through thousands of lines of combined code.

I hope someone else found this useful. I spent far more time than I needed to just trying to find a simple, straight answer though, and it aggravated me enough to write this, in the hopes of saving others in the future.

--

--

Scaerietale
Scaerietale

Written by Scaerietale

I am a legally blind musician, author, programmer, and ADHD advocate

No responses yet