日期:2014-05-17  浏览次数:20349 次

asp.net 三层架构疑问
各位大侠,小弟最近才接触三层架构,遇到个疑问。

BLL:

 public D_BL.BLCollection GetAllBL(M_BL1 bl1)
  {
  return new D_BL().GetAllBL(bl1);
  }

DAL:

public BLCollection GetAllBL(M_BL1 bl1)
  {
  BLCollection bls = new BLCollection();
  string sql = "Select t.pnl_id,t.prod_nbr,t.cust_nbr,t.bl_log_mach,t.work_ctr,t.bl_factory,t.reason,t.position,t.attribute,t.user_id,to_char(t.key_date,'yyyy-MM-dd hh24:mi:ss') as key_date From bl_bad t";

  sql += " Where fac_id='"+bl1.fac_id+"'";
  if (bl1.date1 != "")
  {
  sql += " and to_char(key_date,'yyyy-MM-dd')>='" + bl1.date1 + "'";
  }
  if (bl1.date2 != "")
  {
  sql += " and to_char(key_date,'yyyy-MM-dd')<='" + bl1.date2 + "'";
  }
  if (bl1.cust_nbr != "")
  {
  sql += " and cust_nbr='" + bl1.cust_nbr + "'";
  }
   
   
  OracleDataReader dr = null;
  try
  {
  dr = Get_DataReader(sql);
  while (dr.Read())
  {
  bls.Add(PopuplateBL(dr));
  }
  }
  catch (OracleException ex)
  {
  throw new Exception(ex.Message);
  }
  finally
  {
  if (dr != null && !dr.IsClosed)
  {
  dr.Close();
  }
  conn.Close();
  }
  return bls;
  }

我在写SQL语句的时候,把这些判断逻辑写到了DAL,如果想写进BLL层怎么写呢。
  if (bl1.date1 != "")
  {
  sql += " and to_char(key_date,'yyyy-MM-dd')>='" + bl1.date1 + "'";
  }
  if (bl1.date2 != "")
  {
  sql += " and to_char(key_date,'yyyy-MM-dd')<='" + bl1.date2 + "'";
  }
  if (bl1.cust_nbr != "")
  {
  sql += " and cust_nbr='" + bl1.cust_nbr + "'";
  }


------解决方案--------------------
sql语句不能往BLL里写,你只能在DAL中写好,在BLL中调用!
------解决方案--------------------
探讨

sql语句不能往BLL里写,你只能在DAL中写好,在BLL中调用!

------解决方案--------------------
三层使得项目更加的模块化,更加的低耦合

你可以想一下,如果表结构发生了变化,那么我们只需要修改数据访问层中的查询语句就可以了

如果出现在了业务逻辑层,那么你要修改的就不仅仅是访问层了,不利于扩展
------解决方案--------------------
三层只是个代名词,你可以写成5层,7层。但重要的是业务逻辑层,UI层只负责与界面交互就行了,而把所有逻辑交给BLL,DAL只负责与数据库打交道,你这明显的是两头粗,中间细,实际上应该是中间粗,两头细。
------解决方案--------------------
BLL:

 public D_BL.BLCollection GetAllBL(M_BL1 bl1)
{
string sqlWhere="
sqlWhere Where fac_id='"+bl1.fac_id+"'";
if (bl1.date1 != "")
{
sqlWhere += " and to_char(key_date,'yyyy-MM-dd')>='" + bl1.date1 + "'";