打造屬於自己的博客 - Asp.net三層架構

   大家好,暑假過大半,不知道現在的你在做些什麼,也許在苦力,也許在加班,也許在打dota,也許...在寫代碼。體會着屬於自己的生活和理想,而我也在忙碌中迅速的往社會的方向邁進,馬上畢業了,壓力山大啊,找工作的話還是堅持自己心中的理想,不達目標絕不妥協!雖然有些偏激但更爲自己的態度所決定,我就是這麼一個人。

   可能有想要表白的意願吧,所以就在暑假工作期間萌發了寫網站的想法,技術有限的我,在工作期間大量的學習和問項目總監(挺不錯的一個牛x的人),花了大概一個星期就艱難的完成了博客,其實是爲她而做,希望在開學的時候告訴她自己的內心的想法,並且期望自己想要的結果,所以很是努力。天在看.....

   喜歡開源的東西,所以這次也就準備試一試,在前幾期的博文中已經介紹了幾個博客建站的必備功能的代碼學習過程。這裏就不多陳述了,接下來就直接進入三層的編寫,表現層,業務邏輯層,數據層大家應該很熟悉了吧,如果不清楚這個過程的,百度一下,這裏就不做陳述了。

下面是三層的代碼和邏輯引用。舉一例,用戶信息的三層代碼:


Model:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Model
{
    public class L_User
    {
        #region  實例化用戶&管理員的主要字段
        public int UID { get; set; }
        public string Author { get; set; }
        public string UUserPwd { get; set; }
        public string UEmail { get; set; }
        public DateTime UCreateTime { get; set; }
        public string UUserRole { get; set; }
        #endregion
    }
}


