我正在使用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