• .net的架构模式


    一:ADO.NET实现三层架构

         不用三层的普通的查询写法:

      

     string sql = string.Format("select * from Studnet where StuName like '%{0}%'", txtName.Text);
                string strconn = "server=.;database=MySc;uid=sa;pwd=123456";
                SqlConnection conn = new SqlConnection(strconn);
                SqlDataAdapter da = new SqlDataAdapter("select * fromStuden", conn);
                DataSet ds = new DataSet();
                try
                {
                    da.Fill(ds);
                   // dataGridView1.DataSource = ds.Tables[0];
                    DataTable dt = ds.Tables[0];
                    txtName.Text = dt.Rows[0]["Name"].ToString();
    
                }
                catch (Exception)
                {
    
                    throw;
                }
    

     普通删除的写法:

      SqlConnection conn = new SqlConnection("server=.;database=MySc;uid=sa;pwd=123456");
                string id = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
                string sql = "delect from Student where stNO=" + id;
                SqlCommand comm = new SqlCommand(sql,conn);
                conn.Open();
    

     修改的普通写法:

       string strsql = @"UPDATE Student SET [StuName]='0' WHERE [StuNO]={3}";
                string id = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
                string sql = string.Format(strsql, txtName.Text, id);
                SqlConnection conn = new SqlConnection("server=.;database=MySc;uid=sa;pwd=123456");
                SqlCommand com = new SqlCommand(sql, conn);
                try
                {
                    conn.Open();
                    com.ExecuteNonQuery();
                    //刷新数据
                }
                catch (Exception)
                {
    
                    throw;
                }
                finally {
                    conn.Close();
                }
    

     一个简单的DbHelper:

     private static SqlConnection conn = new SqlConnection("server=.;database=Mys,uid=sa;pwd=123456");
    
           public static int ExecuteSQL(string strsql)
           {
               SqlCommand comm = new SqlCommand(strsql, conn);
               try
               {
                   conn.Open();
                   return comm.ExecuteNonQuery();
               }
               catch (Exception ex)
               {
    
                   throw ex;
               }
               finally {
                   conn.Close();
               }
           }
    
    
           public static int GetDataTable(string strsql)
           {
               SqlDataAdapter da = new SqlDataAdapter(strsql, conn);
               DataSet ds = new DataSet();
               try
               {
                   da.Fill(ds);
                   return ds.Tables[0];
               }
               catch (Exception ex)
               {
                   
                   throw ex;
               }
                        
           }
    
           /// <summary>
           /// 返回一个单一的值
           /// </summary>
           /// <param name="strsql">传入的是select打头的sql语句(select count(*),select max(..))</param>
           /// <returns>返回的是Object类型</returns>
           public static object GetSingle(string strsql)
           {
               SqlCommand comm = new SqlCommand(strsql, conn);
               try
               {
                   conn.Open;
                   return comm.ExecuteScalar();
               }
               catch (Exception ex)
               {
    
                   throw ex;
               }
               finally {
                   conn.Close();
               }
           }

     缺点:不是面向对象,客户端不了解后台数据也能完全的操作,而这个三层架构实现不了

            解决方案: OOP实现

    二:用OOP实现三层架构

      private static IList<Student> GetStudentsBySQL(string strsql)
           {
               DataTable dt = DbHelper.GetDataTable(sql);
               IList<Student> result = new List<Student>();
               for (int i = 0; i < dt.Rows.Count; i++)
               {
                   Student student = new Student();
                   student.Sid = int.Parse(dt.Rows[i]["id"].ToString());
                   student.Sname = dt.Rows[i]["Name"].ToString();
    
                   result.Add(student);
               }
    
               return result;
           }
           /// <summary>
           /// Id查询
           /// </summary>
           /// <param name="id"></param>
           /// <returns></returns>
           public static Student GetStudentById(int id)
           {
               string sql = string.Format("select * from Studnet where stuno={0}", id);
               try
               {
                   return GetStudentsBySQL(sql)[0];
               }
               catch (Exception)
               {
    
                   return null;
               }
           }
           
           /// <summary>
           /// 查询所有
           /// </summary>
           /// <returns></returns>
           public static IList<Student> GetAllStudent()
           {
               string sql = "select * from Studnet";
               return GetStudentsBySQL(sql);
            
           }
    
           /// <summary>
           /// 姓名查询
           /// </summary>
           /// <param name="name"></param>
           /// <returns></returns>
           public static IList<Student> GetStudentByName(string name)
           {
               string strsql = string.Format("select * from student where stuname like '%{0}%'", name);
               return GetStudentByName(strsql);
           }
    
          

    三:应用抽象工厂+反射实现通用数据源的设计

        一:解决思路:

           1.利用工厂方法模式(多态)来封装 new SqlServerUser()所造成的变化

            说明:工厂模式就是定义一个创建对象的接口,让子类来决定实例化那个类

          2.抽象工厂模式:提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类

         面向对象的好处:可维护性,可复用,可扩展,灵活性

         面向对象的原则:1.类的单一职责

                                2.开放--封闭原则(对扩展是开放的,对于更改是封闭的)

                                3.依赖倒转原则(针对接口编程,不要对实现编程)

                                4.里氏替换原则(子类型必须能够替换它们的父类型)

    四:23种设计模式的分类:

    一:创建型模式

         1.简单工厂模式    2.工厂方法模式    3.抽象工厂模式     4.创建者模式    5.原型模式    6.单例模式  

    二:结构型模式

        1.外观模式    2.适配器模式    3.代理模式    4.装饰模式  5.桥模式    6.组合模式    7.享元模式

    三:行为型模式

        1.模板方法模式      2.观察者模式     3.状态模式     4.策略模式     5.职责链模式      6.访问者模式      7.调停者模式    8.备忘录模式   9.迭代器模式    10.解释器模式

  • 相关阅读:
    Nginx缓存[proxy cache、memcache]
    Nginx重写规则
    同步异步,阻塞非阻塞 和nginx的IO模型
    cookie & session
    HTTP状态码
    web简单的整体测试
    关于 如何用电脑的adb连接Mumu模拟器
    关于社保断交一个月的影响
    关于androidStudio的下载
    可以直接拿来用的android开源项目研究
  • 原文地址:https://www.cnblogs.com/sunliyuan/p/5797564.html
Copyright © 2020-2023  润新知