三层开发:BLL DAL UI
BLL:业务逻辑层 IBLL(业务逻辑的接口) 此类中调用DAL方法
DAL:数据访问层 IDAL(数据访问的接口) 此类中调用linq语句访问数据
UI:界面层
BLL中,每个方法都要单独实例化数据访问类,保证数据的及时更新;
DAL中,对象不能直接替换,要对每个属性逐一更改;
--------------------
抽象工厂:(反射) using System.Reflection; --为了更好的实现独立,降低耦合度
在IDAL中建立DAL_factory类中:
public static IDAL_people createpeo() { IDAL_people p = Assembly.Load("SQL_DAL").CreateInstance("SQL_DAL.DAL_people") as IDAL_people; return p; }
BLL中实例化类:
IDAL_people peoDAL = DAL_Factory.createpeo();
IDAL_nation naDAL = DAL_Factory.createna();
---------------------
ConfigurationManager --config配置文件,需要引用 using System.Configuration;
Web的config配置文件中:
<appSettings> <add key="load" value="SQL_DAL" /> <add key="peo" value="DAL_people" /> <add key="na" value="DAL_nation" /> </appSettings>
DAL_factory类中:
public static IDAL_people createpeo() { IDAL_people p = null; string load = ConfigurationManager.AppSettings["load"]; string peo = ConfigurationManager.AppSettings["peo"]; p = Assembly.Load(load).CreateInstance(load + "." + peo) as IDAL_people; return p; }
更改时只需更改 键值对;
----------------------
lambda表达式(r=>r.) using System.Linq.Expressions;
Expression<Func<people, bool>> exp
数据访问DAL中的方法:
public IQueryable<people> se(Expression<Func<people, bool>> exp) { return con.people.Where(exp).AsQueryable(); }
业务逻辑BLL中的方法:
public IQueryable<people> se(string s) { IDAL_people peoDAL = DAL_Factory.createpeo(); Expression<Func<people, bool>> exp = r => r.nickname.Contains(s); return peoDAL.se(exp); }