应用抽象工厂+反射实现通用数据源的设计(二)

    上篇博文到学生表的一个接口由SqlServerDal和AccessDal层实现具体的方法。

   下面定义一个生产抽象产品工厂的接口:

  五:定义MySchoolIFactory下IStudentFactory的工厂(引用IStudens接口):

 public interface IStudentFactory
    { 
//产生学生接口的抽象工厂接口 IStudent CreateStudent(); }

 六:有两个具体的工厂:SQLServerFactory和AccessFactory(引用MySchoolIStudens和MySchoolIFactory):

    1.SQLServerFactory另外添加SQLServerFactory的引用。

 public class StudentFactory:IStudentFactory
    {
        public IStudent CreateStudent()
        {

            // 创建具体产品的工厂 (生产的是sqlserver产品的工厂)
           // StudentServer studentService = new StudentServer();
           // return studentService;

            return new MyScholSQLServerDal.StudentServer();
        }
    }

      2.AccessFactory添加AccessDal的引用

  public class StudentFactory:IStudentFactory
    {

        public IStudent CreateStudent()
        {
            //创建具体的Access产品
            return new MySchoolAccessDal.StudentServer();
        }
    }

 七:简单工厂对抽象工厂进行改进:DataAccess 目的:不让前台进行判断,通过抽象工厂结合配置文件决定产生哪个具体的工厂,具体工厂产生具体的产品(引入连两个接口:MySchoolIStudens和MySchoolIFactory

   首先在App.Config中添加:

<add key="db" value="SQLServer" />

 

/// <summary>
    /// 产生一个具体的工厂  返回一个接口(抽象工厂)
    /// </summary>
    public class DataAccessFactory
    {
        //读取数据库的配置
        private static readonly string db = ConfigurationManager.AppSettings["db"];
        //定义程序集
        private static readonly string assName = db + "Factory";
        //定义命名空间
        private static readonly string nameSpace = db + "Factory";
       /// <summary>
       ///通过反射动态决定加载那个程序集
       /// </summary>
       /// <returns></returns>
        public static IStudentFactory CreateStudentFactory()
        { 
            //反射具体的类  加载程序集
            Assembly ass = Assembly.Load(assName);
            //创建一个类的实例
            IStudentFactory factory = ass.CreateInstance(nameSpace + "." + "StudentFactory") as IStudentFactory;
            return factory;
        }
    }

 八:业务逻辑层:MySchoolBLL  添加的引用:(MySchoolModels和MySchoolIFactory和DataAccess和MySchoolIStudens;

 public  class StudentManager
    {
      //通过配置文件产生抽象工厂
      private IStudentFactory studentFactory=DataAccessFactory.CreateStudentFactory();
      //具体的产品
      private IStudent studentMgr=null;
      //构造函数
      public StudentManager()
      {
         studentMgr=studentFactory.CreateStudent();
      }
            /// <summary>
           /// 得到所有的学生信息
           /// </summary>
           /// <returns></returns>
            public IList<Student> GetAllStudents()
            {
             return studentMgr.GetAllStudents();
            }

           /// <summary>
           /// 根据主建ID查询学生
           /// </summary>
           /// <param name="stuno"></param>
           /// <returns></returns>
            public Student GetStudent(int stuno)
            {
               return studentMgr.GetStudent(stuno);
            }

           /// <summary>
           /// 根据学生的姓名进行查询
           /// </summary>
           /// <param name="stuname"></param>
           /// <returns></returns>
            public IList<Student> GetStudentByName(string stuname)
            {
               return  studentMgr.GetStudentByName(stuname);
            }

           /// <summary>
           /// 添加一个学生
           /// </summary>
           /// <param name="student"></param>
           /// <returns></returns>
            public bool AddStudnet(Student student)
            {
              return studentMgr.AddStudnet(student)>0;
            }

        
           /// <summary>
           /// 删除一个学生
           /// </summary>
           /// <param name="stuno"></param>
           /// <returns></returns>
            public bool DelStudetnt(int stuno)
            {
                return studentMgr.DelStudetnt(stuno) > 0;
            }
    }

 

posted @ 2016-08-26 00:55  石shi  阅读(274)  评论(0编辑  收藏  举报