DAL:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model;
using System.Data.SqlClient;
using System.Data;
namespace DAL
{
    public class UserDAL
    {
        //添加用戶
        public int AddUser(L_User user)
        {
            try
            {
                int i = DBHelper.executeSql("insert into L_User(Author,UUserPwd,UEmail,UCreateTime,UUserRole) values(@Author,@UUserPwd,@UEmail,@UCreateTime,@UUserRole)",
                    new SqlParameter("@Author", user.Author), new SqlParameter("@UUserPwd", user.UUserPwd),
                    new SqlParameter("@UEmail", user.UEmail), new SqlParameter("@UCreateTime", user.UCreateTime),
                    new SqlParameter("@UUserRole", user.UUserRole));
                return i;
            }
            catch (Exception ee)
            {
                throw new Exception(ee.Message);
            }
        }
        //修改user
        public int UpdateUser(int UID, string Author, string UUserPwd, string UEmail, DateTime UCreateTime, string UUserRole)
        {
            int i = DBHelper.executeSql("update L_User set Author=@Author,UUserPwd=@UUserPwd,UEmail=@UEmail, UCreateTime=@UCreateTime,UUserRole=@UUserRole where UID=@UID",
                new SqlParameter("@UID", UID),
                new SqlParameter("@Author", Author),
                new SqlParameter("@UUserPwd", UUserPwd),
                new SqlParameter("@UEmail", UEmail),
                new SqlParameter("@UCreateTime", UCreateTime),
                new SqlParameter("@UUserRole", UUserRole));
            return i;
        }
        //刪除user
        public int DeleteUser(int UID)
        {
            int i = DBHelper.executeSql("delete from L_User where UID=@UID ", new SqlParameter("@UID", UID));
            return i;
        }
        //單獨的一條user信息
        public L_User get_singleUser(int UID)
        {
            DataSet ds = DBHelper.dataset("select * from L_User where UID=@UID ", new SqlParameter("@UID", UID));
            L_User lu = new L_User();
            if (ds != null)
            {
                lu.UID = Convert.ToInt32(ds.Tables[0].Rows[0]["UID"].ToString());
                lu.Author = ds.Tables[0].Rows[0]["Author"].ToString();
                lu.UUserPwd = ds.Tables[0].Rows[0]["UUserPwd"].ToString();
                lu.UEmail = ds.Tables[0].Rows[0]["UEmail"].ToString();
                lu.UCreateTime = Convert.ToDateTime(ds.Tables[0].Rows[0]["UCreateTime"].ToString());
                lu.UUserRole = ds.Tables[0].Rows[0]["UUserRole"].ToString();
                return lu;
            }
            else
            {
                return null;
            }
        }
        //單獨的一條user信息2
        public L_User get_singleUser(string name)
        {
            DataSet ds = DBHelper.dataset("select * from L_User where Author=@Author ", new SqlParameter("@Author", name));
            L_User lu = new L_User();
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                lu.UID = Convert.ToInt32(ds.Tables[0].Rows[0]["UID"].ToString());
                lu.Author = ds.Tables[0].Rows[0]["Author"].ToString();
                lu.UUserPwd = ds.Tables[0].Rows[0]["UUserPwd"].ToString();
                lu.UEmail = ds.Tables[0].Rows[0]["UEmail"].ToString();
                lu.UCreateTime = Convert.ToDateTime(ds.Tables[0].Rows[0]["UCreateTime"].ToString());
                lu.UUserRole = ds.Tables[0].Rows[0]["UUserRole"].ToString();
                return lu;
            }
            else
            {
                return null;
            }
        }
        // 獲取所有用戶的列表信息
        public DataSet GetUserGroupList2(string sqlstr)
        {
            string cmdText = "select * from L_User where 1=1 ";
            if (sqlstr != "")
            {
                cmdText += sqlstr;
            }
            return DBHelper.getDataSet(cmdText);
        }
        //修改密碼
        public int UpdateUS(string UUserPwd)
        {
            int i = DBHelper.executeSql(@" update L_User set UUserPwd=@UUserPwd ;", new SqlParameter("@UUserPwd", UUserPwd));
            return i;
        }
        //檢查登錄(此方法有誤)
        public int CheckLogin(string Author, string UUserPwd)
        {
            try
            {
                int i = Convert.ToInt32(DBHelper.executeScalar("select count(*) from L_User where Author=@Author and UUserPwd=@UUserPwd ", new SqlParameter("@Author", Author), new SqlParameter("@UUserPwd", UUserPwd)));
                return i;
            }
            catch (Exception ee)
            {
                throw new Exception(ee.Message);
            }
        }
        //驗證用戶的角色
        public L_User checkAuthor(string Author)
        {
            DataSet ds = DBHelper.dataset("select * from L_User where Author=@Author ", new SqlParameter("@Author", Author));
            if (ds != null)
            {
                L_User LU = new L_User();
                LU.UUserRole = ds.Tables[0].Rows[0]["UUserRole"].ToString();
                return LU;
            }
            else
            {
                return null;
            }
        }
        //驗證用戶是否相同
        public int CheckUser(string Author)
        {
            try
            {
                int i = Convert.ToInt32(DBHelper.executeScalar("select count(*) from L_User where Author=@Author", new SqlParameter("@Author", Author)));
                return i;
            }
            catch (Exception ee)
            {
                throw new Exception(ee.Message);
            }
        }
        //驗證舊密碼是否相同
        public L_User Checkjiu(string Author)
        {
            DataSet ds = DBHelper.dataset("select * from L_User where Author=@Author ", new SqlParameter("@Author", Author));
            L_User LU = new L_User();
            LU.UUserPwd = ds.Tables[0].Rows[0]["UUserPwd"].ToString();
            return LU;
        }
    }
}


 BLL:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model;
