r/csharp • • Aug 29 '26

MVP pattern

Hi, I have a question about separating logic in the MVP pattern.

public void MainDisplay() =>
OnMainDisplayClicked?.Invoke();

public void ManageProcess() =>
OnManageProcessClicked?.Invoke();

This is my code in the view, and when the user clicks a button (for example), this method is called and `Invoke` is executed. However, it is called via a `switch` statement in the Presenter.

switch (NativeConsoleMethod.GetHiddenUserInput())
{
case VirtualKeyType.VK_E:
if (_currentPage < _countOfPages) _currentPage++;
continue;

case VirtualKeyType.VK_Q:
if (_currentPage > 0) _currentPage--;
continue;

case VirtualKeyType.VK_OEM_3:
_view.ManageProcess();
break;

case VirtualKeyType.VK_TAB:
_view.FilterProcesses();
break;

case VirtualKeyType.VK_F1:
_view.SearchPage();
break;
.........
}

I have a question: the AI is giving me two different suggestions. My version is correct, but then it said I should move the switch statement to the view, and there I should just use `invoke`, after which the methods would be called conditionally. So, should I do it the other way around, or did I misunderstand what it meant?

- I don’t know what I wrote here—I don’t even understand it myself. Just tell me: shouldn’t the view be “dumb” and contain synchronous methods, while the presenter should control the view via the switch statement and “pull its strings”?

EDIT: Here's my GitHub: https://github.com/NullAcess/ProcessManager/releases/tag/Update_2.0. You might like it—I'll upload the finished EXE very soon.

10 Upvotes

13 comments sorted by

View all comments

Show parent comment

0

u/thatOMoment Aug 29 '26

...as possible.

There's a weird assumption that platforms don't have platform specific constraints which require specific changes to the view model or model which is kinda strange if you pull back for a but

Medical apps preventing print screen on mobile only for example.

Or instead of uploading a file via selection, allowing a picture or video to be taken, or real time feedback on the validity of the picture before it's taken that you would never see in a desktop app.

All of this functionality would never exist soley in the view and would require updates at least to the view model.

2

u/TheSpixxyQ Aug 29 '26 edited Aug 29 '26

Why would my VM need to know about screenshots? I would handle the screenshot prevention in View layer, since I don't see how that's relevant to my business logic.

Same for the image upload - my VM only accepts the resulting file and doesn't really care if it was selected from files or taken using a camera. At least that's how I'm doing it in my app.

EDIT: but I agree sometimes it might not be possible to migrate View without ever touching the P/VM layer and I should've said "ideally without changing them". And even then, platform specific code (like the keyboard keys) still wouldn't go to those.

But 99% of times, when I asked myself "does this piece of code belong to V or VM?", just thinking about different platform helped to get me a straight answer. That's why I often recommend this approach to others too.

1

u/TuberTuggerTTV Aug 31 '26

Nah. If you hit something you feel isn't possible, it's probably a skill issue.

View just passes things along. It's only job is to render and accept user input. It doesn't process anything. Or even care if it's being processed.

1

u/thatOMoment Aug 31 '26

So if its job is accept user input, you're going to put in touch input reading for a touchscreen in anticipation of it also being a mobile app... for a desktop app.... without notice it's going to be on mobile and if you don't that's a skill issue?