现在我正在研究一个用 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; }
}
District Entity 类,为什么它的扩展名是 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 块
- 这个实现有什么好的设计