In my little WinForm app, intended to practice as a hobbyist, I'm using this code to pass data from the View to its Presenter:
public event Action<object, string, string?>? SearchInAlbums;
private void Btn_SearchInAlbums_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(cmbSearchContext.SelectedValue.ToString()))
SearchInAlbums?.Invoke(this, Txt_Search.Text, cmbSearchContext.SelectedValue.ToString());
}
Then in the listener of this event, the Presenter, I can write code like this:
public void OnSearchInAlbums(object? sender, string searchPhrase, string? tableName)
{
if (!string.IsNullOrEmpty(tableName))
{
_albumList = _albumRepository.GetAlbumsByValue(searchPhrase, tableName);
SetGridViewBindingSource();
}
else
{
_mainView.Message =
"No records were affected: the table name was NULL or EMPTY (no table name)";
}
}
This way this consumer (Presenter), doesn't have to ask for data - instead it gets it and then tells the Repository what to do with it. This way, in my mind, I have enforced the "Tell Don't Ask" principle.
But, I see nobody using code like:
public event Action<object, string, string?>? SearchInAlbums;
Instead, they all use:
public event EventHandler SearchInAlbums; //or something similar
By doing the latter, the presenter is forced to ask for data from the view and therefore the TDA principle has been broken. I'm a hobby-learner, so I don't know. What is the take by more experienced programmers? Am I actually doing things right here? Or am I using concepts wrong?
Teachers on YouTube always ask for data from the View of the Presenter, totally ignoring the TDA principle. Maybe that's normal in this case. But I wouldn't know why as it strikes me as unnecessary.