using DAL;
using System.Data;
namespace BLL
{
    public class UserService
    {
        /// <summary>
        /// 添加用戶
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public int AddUser(L_User user)
        {
            UserDAL userDal = new UserDAL();
            return userDal.AddUser(user);
        }
        /// <summary>
        /// 修改user
        /// </summary>
        /// <param name="UID"></param>
        /// <param name="BlogTiltle"></param>
        /// <param name="BlogContent"></param>
        /// <param name="CreateTime"></param>
        /// <param name="Recommand"></param>
        /// <returns></returns>
        public int UpdateUser(int UID, string Author, string UUserPwd, string UEmail, DateTime UCreateTime, string UUserRole)
        {
            UserDAL uDAL = new UserDAL();
            return uDAL.UpdateUser(UID, Author, UUserPwd, UEmail, UCreateTime, UUserRole);
        }
        /// <summary>
        /// 刪除user
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public int DeleteUser(int UID)
        {
            UserDAL uDAL = new UserDAL();
            return uDAL.DeleteUser(UID);
        }
        /// <summary>
        /// 獲取所有用戶的信息列表
        /// </summary>
        /// <returns></returns>
        public DataSet GetUserGroupList2(string sqlstr)
        {
            UserDAL ugDAL = new UserDAL();
            return ugDAL.GetUserGroupList2(sqlstr);
        }
        /// <summary>
        /// 單獨的一條用戶信息
        /// </summary>
        /// <param name="UID"></param>
        /// <returns></returns>
        public L_User get_singleUser(int UID)
        {
            UserDAL uDAL = new UserDAL();
            return uDAL.get_singleUser(UID);
        }
        /// <summary>
        /// 查找login用戶名,實現登錄的安全驗證
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public L_User get_singleUser(string  name)
        {
            UserDAL uDAL = new UserDAL();
            return uDAL.get_singleUser(name);
        }
        /// <summary>
        /// 修改密碼
        /// </summary>
        /// <param name="AUserPwd"></param>
        /// <returns></returns>
        public int UpdateUS(string UUserPwd)
        {
            UserDAL uDAL = new UserDAL();
            return uDAL.UpdateUS(UUserPwd);
        }
        /// <summary>
        /// 檢查登錄(此方法有誤)
        /// </summary>
        /// <param name="Author"></param>
        /// <param name="UUserPwd"></param>
        /// <param name="UUserRole"></param>
        /// <returns></returns>
        public int CheckLogin(string Author, string UUserPwd)
        {
            UserDAL userDal = new UserDAL();
            return userDal.CheckLogin(Author, UUserPwd);
        }
        /// <summary>
        /// 檢查用戶權限
        /// </summary>
        /// <param name="Author"></param>
        /// <returns></returns>
        public L_User checkAuthor(string Author)
        {
            UserDAL userDal = new UserDAL();
            return userDal.checkAuthor(Author);
        }
        /// <summary>
        /// 檢查用戶名
        /// </summary>
        /// <param name="LoginName"></param>
        /// <returns></returns>
        public int CheckUser(string Author)
        {
            UserDAL userDal = new UserDAL();
            return userDal.CheckUser(Author);
        }
        /// <summary>
        /// 驗證舊密碼是否相同
        /// </summary>
        /// <param name="Author"></param>
        /// <returns></returns>
        public L_User Checkjiu(string Author)
        {
            UserDAL uDAL = new UserDAL();
            return uDAL.Checkjiu(Author);
        }
    }
}


接下來我們就可以開始在web頁面cs中開始我們的功能調用了,實現編輯功能的一部分代碼如下,PS:驗證input的

話建議大家多寫一些,JS在前臺,本頁面的驗證,以及服務器端的驗證,保證信息的安全性,養成好習慣。

protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            if(TextBox1.Text.Trim().ToString() =="")
            {
                lblMessage.Text = "<font color=\"red\" size=\"2\">請輸入用戶名</font>";
                NormalMethod.Show(this, "請完整填寫用戶的信息");
                return;
            }
            if (pwd.Text.Trim().ToString() == "")
            {
                lblMessage.Text = "<font color=\"red\" size=\"2\">請輸入用戶密碼</font>";
                NormalMethod.Show(this, "請完整填寫用戶的信息");
                return;
            }
            if (TextBox3.Text.Trim().ToString() == "")
            {
                lblMessage.Text = "<font color=\"red\" size=\"2\">請輸入用戶郵箱</font>";
                NormalMethod.Show(this, "請完整填寫用戶的信息");
                return;
            }
            if (ddlGroup.SelectedIndex == 0)
            {
                lblMessage.Text = "<font color=\"red\" size=\"2\">請輸入用戶組</font>";
                NormalMethod.Show(this, "請完整填寫用戶的信息");
                return;
            }
            if (my97.Value.ToString() == "")
            {
                lblMessage.Text = "<font color=\"red\" size=\"2\">請輸入正確的日期格式</font>";
                NormalMethod.Show(this, "請完整填寫用戶的信息");
                return;
            }
            UserService uService = new UserService();
            if (editor == "edit")
            {
                int i = uService.UpdateUser(Convert.ToInt32(Session["UID"].ToString()), TextBox1.Text.ToString(), pwd.Text.ToString(), TextBox3.Text.ToString(), DateTime.Now, ddlGroup.Text.ToString());
                if (i > 0)
                {
                    NormalMethod.ShowAndRedirect(this, "修改成功!(*^__^*)", "UserGuan.aspx");
                    editor = null;
                }
                else
                {
                    NormalMethod.ShowAndRedirect(this, "修改失敗!#(┬_┬)我的錯,我改還不行嗎?", "UserGuan.aspx");
                }
            }
            else
            {
                TextBox1.ReadOnly = false;
                TextBox3.ReadOnly = false;
                my97.Visible = true;
                L_User lu = new L_User();
                lu.Author = TextBox1.Text.ToString();
                lu.UUserPwd = pwd.Text.ToString();
                lu.UEmail = TextBox3.Text.ToString();
                lu.UUserRole = ddlGroup.SelectedValue.ToString();
                lu.UCreateTime =Convert.ToDateTime(my97.Value.ToString());
                int j = uService.AddUser(lu);
                if (j > 0)
                {
                    NormalMethod.ShowAndRedirect(this, "添加成功!(*^__^*)", "UserGuan.aspx");
                    TextBox1.Text = "";
                    TextBox3.Text = "";
                    my97.Value = "";
                    pwd.Text = "";
                }
                else
                {
                    NormalMethod.ShowAndRedirect(this, "添加失敗!#(┬_┬)我的錯,我改還不行嗎?", "UserGuan.aspx");
                }
            }
        }


