Friday, January 22, 2010

Coining user messages in a failure scenario

When an operation (such as save) is triggered from an UI application, the call trickles down to underling APIs. If something wrong happens with the operation, then the user needs to be notified of the failure on the UI. The question: how and where should this UI message be coined?

 

Consider the following (over simplified) code to achieve a save operation

 

// 1. UI code calls setNewStudent(Student) with the student data entered in UI form (xhtml, JSP, etc…)

 

// 2. An action on the UI (button click for example) invoked the following method

StudentUIController.saveNewStudent()

{

    StudentDAO.SaveOfUpdate(getNewStudent()); // 3. UI Code makes DAO layer call to persist object

}

 

StudentDAO.saveOrUpdate( Student )  // 3.

{

    // Persist the student

}

 

One of the best practices to handle the failure scenario is to have the DAO layer (StudentDAO in this example) throw a custom exception. The UI layer (StudentUIController) must catch this exception and coin a message for the user. The custom exception should have enough information in it for the UI layer to coin a meaningful message for the user from the exception. In some cases, the DAO layer may also give a brief message of what went wrong in the exception.

 

Will update this post soon with the code after I have a working example of how this is to work.

 

No comments: