ASP.NET三层架构的浅层解读与实战练习

理论知识

三层架构比较官方的说法是为了符合“高内聚,低耦合”,的概念,把各个功能模块划分为表示层(UI)、业务逻辑层(BLL)和数据访问层(DAL)三层架构,各层之间采用接口相互访问,并通过对象模型的实体类(Model)作为数据传递的载体。
表示层(UI):用于用户交互的网页;
业务逻辑层(BLL):用于对数据进行逻辑处理的代码;
数据访问层(DAL):主要作用是进行数据库里面数据的调用;
实体类(Model):一个存放数据的类,方便其他层调用;
引用关系:UI层引用Model和BLL;
BLL层引用Model和DAL;
DAL层引用Model;
Model层不引用任何项目;

实战练习

我们以登录页面为例进行实战练习(我所用的软件是VS2019),首先创建一个空白解决方案,在里面添加Web应用程序和三个类库,并按照下图命名,按照上面引用关系进行引用。在这里插入图片描述
首先在UI层(WebApplication)设计一个登录页面(登录页面的前端的设计这里就不在进行写了,我们只讲数据传输的代码);

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;  //进行BLL层的引用
using Model; //进行Model层的引用

namespace WebApplication
{
    public partial class UserLogin : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void Button1_Click(object sender, EventArgs e)
        {
                tblUser.UserName = txtname.Text; //txtname为控件ID,这里是为了把数据存放在Model层
                tblUser.UserPwd = txtpwd.Text; //同上
                
            if (LoginBLL.GetoginBLL(Model.tblUser.UserName, Model.tblUser.UserPwd)) 
            //也可以不用通过Model层进行赋值,而直接把控件上的值赋给BLL层
            {
                Response.Redirect("Login.aspx");
            }
            else
            {
                Response.Write("<Script>alert('登陆失败')</Script>");
            }
        }
    }
}

Model层代码的展示:先在Model层的类库里面建一个命名为tblUser的类;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Model
{
    public static class tblUser
    {
        public static string UserName;  //存放的数据
        public static string UserPwd;
    }
}

BLL层代码展示,先在Model层的类库里面建一个命名为LoginBLL的类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;  //引用DAL层
namespace BLL
{
    public static class LoginBLL
    {
        public static bool GetoginBLL(string NameBLL, string PwdBLL) //给数据定义名字
        {
            return LoginDAL.GetLoginDAl(NameBLL, PwdBLL); //方法重载,把值赋给DAL层
        }
    }
}

DAL层代码展示, 先在DAL类库中建立DBHelp类和LoginDAL类:
DBHelp类代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;  //引用数据库程序集
using System.Data.SqlClient;  //引用数据库程序集
namespace DAL
{
 public static  class DBHelp
    {
        static string strsql = "Data Source=.;Initial Catalog=Human Resource;Integrated Security=True";  //数据库连接字符串
        public static SqlConnection Myconn = new SqlConnection(strsql);

        public static SqlCommand Mycomm = new SqlCommand();
        public static SqlDataAdapter da = new SqlDataAdapter();
    }
}

LoginDAL类代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model;
namespace DAL
{
   public static  class LoginDAL
    {
        public static bool GetLoginDAl(string NameDAl, string PwdDAL) 
        {
            bool state = false;
            try
            {
                DBHelp.Myconn.Open();
                string sqlstr = string.Format("select * from tbUser where User_name='{0}' and User_password='{1}'", NameDAl, PwdDAL); //操作数据库字符串
                DBHelp.Mycomm.CommandText = sqlstr;
                DBHelp.Mycomm.Connection = DBHelp.Myconn;
                object obj =DBHelp.Mycomm.ExecuteScalar(); //返回一个值进行判断
                if (obj  != null)
                {
                    state = true;
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
            finally
            {
                DBHelp.Myconn.Close();
            }
            return state;
        }
    }
}

总结

三层架构还是简单方便的,代码重用性更高,减少了代码的堆积,代码的逻辑性更加清楚,有利于后期的维护与更新。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值