When I want to check the validity of an attendance being entered into the system, I perform following action.
AttendancePresenter Class
void _View_OnCheckValidity(object sender, EventArgs e)
{
//ExecuteAction performs exception handling in Base Class
this.ExecutAction(() =>
{
ValidateAttendance();
});
}
private void ValidateAttendance()
{
//DataService method returns true if the attendance is valid.
var validity = _DataService.CheckValidityOfAnAttendance(_View.EmployeeID, _View.Date, _View.ShiftType);
//Set the validity of the attendance in a View property.
//So that View can stop execution if validity is false.
_View.AttendanceValidity = validity;
//If validation fails, throw an exception
if (!validity)
{ throw new Exception("Invalid Attendance. Already there is a matching attendance for this employee"); }
}
View
private void txtOutTime_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Enter & !string .IsNullOrWhiteSpace(txtEmployeeID.Text) & txtOutTime.Text != DefaultText)
{
OnCheckValidity(sender, e);
if (this.AttendanceValidity)
{ OnEnterAttendance(sender, e); } //This line of code enters attendance into the DB
}
}
Appreciate if you could review this code and give your feedback. Specially what I want to know is about the exception thrown in presenter if the validation fails. Is it acceptable? Is this is a standard use of throw
?