上面的代碼配上bootstrap就會達到一種很棒的效果,這裏我就不截圖了,最後會給大家看一個遍。

緊接着就是設計其他的網頁所需的業務邏輯功能,如圖,

後臺的四操作把類庫的功能都寫好後就可以很容易的處理前臺的問題了。這裏的前臺我是用了XML和Repeater的綁定方式。

Repeater的綁定:


代碼如下:

<asp:Repeater ID="BlogList" runat="server">
                   <ItemTemplate>
                       <div class="title">
                           <a href='BlogShow.aspx?ID=<%# Eval("ID") %>'
title='<%# Eval("BlogTiltle") %>'>
                               <%# Eval("BlogTiltle")%></a>
                       </div>
                       <div>
                           <p style="color:Gray">
                               <%#BLL.SystemTools.FixLengthString
(BLL.SystemTools.NoHTML(Eval("BlogContent").ToString()),300)%>...
                           <a href='BlogShow.aspx?ID=<%# Eval("ID") %>'>[查看全文]</a>
                           </p>
                       </div>
                                                                                                                                                                                                                                                                                                                                                                     
                                                                                                                                                                                                                                                                                                                                                           
                       <span style="font-size: small; color: Gray">
瀏覽(<%# Eval("Click") %>)
                           &nbsp; &nbsp;|&nbsp; &nbsp;發佈時間:
<%# Eval("CreateTime")%></span><br /><hr style="border: 0.5px Dotted gray" />
                   </ItemTemplate>
               </asp:Repeater>
                                                                                                                                                                                                                                                                                                                                                                   
               <a  href="bowenList.aspx">查看全部博文>></a>

cs:

protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bindRepeater();
            }
        }
        //綁定Repeater的內容
        protected void bindRepeater()
        {
            BlogService blogService = new BlogService();
            BlogList.DataSource = blogService.getData();
            BlogList.DataBind();
        }



接着就是顯示博文的界面了:








這就基本實現了博客的主要功能,下面來看看一些jQuery的知識,比如Ajax的驗證,大家可以參考W3上的介紹學習,全面的瞭解後就可以去做一些圖片的輪播和天氣,text的顯示效果,這裏舉一例,圖片的輪播,下面是效果圖,大家可以按照說明自己做做,如圖,


3D gallery:



童話輪播:



還有最簡單的輪播:


講到這裏基本上前臺要做的就差不多了,對了忘說了,最後我想了想,由於是博客,所以就加了一個播放器,和XML很好的結合,實現了四操作和播放歌曲的功能,效果圖和代碼如下,


<object type="application/x-shockwave-flash"
data="http://service.weibo.com/staticjs/weiboshow.swf?verifier=b92d5ce3&amp;uid=1624424711&amp;
width=230&amp;height=500&amp;fansRow=2&amp;isTitle=1&amp;
isWeibo=1&amp;isFans=1&amp;noborder=0&amp;ptype=1&amp;
colors=cfe1f3,fafcff,444444,5093d5"
width="230" height="500" id="pre_flash" style="visibility: visible;">
                       <param name="wmode" value="transparent"/>
                       <param name="quality" value="high"/>
                       <param name="bgcolor" value="#FFFFFF"/>
                       <param name="align" value="L"/>
                       <param name="scale" value="noborder"/>
                       <param name="allowScriptAccess" value="sameDomain"/>
</object>



   到這裏前臺的大體功能基本上就差不多了,下面的一些技術我分三期講吧,有點多,已經三點了,大家明天先看着,我會繼續更新,有時間的話。謝謝大家的支持!謝謝莉子姐,米米姐,蘑菇姐,和5CTO一起成長!加油!

   博客地址:www.liujinlan.cc ,請大家多多指點,輕拍....




優酷視頻合作聯繫郵箱:[email protected]

youku地址:http://i.youku.com/liangxiao


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章