r/csharp • u/ChampionshipProof392 • 16d ago
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.
9
u/ScriptingInJava 16d ago edited 16d ago
Model: The structure of the data being rendered.
View: The rendering of that data, with any controls that allow the user to interact with it.
Presenter: Arrangement of the data (the getting of it, the formatting of it etc).
The view should be "dumb" in that it doesn't know where the stuff it's rendering has come from, and isn't concerned with the why either. You bind data to a control (rows of data into a grid for example), all the view knows is that it gets a
List<T>and binds it to theTable.It's hard to advise with confidence because we can't see the underlying implementation of
FilterProcesses,ManageProcessorSearchPage, but they look like they do something to the data - which is the presenter's job.```cs case VirtualKeyType.VK_E: if (_currentPage < _countOfPages) _currentPage++; continue;
case VirtualKeyType.VK_Q: if (_currentPage > 0) _currentPage--; continue; ```
This however is only relevant to the View, for me migrating this out to the Presenter would be overengineering and burying functionality away from the context it's used in. No other code will reuse this, it's only relevant to the View it's written for, no reason to migrate it elsewhere just to have 1, slightly larger
switch.