2012-02-11 12 views
0

異なるレイヤーでエラーが発生した場合の例外処理のベストプラクティスは何ですか?ベストプラクティス - サービス/ DAO /ビジネス層での例外処理

私は、DAO、SERVICE、BUSINESS、PRESENTATIONの4層のコードを持っています。私はDAOのレイヤーで実行時例外をキャッチして、プレゼンテーションレイヤーにメッセージを表示したいと思っています。以下のアプローチは良いアプローチですか?

ここでコードスニペットで - DataExceptionは私の実行時例外クラスです.ServiceクラスとBusiness例外クラスは、私のチェックされた例外実装クラスです。 DAO層で

、データベース

class dao{ 
public User getUser() throws DataException{ 

User user = null; 

try 
{ 
//some operation to fetch data using hibernatetemplate 
}catch(Exception ex){ 
throw new DataException(ex.getMessage()); 
} 

return user; 
} 
} 

service.java

class service{ 
public User getUser(String username) throws ServiceException{ 

User user = null; 

try 
{ 
//some operation to fetch data using dao method 
dao.getuser(username); 
}catch(DataException ex){ 
throw new ServiceException(ex.getMessage()); 
} 

return user; 
} 
} 

business.java

class business{ 
public User getUser(String username) throws BusinessException{ 

User user = null; 

try 
{ 
//some operation to fetch data using dao method 
service.getuser(username); 
}catch(ServiceException ex){ 
throw new BusinessException(ex.getMessage()); 
} 

return user; 
} 
} 
からいくつかの値のための方法をチェック:以下

コードスニペット

事前sentation層、あなたがしてPresentationExceptionでビジネス例外をカプセル化する必要があり

答えて

2

...それはプレゼンテーション層メッセージはフロントエンドJSPページでユーザーにスローされるから

class Presentation{ 
public User getUser(String username) throws BusinessException{ 

    User user = null; 


//some operation to fetch data using business method 
business.getUser(username); 

    return user; 
} 
} 

が想定コントローラクラスも聞かせてコード。このコードは、ローカライズされた方法でエラーメッセージを表示するために使用されます。このコードでは、エラーメッセージが完全にプレゼンテーション内にあり、異なるビューに対して異なるメッセージが表示されます。

try{ 
    getUser(...); 
}catch(BusinessException b){ 
    throw new PresentationException(ErrorEnum.GetUserError,b); 
} 

この実行は、何らかの形でモデル(ビューコンテキスト)に置く必要があります。

if(exception){ 
if(debug) print(exception.getCause().getMessage()); 
else print(localize(exception.getErrorCode()); 
} 
:あなたのような何かを行うことができますJSPで

関連する問題