Struggling to keep using MVVM, feeling like abandoning it
Hi, i've been making a video editor for fun in my spare time and, while it functions, the code base is dreadful and adding new features takes so long and always leads to bugs because I'm basically shadowing all models with view models, in order to bind things in the UI
In my app, I have a VideoEditor class, which contains the active Project, containing the main Timeline, which contains a list of Track objects, which contains a list of Clip objects, so overall a somewhat deep hierarchy of objects. But in order to fully support binding, I have to create a complete shadow with view models: VideoEditorViewModel (the datacontext of the main window), ProjectViewModel, TimelineViewModel, TrackViewModel, ClipViewModel. And since tracks and clips are basically abstract, I have to create a view model for each type of model, and register some type of mapping between model and view model so that I can create view models from models.
But there's also this: tracks have add, remove and move events for clips and, while adding/removing clips is easy (model fires add or remove event, track view model responds), implementing a move operation is difficult; the model can't access view models, so how do I move the clip VM from the source track VM to target track VM?
Sorry for the ultra long post but trying to keep this app within the MVVM bounds is becoming more and more difficult. I experimented by accessing models directly in controls and it feels so much easier, although I have to manually handle all the appropriate events in the models (such as DisplayNameChanged, FrameSpanChanged; frame spans containing a pos and duration) instead of just relying on PropertyChanged+Binding in view models... but at least the code becomes much much easier to write and modify, and is more straight forward to me. Does anyone have any advice? It would be really helpful, thanks 😄
21 Replies
Community toolkit mvvm solved so many problems for me. I'm currently leading a greenfield project that heavily utilizes that library to make mvvm a breeze.
Communication between viewmodels is possible via Messenger.
I definitely do not map models to viewmodels. If you need to have an observable model just make it so... There is no rule that says a model cannot be observable, afaik. And even if, it's an unnecessary headache
There is also ReactiveUI as a very powerful MVVM framework
https://www.reactiveui.net/
Indeed a very powerful framework; however, from my research it can be a bit overwhelming due to its learning curve
They might help but idealy i'd still have the exact same problems of having to shadow alot of the models with view models, otherwise I don't really know how else to do it using MVVM
Why would you shadow your models with viewmodels?
Because otherwise i'd need to implement INotifyPropertyChanged in all of my models
At that point I might as well just not use model classes, and put all of my data in view models, which is also an option
But that also means view models would be doing things like rendering, exporting, automation/animation processing, etc.
Unknown User•11mo ago
Message Not Public
Sign In & Join Server To View
That's what I've been doing
Unknown User•11mo ago
Message Not Public
Sign In & Join Server To View
But I don't know what else to do except for create the same hierarchy of models but for view models
Timeline contains Tracks which contain Clips
Therefore I need TimelineViewModel containing TrackViewModels containing ClipViewModels
Unknown User•11mo ago
Message Not Public
Sign In & Join Server To View
I know about that kind of stuff, but in my use case of it for a video editor, it's a massive hassle to have to create that 1:1 between model types and view model types
Unknown User•11mo ago
Message Not Public
Sign In & Join Server To View
I haven't yet since it would take hours and hours to convert the code, but I kinda want to
Unknown User•11mo ago
Message Not Public
Sign In & Join Server To View
In large project I'm working on it really paid off to just make the models observable too
Otherwise just like you say, there'd be so much unnecessary code
Unknown User•11mo ago
Message Not Public
Sign In & Join Server To View
At the moment I do but who knows...
Unknown User•11mo ago
Message Not Public
Sign In & Join Server To View
I can guarantee almost no one else would want to work on it if I do without view models and have views referencing models and vice versa
Unknown User•11mo ago
Message Not Public
Sign In & Join Server To View