• 三层架构之泛型应用的简单登录


    最近在学习泛型,感觉很灵活,做了一个小例子,方便我们了解泛型,本博文主要目的是为了分享这个小源码,没有什么其他目的,觉得文章哪里有问题的请指出来,不要,谢谢!

    项目结构图如下所示:

    Model类库中的UserEntity.cs文件如下所示:

    UserEntity
     1 using System;
    2 using System.Collections.Generic;
    3 using System.Linq;
    4 using System.Text;
    5
    6 namespace xu.Model.user
    7 {
    8 [Serializable]
    9 public class UserEntity
    10 {
    11 private string _username;
    12 private string _userpassword;
    13 public string UserName
    14 {
    15 set { _username = value; }
    16 get { return _username; }
    17 }
    18 public string UserPassword
    19 {
    20 set { _userpassword = value; }
    21 get { return _userpassword; }
    22 }
    23 }
    24 }

    IDAL类库中IBaseDAL.cs文件如下所示:

    IBaseDAL.cs
     1 using System;
    2 using System.Collections.Generic;
    3 using System.Linq;
    4 using System.Text;
    5
    6 namespace xu.IDAL
    7 {
    8 public interface IBaseDAL<T> where T : class
    9 {
    10 bool Inset(T model);
    11 bool Update(T model);
    12 bool Delete(object ID);
    13 T GetModel(object ID);
    14 IList<T> GetList();
    15 }
    16 }

    DAL类库中UserDAL.cs文件如下所示:

    UserDAL.cs
     1 using System;
    2 using System.Collections.Generic;
    3 using System.Linq;
    4 using System.Text;
    5
    6 namespace xu.DAL.user
    7 {
    8 public class UserDAL:xu.IDAL.IBaseDAL<xu.Model.user.UserEntity>
    9 {
    10 #region IBaseDAL<UserEntity> 成员
    11
    12 public bool Inset(xu.Model.user.UserEntity model)
    13 {
    14 throw new NotImplementedException();
    15 }
    16
    17 public bool Update(xu.Model.user.UserEntity model)
    18 {
    19 throw new NotImplementedException();
    20 }
    21
    22 public bool Delete(object ID)
    23 {
    24 throw new NotImplementedException();
    25 }
    26
    27 public xu.Model.user.UserEntity GetModel(object ID)
    28 {
    29 xu.Model.user.UserEntity userEntity = new xu.Model.user.UserEntity();
    30 userEntity.UserName = "admin";
    31 userEntity.UserPassword = "admin";
    32 return userEntity;
    33 }
    34
    35 public IList<xu.Model.user.UserEntity> GetList()
    36 {
    37 throw new NotImplementedException();
    38 }
    39
    40 #endregion
    41 }
    42 }

    BLL类库中BaseBLL.cs文件如下所示:

    BaseBLL
     1 using System;
    2 using System.Collections.Generic;
    3 using System.Linq;
    4 using System.Text;
    5
    6 namespace xu.BLL
    7 {
    8 public class BaseBLL<T,D>
    9 where T:class
    10 where D:xu.IDAL.IBaseDAL<T>,new()
    11 {
    12 private D dal = new D();
    13
    14 public virtual bool Insert(T model)
    15 {
    16 return dal.Inset(model);
    17 }
    18
    19 public virtual bool Update(T model)
    20 {
    21 return dal.Update(model);
    22 }
    23
    24 public virtual bool Delete(object ID)
    25 {
    26 return dal.Delete(ID);
    27 }
    28
    29 public virtual T GetModel(object ID)
    30 {
    31 return dal.GetModel(ID);
    32 }
    33
    34 public virtual IList<T> GetList()
    35 {
    36 return dal.GetList();
    37 }
    38 }
    39 }

    BLL类库中UserBLL.cs文件如下所示:(在UserBLL中没有什么业务的时候可以直接调用BaseBLL文件)

    UserBLL.cs
     1 using System;
    2 using System.Collections.Generic;
    3 using System.Linq;
    4 using System.Text;
    5
    6 namespace xu.BLL.user
    7 {
    8 public class UserBLL:xu.BLL.BaseBLL<xu.Model.user.UserEntity,xu.DAL.user.UserDAL>
    9 {
    10 }
    11 }

    页面login.aspx文件如下所示:

    View Code
     1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="login.aspx.cs" Inherits="login" %>
    2
    3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    4
    5 <html xmlns="http://www.w3.org/1999/xhtml">
    6 <head runat="server">
    7 <title></title>
    8 </head>
    9 <body>
    10 <form id="form1" runat="server">
    11 <div>
    12
    13 用户名:<asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
    14 密码:<asp:TextBox ID="txtPassword" TextMode="Password" runat="server"></asp:TextBox>
    15 <asp:Button ID="btnLogin" runat="server" Text="登录" onclick="btnLogin_Click" />
    16 </div>
    17 </form>
    18 </body>
    19 </html>

    login.cs文件如下所示:

    login.cs
     1 using System;
    2 using System.Collections.Generic;
    3 using System.Linq;
    4 using System.Web;
    5 using System.Web.UI;
    6 using System.Web.UI.WebControls;
    7
    8 public partial class login : System.Web.UI.Page
    9 {
    10 protected void Page_Load(object sender, EventArgs e)
    11 {
    12 if (!IsPostBack)
    13 { }
    14 }
    15
    16 /// <summary>
    17 /// 登录
    18 /// </summary>
    19 /// <param name="sender"></param>
    20 /// <param name="e"></param>
    21 protected void btnLogin_Click(object sender, EventArgs e)
    22 {
    23 xu.BLL.user.UserBLL userBLL = new xu.BLL.user.UserBLL();
    24 //如果UserBLL没有任何业务的话,那就不要继承了,在UI直接用BaseBLL这个泛型类就可以
    25 xu.BLL.BaseBLL<xu.Model.user.UserEntity, xu.DAL.user.UserDAL> baseBLL = new xu.BLL.BaseBLL<xu.Model.user.UserEntity, xu.DAL.user.UserDAL>();
    26 xu.Model.user.UserEntity userEntity = userBLL.GetModel(1);
    27 if (this.txtUserName.Text == userEntity.UserName && this.txtPassword.Text == userEntity.UserPassword)
    28 {
    29 Response.Write("<font color=red>登录成功!</font>");
    30 }
    31 else {
    32 Response.Write("<font color=red>登录失败!</font>");
    33 }
    34 }
    35 }

    本博文目的是在于分享源码,可以方便研究学习之用!请不要恶意喷,你觉得没必要学习可以马上离开本页面,谢谢。此网页如有什么不明白的地方,可以加入.NET技术交流群29844530 可以查看原博客,地址:http://www.cnblogs.com/liubiaocai/archive/2011/08/07/2129809.html

    一切伟大的行动和思想,都有一个微不足道的开始。微不足道的我,正在吸取知识的土壤,希望能取得成功!不嫌弃我微不足道的,愿交天下好友!

    源码下载,点击这里

    版权所有,转载请注明出处!

    一切伟大的行动和思想,都有一个微不足道的开始。微不足道的我,正在吸取知识的土壤,希望能取得成功!不嫌弃我微不足道的,愿交天下好友!

  • 相关阅读:
    HDU6216
    HDU6213
    HDU6191(01字典树启发式合并)
    HDU4825(01字典树)
    HDU5293(SummerTrainingDay13-B Tree DP + 树状数组 + dfs序)
    HDU2196(SummerTrainingDay13-D tree dp)
    HDU6201
    HDU6205
    HDU6195
    ffmpeg.编译20200719
  • 原文地址:https://www.cnblogs.com/cmsdn/p/2210825.html
Copyright © 2020-2023  润新知