Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In MVVM, play media file from Model or ViewModel?

Tags:

c#

mvvm

wpf

This question is not related to the MediaElement—pertaining to this, I’ve read both MediaElement.play() from within ViewModel and MVVM pattern violation: MediaElement.Play(), and I agree with both of them.

But say that we’re using instead a third-party assembly such as NAudio. We don’t have to find a way of plugging an existing XAML media control’s interactive features into our ViewModel. Instead, we can simply use, e.g., InteractionTriggers in our View, subscribe to it in our ViewModel, and invoke the play command on a given media file.

Now my question is: is this still something that fits, within the MVVM pattern, into the ViewModel? Or should we rather hand this down to the Model and play the file from there?

like image 400
Informagic Avatar asked Jan 27 '23 08:01

Informagic


1 Answers

I would say it entirely depends on the nature of the media file and its relation to your app's core logic.

Is it just a UI feedback sound effect that gets played when you click on something? Stays in the view.

Is it a music player app that plays music from a playlist? That's core logic and belongs in the VM.

If I'm unsure where something lives, I think about an imaginary "view" on my app that uses a text console rather than WPF. If the logic still exists even if driven by a console, then it lives in the VM.

like image 64
GazTheDestroyer Avatar answered Jan 29 '23 22:01

GazTheDestroyer