• asp webForm 三层框架的简单实例(一)未完待续


    asp webForm 三层框架的简单实例(一)未完待续--

    置顶 2017年02月19日 18:19:56 活的就是心态 阅读数 6787 标签: webformasp.net架构框架asp更多

    个人分类: .net

    版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/shaoyanning/article/details/55806330

    Note:本文主要通过简单的实例引导初学者对webForm 三层框架的一个入门,其中也是个自学获得的理解,难免与大神有出入,敬请批评指导。

    一、引言

    webForm是asp.net 其中的一种web开发方式,其三层框架是经验获得最有效的软件开发模式架构。大体上可分为三层,就是很多人所说的视图层(UI),业务逻辑层(BLL),数据访问层(DAL)。其中,为了辅助三层还产生了Model,Common,IDAL,IBLL,其中Model也叫Entity实体,是对数据库中具体表内字段以 “”类“” 方式的封装。在各层传递时方便安全有效的使用。Common主要是存放整个项目通用的“小工具库”。IDAL、IBLL都是为了降低各层之间的耦合度而出现的一接口层。其中在各层划分时那面会出现归属那层更合适的问题,出现的这种情况,可以根据实际项目,经验对其进行划分,或者是看是否能独立出来以解除两层间的耦合度(所谓的耦合度说白了就是你的变化影响我的程度,理想情况下就是你变化不会影响我变化),如下图一所示WebForm三层框架详图,对其理解时可以忽略接口层。

                 

    本实例中,即使建立IDL,和IBLL,common等项目也未使用直接忽略即可。实例是在visiual studio 2010,附加数据库文件的方式完成,文中最后附上源代码,敬请参考。

            开始我们的代码:本实例是通过在一个解决方案下建立多个项目搭建的三层框架。当然,你也可以通过在一个解决方案下通过一个项目搭建自己的三层框架(这两种还是有区别的,详细区别自己可以查阅详细资料,可以:仅供参考)。

    本实例,一共建立了如下所示,共七个项目,分别是:BLL、c:\....\Login\(UI)、Common、DAL、IBLL、IDAL、Model。

                                                                      

    其中,本实例中没用有用到IBLL、DAL、Common项目,不用理会。下面,我们开始我们的编程之旅。

    首先,我们的需求是,建立一个用户登录页面(UI),然后界面接收到的数据传递给BLL层,BLL层对数据进行处理,这是需要数据库中的数据,但是他只能通过DAL获得后台数据,期间关于数据的传递当属性列多了的时候,通过Model进行封装就可以实现对象方式的传传递,由于本实例字段较少,只进行了封装,没有使用,敬请注意。

    二、代码

    1)UI代码块:

    Login.aspx:

    
     
    1. <html xmlns="http://www.w3.org/1999/xhtml">

    2. <head runat="server">

    3. <title></title>

    4. </head>

    5. <body>

    6. <form id="form1" runat="server">

    7. <asp:Label ID="Label1" runat="server" Text="用户名:"></asp:Label>

    8. <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

    9. <br />

    10. <asp:Label ID="Label2" runat="server" Text="密码:"></asp:Label>

    11. <asp:TextBox ID="TextBox2" runat="server" TextMode="Password"></asp:TextBox>

    12. <br />

    13. <asp:Button ID="Button1" runat="server" Text="取消" />

    14. <asp:Button ID="Button2" runat="server" Text="提交" οnclick="Button2_Click" />

    15. </form>

    16. </body>

    17. </html>

    Login.aspx.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.  
    9. public partial class Login_Login : System.Web.UI.Page

    10. {

    11. protected void Page_Load(object sender, EventArgs e)

    12. {

    13.  
    14. }

    15.  
    16. protected void Button2_Click(object sender, EventArgs e)

    17. {

    18. if (BLL.LoginManager.LoginManagers.Login(TextBox1.Text.Trim(), TextBox2.Text.Trim()))

    19. {

    20. Response.Write(@"<script type='text/javascript'>alert('登陆成功')</script>");

    21. }

    22. else {

    23. Response.Write(@"<script type='text/javascript'>alert('登陆失败!!')</script>");

    24. }

    25.  
    26. }

    27. }

    2)BLL代码块:

    LoginManagers.cs

    
     
    1. using System;

    2. using System.Collections.Generic;

    3. using System.Linq;

    4. using System.Text;

    5.  
    6.  
    7. namespace BLL.LoginManager

    8. {

    9. public class LoginManagers

    10. {

    11. public static bool Login(string userName,string userPassword)

    12. {

    13. //如果需要对密码进行加密,在return前可以对userPassword进行加密处理

    14. return DAL.LoginDAL.LoginDAL.login(userName,userPassword);

    15. }

    16. }

    17. }


    3)DAL代码块:

    Configurations.cs

    
     
    1. using System;

    2. using System.Collections.Generic;

    3. using System.Linq;

    4. using System.Text;

    5. using System.Data;

    6. using System.Data.SqlClient;

    7. using System.Configuration;

    8. namespace DAL.LoginDAL

    9. {

    10. public partial class Configurations

    11. {

    12. public static string connectionString

    13. {

    14. get { return System.Configuration.ConfigurationManager.ConnectionStrings["constr"].ConnectionString; }

    15. set { }

    16. }

    17. }

    18. }

    19.  
    SQLHelper.cs
    
    
     
    1. using System;

    2. using System.Collections.Generic;

    3. using System.Linq;

    4. using System.Text;

    5. using System.Data.SqlClient;

    6. using System.Data;

    7.  
    8. namespace DAL.LoginDAL

    9. {

    10. class SQLHelper

    11. {

    12. public static object ExecuteScalar(string sql,CommandType type,params SqlParameter[] para){

    13. using (SqlConnection conn = new SqlConnection(DAL.LoginDAL.Configurations.connectionString)) {

    14. using (SqlCommand cmd = new SqlCommand(sql, conn)) {

    15. cmd.CommandType = type;

    16. if (para != null)

    17. {

    18. cmd.Parameters.AddRange(para);

    19. }

    20. conn.Open();

    21. return cmd.ExecuteScalar() ;

    22. }

    23.  
    24. }

    25. }

    26. }

    27. }

    LoginDAL.cs

    
     
    1. using System;

    2. using System.Collections.Generic;

    3. using System.Linq;

    4. using System.Text;

    5. using System.Data;

    6. using System.Data.SqlClient;

    7.  
    8. namespace DAL.LoginDAL

    9. {

    10. public class LoginDAL

    11. {

    12. public static bool login(string userName,string userPassword) {

    13. if (userName.Trim() !="" && userPassword.Trim() !="")

    14. {

    15. string sqlstr = @"select * from UserInfo where UserName=@userName and PassWord=@userPassword";

    16. SqlParameter[] paras = new SqlParameter[]

    17. {

    18. new SqlParameter("@userName",userName),

    19. new SqlParameter("@userPassword",userPassword)

    20. };

    21. int result = (int)DAL.LoginDAL.SQLHelper.ExecuteScalar(sqlstr, CommandType.Text, paras);

    22. if (result > 0)

    23. {

    24. return true;

    25. }

    26. else

    27. {

    28. return false;

    29. }

    30. }

    31. else {

    32. return false;

    33. }

    34. }

    35. }

    36. }

    4)Model代码块:

    Model.cs

    
     
    1. using System;

    2. using System.Collections.Generic;

    3. using System.Linq;

    4. using System.Text;

    5.  
    6. namespace Model.UserModel

    7. {

    8. class UserInfo

    9. {

    10. public int Id { get; set; }

    11. public string UserName { get; set; }

    12. public string PassWord { get; set; }

    13. public int Level { get; set; }

    14. }

    15. }

    5)数据据的定义:


    2)web.config配置:

    web.config

    
     
    1. <?xml version="1.0"?>

    2. <!--

    3. 有关如何配置 ASP.NET 应用程序的详细信息,请访问

    4. http://go.microsoft.com/fwlink/?LinkId=169433

    5. -->

    6. <configuration>

    7. <system.web>

    8. <compilation debug="true" targetFramework="4.0"/>

    9. </system.web>

    10. <connectionStrings>

    11. <add name="constr" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\mydb.mdf;Integrated Security=True;User Instance=True"/>

    12. </connectionStrings>

    13. </configuration>

    14.  

    其中,关于<connectionString></connectionString>和数据库连接字符串的只是参照:强烈推荐初学者看

    补充:最终的项目完整目录:

      
    可以根据需要参考如下:

     http://www.cnblogs.com/sufei/archive/2010/01/14/1648026.html
                     http://blog.sina.com.cn/s/blog_71077ea1010154iv.html

      本文实例的源代码:

                       链接:源代码链接 密码: t52y

                                                                                                                                                                                            个人新浪微博:桑泽塔雅

  • 相关阅读:
    Linux目录管理常用指令
    生成器
    Python上的MVC和MVT理解,请求头,请求体,请求行的理解
    sellect、poll、epoll
    冒泡法排序
    (android / IOS)
    发现一个bug如何定位是前端还是后台问题?
    ANR----以及如何定位是前端问题还是后台问题?
    给你一个web端项目你如何展开测试?
    给你一个app你如何展开测试?
  • 原文地址:https://www.cnblogs.com/grj001/p/12224545.html
Copyright © 2020-2023  润新知