zoukankan      html  css  js  c++  java
  • 不想当将军的学生,不是好程序员——数据访问层DAL

    源代码:13033480群共享

    郭德纲说的原话是:“不想当裁缝的厨子不是好司机”。

    他说的是这个意思吗?

    又能天天在家做可口的饭菜(你把厨子理解成给别人做饭,那,我就没办法了);

    又买得起得体漂亮的好衣服(你把裁缝理解成给别人量体裁衣,那,我就没办法了);

    又开得起宝马奔驰兜风的人(你把司机理解成给别人卖苦力开破车的人,那,我就没办法了);

    才能讨得美女的欢心。

    呵呵,仁者见仁,我,也就这智商了…

    我说的这句“不想当将军的学生,不是好程序员”,可没那么绕人:

    将军嘛:要有系统的观点,要指挥别人做事,不需要事事躬亲,这是一种思想;

    学生嘛:学生的最高境界是什么?是“学士”啦…

    “学士”和“战士”没什么大的区别啦,一个打仗的专业人士,一个学习的专业人士而已…

    程序员嘛:计算机专业的学生,一个有系统观点的学士,难道不是一个好的程序员吗??

    前面,我们已经当好了一个裁缝,量体裁衣,精心打造了一个数据集Model;我们又把ADO.NET的核心类封装到了SQLHelper中,构建了一个函数集DBUtilty。

    那,现在,是时候,我们组建一个职能部门,把那些基础的脏活、累活都交给他们去做了吧?

    对了,这个职能部门,就叫做数据访问层DAL…

    【操作步骤】

    一、添加类库DAL,在DAL中添加类Category.cs,并在类库中添加函数GetCategories(),代码如下:

    using System;
    using System.Data;
    using System.Collections.Generic;
    using System.Text;
    
    using System.Data.SqlClient;
    using WestGarden.Model;
    using WestGarden.DBUtility;
    
    namespace WestGarden.DAL
    {
        public class Category
        {
            private const string SQL_SELECT_CATEGORIES = "SELECT CategoryId, Name, Descn FROM Category";
            public IList<CategoryInfo> GetCategories()
            {
                IList<CategoryInfo> categories = new List<CategoryInfo>();
    
                using (SqlDataReader rdr = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, SQL_SELECT_CATEGORIES, null))
                {
                    while (rdr.Read())
                    {
                        CategoryInfo cat = new CategoryInfo(rdr.GetString(0), rdr.GetString(1), rdr.GetString(2));
                        categories.Add(cat);
                    }
                }
                return categories;
            }
        }
    }
    


    函数GetCategories()要在SQLHelper中读取连接字符串,因此,需要在SQLHelper.cs中添加代码:

    public static readonly string ConnectionStringLocalTransaction = ConfigurationManager.ConnectionStrings["NetShopConnString"].ConnectionString;
    


     

    注意添加引用System.Configuration。

    二、使用用户控件

    在Web中新建文件夹Controls并在其中添加用户控件NavigationControl.ascx,窗体页和代码页代码分别如下:

    1、NavigationControl.ascx

    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="NavigationControl.ascx.cs" Inherits="WestGarden.Web.NavigationControl" %>
    <%@ OutputCache Duration="100000" VaryByParam="*" %>
    
    <asp:Repeater ID="repCategories" runat="server">
    <HeaderTemplate>
    <table cellspacing="0" border="0" style="border-collapse: collapse;">
    </HeaderTemplate>
    <ItemTemplate>
    <tr>
    <td class="<%= ControlStyle %>"><asp:HyperLink runat="server" ID="lnkCategory"  NavigateUrl='<%# string.Format("~/Items.aspx?page=0&categoryId={0}", Eval("CategoryId")) %>' Text='<%# Eval("Name") %>' /><asp:HiddenField runat="server" ID="hidCategoryId" Value='<%# Eval("CategoryId") %>' /></td>
    </tr>
    </ItemTemplate>
    <FooterTemplate>
    </table>
    </FooterTemplate>
    </asp:Repeater>
    


    2、NavigationControl.ascx.cs

    using System;
    using System.Web.UI.WebControls;
    using WestGarden.DAL;
    
    namespace WestGarden.Web {
        public partial class NavigationControl : System.Web.UI.UserControl {
                   
            private string controlStyle;
    
            protected string ControlStyle {
                get { return controlStyle; }
            }
    	
            protected void GetControlStyle() {
                if (Request.ServerVariables["SCRIPT_NAME"].ToLower().IndexOf("default.aspx") > 0)
                    controlStyle = "navigationLinks";
                else
                    controlStyle = "mainNavigation";
            }
    
            protected void Page_Load(object sender, EventArgs e) {
                GetControlStyle();
                BindCategories();
    
                string categoryId = Request.QueryString["categoryId"];
                if (!string.IsNullOrEmpty(categoryId))
                    SelectCategory(categoryId);
            }
    
            private void SelectCategory(string categoryId) {
                foreach (RepeaterItem item in repCategories.Items) {
                    HiddenField hidCategoryId = (HiddenField)item.FindControl("hidCategoryId");
                    if(hidCategoryId.Value.ToLower() == categoryId.ToLower()) {
                        HyperLink lnkCategory = (HyperLink)item.FindControl("lnkCategory");
                        lnkCategory.ForeColor = System.Drawing.Color.FromArgb(199, 116, 3);
                        break;
                    }
                }
            }
    
            private void BindCategories() {
                Category category = new Category();
                repCategories.DataSource = category.GetCategories();
                repCategories.DataBind();            
            }
        }
    }
    


     

    三、为Default.aspx添加主题WestGarden并添加样式表StyleSheet.css,在Default.aspx中应用主题,删除Default.aspx.cs代码页中的代码及Default.aspx中Reapeter控件,直接把用户控件拖入到页面中。

    【几点说明】

    1、  用户控件NavigationControl,由于要应用在首页和母版页两个地方,在这两个地方的样式是不一样的,为此,有了函数GetControlStyle(),通过判断地址中有没有default.aspx来确定采用什么样式。

    2、  母版页中的导般栏,选择点击分类项后,相应分类项的颜色会改变。为此,有了函数SelectCategory(string categoryId),通过地址栏中获取的CategoryId,查询预先放置在HiddenField中的CategoryId,获取相应的Repeater控件的分类项,并设置相应HyperLink的颜色。

    3、  我们把ADO.NET的核心类封装在SQLHelper.cs中,只要告诉它针对哪个连接,命令类型是什么、命令内容是什么,命令参数是什么,就可以使用其中的通用数据库访问函数,完成基本的查询、更新、插入、删除操作。

    4、  我们把基本的查询、更新、插入、删除操作的函数,封装在数据访问层DAL中,也就是把访问数据库的基本参数:针对哪个连接、命令类型、命令内容、命令参数,都存放在数据访问层的相应函数中,这样,上一层,在我们现在这个两层的结构中的应用层,就不再做那些具体的连接、执行命令操作;不再做那些具体的配置连接字符串、命令内容的操作,只需要简单发布一个指令,调用一个GetCategories()函数,就一切都OK了,我们,是不是已经由奴隶到将军了呢????

    5、关于层次架构的问题,我转载的一篇文章讲得非常好,举的例子也特别地好,大家有时间,可以去看看:http://blog.csdn.net/yousuosi/article/details/7658046

    另:前面,我们十八相送,把ADO.NET核心类送到了类库DBUtility中的SQLHelp.cs类中,这次,就一次性把查询商品分类表Name字段的功能段代码,直接形成GetCategories(),直接放在了数据访问层,没有再次来个十八相送,实在只是为了不让程序员中的有情人,再次伤心过度之原因,特此说明...

    版权所有©2012,西园电脑工作室.欢迎转载,转载请注明出处.更多文章请参阅博客http://blog.csdn.com/yousuosi

  • 相关阅读:
    MyBatis中传入参数parameterType类型详解
    mybatis逆向工程无法生成带有_表明的解决办法。
    sql server 正则匹配查询数据
    Kibana按时间(小时,分钟,天)统计数据 ,
    AutoCAD 多重引线 文本 左右对齐的问题
    ObjectARXWizard2022安装
    综合练习:词频统计
    powerDesigner设计表自动生成sql语句,设置携带注释
    打jar包后,无法获取resources目录下的word模板文件
    solr新建core,并配置schema
  • 原文地址:https://www.cnblogs.com/WestGarden/p/3138345.html
Copyright © 2011-2022 走看看