Lets assume that user works with excel and wants to save/open file. How to send data to presenter? I have two options but I'm not sure which one is better. View raises event and then:
- Presenter has to get path by property. It's easy but I have property that is used only in these two events(view can have more events).
or
- View sends data in own EventArgs class. Now view doesn't have rarely used property but that solution needs new class and view has to create required data itself.
Here is example.
Option 1:
interface IView
{
string FilePath { get; set; }
event EventHandler SaveFile;
event EventHandler OpenFile;
// Other events (EventHandler) and required properties
}
class Presenter
{
private readonly IView _view;
public Presenter(IView view)
{
_view = view;
_view.OpenFile += OpenFile;
_view.SaveFile += SaveFile;
}
private void OpenFile(object sender, EventArgs e)
{
string path = _view.FilePath;
// Open file
}
private void SaveFile(object sender, EventArgs e)
{
string path = _view.FilePath;
// Save file
}
}
Option 2:
interface IView
{
event EventHandler<FileEventArgs> SaveFile;
event EventHandler<FileEventArgs> OpenFile;
// Other events (EventHandler<T>)
}
class FileEventArgs
{
public string FilePath { get; private set; }
public FileEventArgs(string filePath)
{
FilePath = filePath;
}
}
class Presenter
{
private readonly IView _view;
public Presenter(IView view)
{
_view = view;
_view.OpenFile += OpenFile;
_view.SaveFile += SaveFile;
}
private void OpenFile(object sender, FileEventArgs e)
{
string path = e.FilePath;
// Open file
}
private void SaveFile(object sender, FileEventArgs e)
{
string path = e.FilePath;
// Save file
}
}
Aucun commentaire:
Enregistrer un commentaire