我正在使用oracle作爲後端的asp.net項目。 Initialy我使用三層架構開發此應用程序,其中UI爲aspx頁面,BLL和DAL爲類庫項目。我在BLL和DAL中都使用了靜態類和方法。 DAL僅由具有接受由BLL類轉發的查詢的Select,Execute和ExecuteScalar靜態方法的單個類組成。在三層體系結構中正確的功能佈局
DAL
private static string connString = ""
private static OracleConnection conn;
public static OracleConnection OpenConn()
{
if (conn==null)
{
conn = new OracleConnection(connString);
}
if (conn.State != ConnectionState.Open)
{
conn.Open();
}
return conn;
}
public static DataTable Select(string query)
{
DataTable dt = new DataTable();
OracleDataAdapter da = new OracleDataAdapter(query, OpenConn());
da.Fill(dt);
return dt;
}
public static void Execute(string query)
{
OracleCommand cmd = new OracleCommand(query, OpenConn());
cmd.ExecuteNonQuery();
}
public static int ExecuteScaler(string query)
{
OracleCommand cmd = new OracleCommand(query, OpenConn());
int id = Convert.ToInt32(cmd.ExecuteScalar());
return id;
}
這個DAL由BLL類調用的方法是如下 員工BLL
public static DataTable GetEmployees(int facilityid)
{
DataTable dt = new DataTable();
string q = string.Format("SELECT * FROM ..");
dt = OraDAL.Select(q);
return dt;
}
public static DataTable AddEmployee(string name, , int departmentid , int employeeType)
{
DataTable dt = new DataTable();
string q = string.Format("INSERT INTO ...");
dt = OraDAL.Select(q);
return dt;
}
現在我重構應用。與BLL和DAL相同的三層體系結構與類庫項目一樣,具有名爲Domain的其他類庫項目,以包含所有其他項目引用的域類。我使用這些類來進行圖層之間的數據傳輸。這次將DAL類保持爲靜態,並將BLL保留爲正常類。我已將大部分功能(查詢)移至DAL。現在DAL具有類EmployeesDAL,DepartmentsDAL以及包含Select,Execute和ExecuteScalar靜態方法的泛型類。
Employee.cs
public class Employee
{
public int Id { get; set; }
public string Name { get; set; }
public Department department { get; set; }
public EmployeeType employeeType { get; set; }
}
EmployeesDAL
public static List<Employee> GetEmployees(int departmentid)
{
if (departmentid > 0)
{
string query = string.Format("SELECT * FROM EMPLOYEES WHERE department='{0}')", departmentid);
return GetCollection(OraDAL.Select(query));
}
return null;
}
public static Employee GetEmployee(int employeeid)
{
if (employeeid > 0)
{
string query = string.Format("SELECT * FROM PMS_EMPLOYEES WHERE Id='{0}'", employeeid);
return GetSingle(OraDAL.Select(query));
}
throw new Exception("Employee id not valid");
}
public static int AddEmployee(Employee employee)
{
if (employee != null)
{
string query = string.Format("INSERT INTO PMS_EMPLOYEES (name, department, employee_type) VALUES ('{0}','{1}','{2}')", employee.Name, employee.department.Id, employee.employeeType.Id);
return OraDAL.Execute(query);
}
throw new Exception("Values not valid");
}
private static List<Employee> GetCollection(DataTable table)
{
List<Employee> employees = null;
if (table != null)
{
if (table.Rows.Count > 0)
{
employees = new List<Employee>();
foreach (DataRow row in table.Rows)
{
employees.Add(ReadDataRow(row));
}
}
}
return employees;
}
private static Employee GetSingle(DataTable table)
{
if (table != null)
{
if (table.Rows.Count > 0)
{
DataRow row = table.Rows[0];
return ReadDataRow(row);
}
}
return null;
}
private static Employee ReadDataRow(DataRow row)
{
Employee employee = new Employee()
{
Id=int.Parse(row["ID"].ToString()),
Name=row["NAME"].ToString(),
employeeType=EmployeeTypesDAL.GetEmployeeType(int.Parse(row["EMPLOYEE_TYPE"].ToString())),
department=DepartmentsDAL.GetDepartment(int.Parse(row["DEPARTMENT"].ToString()))
};
return employee;
}
EmployeesBLL.cs
public class EmployeesBLL
{
public List<Employee> GetEmployees(int departmentid)
{
if (departmentid > 0)
{
return EmployeesDAL.GetEmployees(departmentid);
}
return null;
}
public int AddEmployee(Employee employee)
{
if (employee != null)
{
return EmployeesDAL.AddEmployee(employee);
}
throw new Exception("Employee cannot be null");
}
的職位是時間越來越長,但我希望你能更好地瞭解這種情況。 我面對新設計的幾個問題,並讓我問這個問題,這是做事的正確方法嗎?由於主要目標是避免在不同層之間來回移動數據表。雖然這種編碼邏輯的新方法太耗時,但值得付出努力。業務層越來越薄,因爲看起來他們提供的唯一服務就是調用DAL的類似方法,我首先需要BLL嗎?如果是,在需要單獨的BLL的情況下可能出現什麼情況。
編輯,我和上面的設計注意
一個問題是數據庫調用的次數。調用次數太多,因爲當填充對象時,所有子對象也都被填充,導致調用數據庫。例如填充Employee對象將導致填充部門實例。在大多數情況下,我只需要部門識別碼而不是整個部門信息。
你的設計是數據驅動設計的一個很好的例子,是的 - 在它的業務邏輯只是一個封裝在crud操作。如果你的應用程序只能做到這一點,那真的很好。順便說一下,如果您打算對BL承擔更多責任,請考慮O/RM方法,否則您會花費太多時間來支持您的數據訪問層代碼庫。 – mikalai