I'm a beginner to MVP pattern and just want to know the best practices with regard to the following case.
For better understanding I'll ask the question by an example. Lets say we have a form EmployeeView
, EmployeePresenter
, EmployeeModel
and a DataService
class which encapsulates GetEmployeeByID()
method. For this demonstration I use concrete classes.
Lets say now in a win forms app we want to search employees by ID
, so we enter ID
in the view and press Search button. In this event the Presenter
will update the EmployeeModel
probably using reflection. (at this moment only 'EmployeeModel.ID
' property has data). Then the Presenter
will talk to DataService
. This can be done in two ways
- Here we pass the
Model
toDataService
and it will then update the same model and return back to thePresenter
.
class EmployeePresenter {
private void SearchEmployee (Object sender, EventArgs e)
{
SearchEmployee();
}
private void SearchEmployee()
{
var EmployeeModel = DataService.GetEmployeeByID(EmployeeMode);
base.SetViewPropertiesFromModel(EmployeeModel);
}
}
class DataService {
public EmployeeModel GetEmployeeByID(EmployeeModel employee)
{
//Database code here
employee.Name= (string) dataReader["name"];
.
.
.
return employee;
}
}
- Here we pass only a property value of the
Model
and then theDataService
will create aModel
and return to thePresenter
.
class EmployeePresenter {
private void SearchEmployee (Object sender, EventArgs e)
{
SearchEmployee();
}
private void SearchEmployee()
{
var EmployeeModel = DataService.GetEmployeeByID(EmployeeMode.ID);
Base.SetViewPropertiesFromModel(EmployeeModel);
}
}
class DataService {
public EmployeeModel GetEmployeeByID (string employeeID)
{
//Database code here
return new BankAccount
{
EmployeeName = (string) dataReader["name"],
.
.
. };
}
}
- Which one of the above is acceptable?
- For example if details of two entities (say, employee and salary entities) are shown on a single view, do we consider these entities as two models or as a single model like
EmployeeSalary
? If its two models do we need two presenters? - Should
DataService
always return a business model? cantDataService
returnstings
orDataSets
for ex? - Can presenter set values on the view like
_view.EmployeeName=EmployeeModel.Name;
?