案例1:项目中可能有多个DAL程序集,每个程序集使用不同的持久化技术或对应不同类型的数据库,但是它们使用同一套接口。
可以在配置文件中配置DAL程序集名称,使用反射加载程序集、创建dal对象。
这样做的好处是使用接口解耦BLL、DAL,如果需要更换其他技术的DAL,只需要修改配置文件中程序集名称即可
1 /// <summary>
2 /// 数据层工厂
3 /// </summary>
4 public class DALFactory
5 {
6 /// <summary>
7 /// 通过反射机制,实例化接口对象
8 /// </summary>
9 private static readonly string _path = System.Configuration.ConfigurationManager.AppSettings["ShopCartMySqlDAL"];
10 private static readonly Assembly _Assembly = Assembly.Load(DALFactory._path);
11
12 /// <summary>
13 /// 通过反射机制,实例化Base_country_culturepart接口对象
14 /// </summary>
15 ///<returns>Base_country_culturepart接口对象</returns>
16 public static IPersonDAL PersonInstance()
17 {
18 return (IPersonDAL)_Assembly.CreateInstance(DALFactory._path + ".PersonDAL");
19 }
20
21 //省略其他DAL对象创建
22 }
public class PersonBLL
{
private static readonly IPersonDAL _dal = DALFactory.PersonInstance();
public static Person Select(IDataReader dr)
{
return PersonBLL._dal.Select(dr);
}
}
//省略其他BLL