This article is a mirror article of machine translation, please click here to jump to the original article.

View: 42799|Reply: 2

[Communication] Detailed explanation of the use and introduction of the three-layer architecture in C#, with examples

[Copy link]
Posted on 10/22/2014 9:59:33 PM | | |
Three layers: UI (interface), BLL (business logic layer), DAL (data access layer) These three are must-haves, of which BLL and the classes in DAL are public classes, because the UI needs to call BLL, and BLL needs to call DAL, and UTILITY (the underlying method for connecting to the database and performing basic operations of "adding, deleting, modifying, and checking"). In addition, there can also be entity layers such as ENTITY (which maps data tables) and Common (this library generally places some general methods, such as data validation methods, control operation methods, etc.).
Simply put, it is
Accessing data from the database is a data access layer
Sorting out the business relationships of related data is a layer of business logic
The representation of the collated data shows that this is a representation layer.

By the way, I helped you find some information:
       A good hierarchical structure can make the division of labor for developers clearer. Once the interfaces between the layers are defined, developers responsible for different logic designs can disperse their efforts and work hand in hand. For example, UI personnel only need to consider the experience and operation of the user interface, domain designers can only focus on the design of business logic, and database designers do not have to worry about cumbersome user interactions. Each developer's task is confirmed, and the development progress can be quickly improved.

       The benefits of loose coupling are clear. If a system is not hierarchical, then their logic is tightly intertwined and interdependent, and no one is replaceable. Once a change occurs, it will affect the whole body, and the impact on the project will be extremely serious. Reducing the dependence between layers can not only ensure future scalability, but also have obvious advantages in reusability. Once each functional module has defined a unified interface, it can be called by each module without having to develop the same function repeatedly.

       To carry out a good hierarchical structure design, standards are also essential. Only on a certain level of standardization can this system be scalable and replaceable. The communication between layers also necessarily ensures the standardization of interfaces.

  1. 代码

  2.         /// <summary>
  3.         /// 初始化登录名称、登录密码(Model类)
  4.         /// </summary>
  5.         private string adminUser = string.Empty; //设置用户名称为空值
  6.         private string adminPwd = string.Empty; //设置用户密码为空值
  7.         public string AdminUser
  8.         {
  9.             get
  10.             {
  11.                 return this.adminUser;
  12.             }
  13.             set
  14.             {
  15.                 this.adminUser = value;
  16.             }
  17.         }
  18.         public string AdminPwd
  19.         {
  20.             get
  21.             {
  22.                 return this.adminPwd;
  23.             }
  24.             set
  25.             {
  26.                 this.adminPwd = value;
  27.             }
  28.         }

  29. 代码

  30.         /// <summary>
  31.         /// 用户登录(BLL类)
  32.         /// </summary>
  33.         /// <param name="m"></param>
  34.         /// <returns></returns>
  35.         public static int sysLogin(Model m)
  36.         {
  37.             string str = "adminValid"; //存储过程名称
  38.             SqlParameter[] sqlParameter =
  39.                 {
  40.                     // 将UI层传递过来的用户名称和密码赋值给存储过程中的变量分别是adminUser和adminPwd(注意大小写)
  41.                     new SqlParameter("adminUser",m.AdminUser),
  42.                     new SqlParameter("adminPwd",m.AdminPwd)
  43.                 };
  44.             DAL d = new DAL();
  45.             return Int32.Parse(d.ExecuteScalar(str,sqlParameter));
  46.         }

  47. 代码

  48.         /// <summary>
  49.         /// 新建一个SQL登录链接
  50.         /// </summary>
  51.         /// <returns></returns>
  52.         private static SqlConnection con()
  53.         {
  54.             return new SqlConnection("Data Source=localhost;Initial Catalog=数据库名称;Integrated Security=SSPI;");
  55.         }
  56.         /// <summary>
  57.         /// 执行操作(DAL类)
  58.         /// </summary>
  59.         /// <param name="str"></param>
  60.         /// <param name="sql"></param>
  61.         /// <returns></returns>
  62.         public string ExecuteScalar(string str, SqlParameter[] sql)
  63.         {
  64.             SqlConnection con = DAL.con();
  65.             try
  66.             {
  67.                 con.Open();
  68.                 SqlCommand com = new SqlCommand(str, con);
  69.                 com.CommandType = CommandType.StoredProcedure;
  70.                 com.Parameters.AddRange(sql);
  71.                 return Convert.ToString(com.ExecuteScalar()); //返回受影响的行数(例如影响的行数为1,那么返回数值1到BLL层,然后BLL层将数值1返回到UI层)
  72.             }
  73.             catch (Exception Error)
  74.             {
  75.                 throw Error;
  76.             }
  77.             finally
  78.             {
  79.                 con.Close();
  80.             }
  81.         }

  82. 代码

  83.         //UI 层
  84.         Model m = new Model(); //实例化Model类
  85.         m.AdminUser = this.TextBox1.Text.ToString(); //将文本框1中的值传递给Model类中的AdminUser
  86.         m.AdminPwd = this.TextBox2.Text.ToString(); //将文本框2中的值传递给Model类中的AdminPwd
  87.         if (BLL.sysLogin(m) > 0)
  88.         {
  89.             this.Label1.Text = "登录成功!马上进入管理平台...";
  90.         }
  91.         else
  92.         {
  93.             this.Label1.Text = "用户或密码错误,请重新输入!";
  94.         }

  95. 代码

  96. --存储过程 (SQL2005)
  97. set ANSI_NULLS ON
  98. set QUOTED_IDENTIFIER ON
  99. go

  100. ALTER PROC [dbo].[adminValid]
  101. @adminUser varchar(20),@adminPwd varchar(120)
  102. AS
  103. SELECT COUNT(*) FROM admin WHERE adminUser = @adminUser AND adminPwd = @adminPwd
Copy code







Previous:Many network card tools need to be installed with winpcap, do you know what this is for?
Next:Pay attention to WeChat "Yantang Dairy" to draw the "crown fungus" prize 100%
Posted on 10/6/2021 9:51:23 AM |
Good thing, thank you to the owner for providing !!
Posted on 10/6/2021 11:09:10 AM |
Learn about the three-tier architecture in C#
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com