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