現在我正在開發一個VB6開發的非常大的銀行解決方案。該應用程序是大規模的基於表單的,缺乏分層結構(數據訪問,業務邏輯和表單操作的所有代碼都在單一表單類中)。我的工作是現在重構這段代碼。我正在用C#編寫適當的業務邏輯層和數據訪問層,並且表單將保留在VB中。層架構的最佳實踐是什麼?
以下是代碼片段:
public class DistrictDAO
{
public string Id{get;set;}
public string DistrictName { get; set; }
public string CountryId { get; set; }
public DateTime SetDate { get; set; }
public string UserName { get; set; }
public char StatusFlag { get; set; }
}
區實體類,爲什麼它的擴展名是DAO,我不是不清楚。
public class DistrictGateway
{
#region private variable
private DatabaseManager _databaseManager;
#endregion
#region Constructor
public DistrictGateway(DatabaseManager databaseManager) {
_databaseManager = databaseManager;
}
#endregion
#region private methods
private void SetDistrictToList(List<DistrictDAO> dataTable, int index, DistrictDAO district){
// here is some code for inserting
}
#endregion
#region public methods
try
{
/*
query and rest of the codes
*/
}
catch (SqlException sqlException)
{
Console.WriteLine(sqlException.Message);
throw;
}
catch (FormatException formateException)
{
Console.WriteLine(formateException.Message);
throw;
}
finally {
_databaseManager.ConnectToDatabase();
}
public void InsertDistrict() {
// all query to insert object
}
public void UpdateDistrict() {
}
#endregion
}
DistrictGateway類負責數據庫查詢處理 現在是業務層。
public class District
{
public string Id { get; set; }
public string DistrictName { get; set; }
public string CountryId { get; set; }
}
public class DistrictManager
{
#region private variable
private DatabaseManager _databaseManager;
private DistrictGateway _districtGateway;
#endregion
#region Constructor
public DistrictManager() {
// Instantiate the private variable using utitlity classes
}
#endregion
#region private method
private District TransformDistrictBLLToDL(DistrictDAO districtDAO) {
// return converted district with lots of coding here
}
private DistrictDAO TransformDistrictDLToBLL(District district)
{
// return converted DistrictDAO with lots of coding here
}
private List<District> TransformDistrictBLLToDL(List<DistrictDAO> districtDAOList)
{
// return converted district with lots of coding here
}
private List<DistrictDAO> TransformDistrictDLToBLL(List<District> district)
{
// return converted DistrictDAO with lots of coding here
}
#endregion
#region public methods
public List<District> GetDistrict() {
try
{
_databaseManager.ConnectToDatabase();
return TransformDistrictBLLToDL( _districtGateway.GetDistrict());
}
catch (SqlException sqlException)
{
Console.WriteLine(sqlException.Message);
throw;
}
catch (FormatException formateException)
{
Console.WriteLine(formateException.Message);
throw;
}
finally {
_databaseManager.ConnectToDatabase();
}
}
#endregion
這是業務層的代碼。
我的問題是:
- 它是一個完美的設計?
- 如果不是,這裏有什麼缺陷?
- 我認爲,這個代碼複製try catch塊
- 什麼可以很好的設計,這種實現
可能在這裏有更多的運氣:http://codereview.stackexchange.com/ – Brook