1:添加工厂项目MySchoolDALFactory
2:创建抽象工厂类,代码
public abstract class AbstractDALFactory
{
public static AbstractDALFactory ChooseFactory()
{
string dbType = ConfigurationManager.AppSettings["DBType"].ToString();
AbstractDALFactory factory = null;
switch(dbType)
{
case "Sql":
//factory = new SqlDALFactory();
break;
case "Access":
// factory = new AccessDALFactory();
break;
}
return factory;
}
// public abstract IStudentService CreateStudent();
}
3: 添加DAL接口项目MySchoolIDAL
4: 添加接口,如StudentService的接口为IStudentService,并编写接口内容
5: 在BLL层使用抽象工厂
//调用数据访问层统一数据访问方式
private static AbstractDALFactory factory = AbstractDALFactory.ChooseFactory();
private static IStudentService studentService = factory.CreateStudentService();
其余与之前学过的代码一样
附录:
MySchoolDALFactory:引用MySchoolDAL项目,引用MySchoolIDAL项目
MyschoolIDAL:引用MySchoolModels项目
MySchoolBLL:引用MySchoolDALFactory项目,引用MySchoolIDAL 项目
MySchoolDAL:引用MySchoolIDAL