Practice of A. Net-based layered architecture (10)-Implementation of the business logic layer

Source: Internet
Author: User
Tags sha1 administrator password

In this article, we will implement an nguestbook business logic layer.

In practical applications, the business logic layer is crucial. It carries the core part of the entire system and is also the most important part of the customer. The implementation of this Part usually requires the cooperation of technical experts and field experts. Of course, in this article {
Tagshow (Event)
} "> In the series of demos, due to the simplicity of the business logic, it may not be obvious here.

In the business logic layer implementation in this article, the business logic layer undertakes the following responsibilities:
1. encapsulation of different data access layers. So that the presentation layer does not care about the specific data access layer.
2. Business Logic Data filling and conversion. For example, administrator password encryption.
3. Core Business implementation. Here, many business logics only have one line of code, that is, one business logic method exactly corresponds to one data access method, but there are also multiple data access methods to implement the business. For example, the changepassword method in adminbll calls the getbyid and update Methods of admindal. In addition, although many methods only call one data access method, the differences between the two methods can be seen from the perspective of naming. For example, in admindal, The getbynameandpassword name is obviously a problem from the perspective of the database-that is, the corresponding information is retrieved based on the values of the specified name and password fields, you do not need to know the meaning of the service. In adminbll, the method to call it is login, which is a problem from the business point of view-that is, this method is administrator login.

The following steps implement the business logic layer:

1. Create a project
In this architecture, the business logic layer can be replaced. And the business logic layer is not directly coupled with the presentation layer, but through dependency injection {
Tagshow (Event)
} "> Mechanism implementation. Therefore, we do not name this business logic layer BLL directly, but create a project named simplebll and place the relevant code of this business logic layer.

2 .{
Tagshow (Event)
} "> Configure dependency Injection
The business logic layer loads the corresponding data access layer through the reflection factory. In this way, you need to configure the data access layer to be used in Web. config. Open Web. config, locate the "Dal" item under the deleteworker node, and assign the value to the Project Name of the data access layer to be used. For example, to use nbeardal, this item should be written as follows:
<Add key = "Dal" value = "nbeardal"/>

3. Write hash encryption {
Tagshow (Event)
} "> Tool
Because hash encryption is required in multiple business logic layers, write an auxiliary class encryptor in the utility project to complete this work. The specific code of this auxiliary class is as follows:

Encryptor. CS

  1. 1 using system;
  2. 2 using system. Collections. Generic;
  3. 3 using system. text;
  4. 4
  5. 5 namespace nguestbook. Utility
  6. 6 {
  7. 7/*** // <summary>
  8. 8 // auxiliary class-used to hash sensitive data to achieve Encryption
  9. 9 /// </Summary>
  10. 10 public sealed class encryptor
  11. 11 {
  12. 12/*** // <summary>
  13. 13 // calculate the Hash hash using the MD5 Algorithm
  14. 14 /// </Summary>
  15. 15 /// <Param name = "text"> plaintext </param>
  16. 16 /// <returns> hash value </returns>
  17. 17 public static string md5encrypt (string text)
  18. 18 {
  19. 19 Return System. Web. Security. formsauthentication. hashpasswordforstoringinconfigfile (text, "MD5 ");
  20. 20}
  21. 21
  22. 22/*** // <summary>
  23. 23 // calculate the Hash hash using the sha1 Algorithm
  24. 24 /// </Summary>
  25. 25 /// <Param name = "text"> plaintext </param>
  26. 26 /// <returns> hash value </returns>
  27. 27 public static string sha1encrypt (string text)
  28. 28 {
  29. 29 return system. Web. Security. formsauthentication. hashpasswordforstoringinconfigfile (text, "sha1 ");
  30. 30}
  31. 31}
  32. 32}

Copy code

4. Implement the business logic layer
With the above preparations and previously implemented {
Tagshow (Event)
} "> The implementation of components and business logic layer is very intuitive. Here, the Administrator is used as an example to demonstrate how to implement the business logic layer.
The adminbll class is created in the adminbll. CS file of the simplebll project and implements the iadminbll {
Tagshow (Event)
} "> The specific code is as follows:

Iadminbll

  1. 1 using system;
  2. 2 using system. Collections. Generic;
  3. 3 using system. text;
  4. 4 using nguestbook. entity;
  5. 5 using nguestbook. factory;
  6. 6 using nguestbook. ibll;
  7. 7 using nguestbook. idal;
  8. 8 using nguestbook. utility;
  9. 9
  10. 10 namespace nguestbook. ibll
  11. 11 {
  12. 12/*** // <summary>
  13. 13 // business logic layer interface-Administrator
  14. 14 /// </Summary>
  15. 15 public class adminbll: iadminbll
  16. 16 {
  17. 17/** // <summary>
  18. 18 // Add an administrator
  19. 19 /// </Summary>
  20. 20 /// <Param name = "admin"> new Administrator entity class </param>
  21. 21 /// <returns> Successful </returns>
  22. 22 public bool add (admininfo admin)
  23. 23 {
  24. 24 Admin. Password = encryptor. md5encrypt (Admin. Password );
  25. 25 return dalfactory. createadmindal (). insert (Admin );
  26. 26}
  27. 27
  28. 28/** // <summary>
  29. 29 // Delete the Administrator
  30. 30 /// </Summary>
  31. 31 // <Param name = "ID"> ID of the Administrator to be deleted </param>
  32. 32 // <returns> Successful </returns>
  33. 33 public bool remove (int id)
  34. 34 {
  35. 35 return dalfactory. createadmindal (). Delete (ID );
  36. 36}
  37. 37
  38. 38/** // <summary>
  39. 39 // modify the Administrator Password
  40. 40 /// </Summary>
  41. 41 // <Param name = "ID"> ID of the administrator who wants to change the password </param>
  42. 42 // <Param name = "password"> New Password </param>
  43. 43 // <returns> Successful </returns>
  44. 44 public bool changepassword (int id, string password)
  45. 45 {
  46. 46 Password = encryptor. md5encrypt (password );
  47. 47 admininfo admin = dalfactory. createadmindal (). getbyid (ID );
  48. 48 Admin. Password = password;
  49. 49 return dalfactory. createadmindal (). Update (Admin );
  50. 50}
  51. 51
  52. 52/** // <summary>
  53. 53 // log on as an administrator
  54. 54 /// </Summary>
  55. 55 // <Param name = "name"> Administrator Logon Name </param>
  56. 56 /// <Param name = "password"> administrator password </param>
  57. 57 // <returns> If the logon succeeds, the corresponding administrator's entity class is returned; otherwise, null is returned. </returns>
  58. 58 public admininfo login (string name, string password)
  59. 59 {
  60. 60 Password = encryptor. md5encrypt (password );
  61. 61 Return dalfactory. createadmindal (). getbynameandpassword (name, password );
  62. 62}
  63. 63
  64. 64/** // <summary>
  65. 65 // obtain all administrator Information
  66. 66 /// </Summary>
  67. 67 /// <returns> administrator object class set </returns>
  68. 68 public ilist <admininfo> getall ()
  69. 69 {
  70. 70 return dalfactory. createadmindal (). getall ();
  71. 71}
  72. 72}
  73. 73}

Copy code

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.