Practice of Hierarchical Architecture Based on. NET platform (9) -- Third Implementation of the data access layer: Orm implementation based on the nbear framework

Source: Internet
Author: User

The previous article discussed how to implement the data access layer using SQL statements and stored procedures. In this article, we will discuss how to implement the data access layer using Orm.

Object/Relation Mapping (ORM) is generated with the development of object-oriented software development methods. Object-Oriented development is the mainstream development method in today's enterprise-level application development environment. Relational databases are the mainstream data storage systems that permanently store data in enterprise-level application environments. Objects and relational data are two forms of business entities. business entities are represented as objects in the memory and relational data in the database. Objects in the memory have associations and inheritance relationships. In the database, relational data cannot directly express many-to-many associations and inheritance relationships. Therefore, the object-relational ing (ORM) system generally exists in the form of middleware, mainly to map program objects to relational database data.

Currently, the. NET platform has many ORM {
Tagshow (Event)
} "> Available frameworks, such as nbear and ,{
Tagshow (Event)
} "> Nhib.pdf and so on. Here we choose nbear to implement Orm.

Nbear is {
Tagshow (Event)
} "> The open-source framework is mainly used to improve the development efficiency of the. NET platform, including multiple {
Tagshow (Event)
} "> Component. Only the ORM function is used here. For more information about how to use nbear, see the nbear getting started tutorial. The latest version of nbear: attachment: nbearv3.7.2_src.rar.

Next we will implement the ORM at the data access layer step by step.

1. Create an entity Design Project

To use nbear to implement the orm function, you must first create an entity design project, which will not be applied to the system, but must be used to generate the nbear entity class and {
Tagshow (Event)
} "> Configuration file.

First, create a project named nbearentitydesign under the solution and add it to the file nbear. common. design. DLL reference. This file is under the DIST directory of the nbear file package.

After completion, create a new C # file named entitydesign. CS under this project. This file is the design file. According to the design of the entity and database, write the complete code as follows:

Entitydesign. CS

  1. 1 using system;
  2. 2 using system. Collections. Generic;
  3. 3 using system. text;
  4. 4 using nbear. Common. design;
  5. 5
  6. 6 namespace nguestbook. nbearentitydesign
  7. 7 {
  8. 8 public interface tadmin: Entity
  9. 9 {
  10. 10 [primarykey]
  11. 11 int ID {Get ;}
  12. 12 [sqltype ("nvarchar (20)")]
  13. 13 string name {Get; set ;}
  14. 14 [sqltype ("nvarchar (50)")]
  15. 15 string password {Get; set ;}
  16. 16}
  17. 17
  18. 18 public interface tcomment: Entity
  19. 19 {
  20. 20 [primarykey]
  21. 21 int ID {Get ;}
  22. 22 [sqltype ("ntext")]
  23. 23 string content {Get; set ;}
  24. 24 datetime time {Get; set ;}
  25. 25 int messageid {Get; set ;}
  26. 26}
  27. 27
  28. 28 public interface tmessage: Entity
  29. 29 {
  30. 30 [primarykey]
  31. 31 int ID {Get ;}
  32. 32 [sqltype ("nvarchar (20)")]
  33. 33 string guestname {Get; set ;}
  34. 34 [sqltype ("nvarchar (100)")]
  35. 35 string guestemail {Get; set ;}
  36. 36 [sqltype ("ntext")]
  37. 37 string content {Get; set ;}
  38. 38 datetime time {Get; set ;}
  39. 39 [sqltype ("ntext")]
  40. 40 string reply {Get; set ;}
  41. 41 [sqltype ("nvarchar (10)")]
  42. 42 string ispass {Get; set ;}
  43. 43}
  44. 44}

Copy code

After the design, compile the project for backup.

2. Create nbear dedicated object classes and configuration files

In the distdirectory of the nbearar.tools.entitydesigntoentity.exe program, open the program, click "Browse", and select the nguestbook that was just compiled. nbearentitydesign. DLL file, and enter the corresponding namespace in the output namespace text box. Here we should enter "nguestbook. nbeardal ". Click "generate entities". The nbear dedicated entity class code is generated in the text box below.

Create a new project nbeardal under the solution to store the implementation code of all the orm Data Access layers. Create entities. CS in this project, and overwrite the code of this file with the automatically generated code. The dedicated entity class is ready.

In addition, you need to add the nbeardal project to the nbear. Common. dll and nbear. Data. dll references. Both files are under the DIST directory.

Click "generate configuration". The configuration code is generated. Create the nbearconfig. xml file in the web project and copy the generated code to the file to save it.

Finally, modify the Web. config file. Add the following configuration code:

  1. <Configsections>
  2. <Section name = "entityconfig" type = "nbear. Common. entityconfigurationsection, nbear. Common"/>
  3. </Configsections>
  4. <Entityconfig>
  5. <Shortdes>
  6. <Add key = "sample entity config" value = "~ /Nbearconfig. xml "/>
  7. </Shortdes>
  8. </Entityconfig>

Copy code

Then add the following items under the <connectionstrings> node:

  1. <Add name = "nbearconnectionstring" connectionstring = "Server = localhost \ sqlexpress; database = nguestbook; uid = webuser; Pwd = 123456" providername = "nbear. data. sqlserver. sqldbprovider "/>

Copy code

Connectionstring is the connection string, which can be modified according to individual circumstances. Sqlserver2005 is used here.
Because the database has been created in the previous article, you do not need to create a database here.

3. Write a converter

There is a conflict: the business logic layer and the presentation layer need to use a common entity class, such as admininfo, while nbear needs to use a dedicated entity class. How can this problem be solved? The method I use here is a method that I call "converter. That is, write a dedicated converter for no entity to convert the two entity classes. Take the Administrator entity as an example. This converter is written in the adminconvertor. CS file under the nbeardal project. The Code is as follows:

Adminconvertor

  1. 1 using system;
  2. 2 using nguestbook. entity;
  3. 3
  4. 4 namespace nguestbook. nbeardal
  5. 5 {
  6. 6/** // <summary>
  7. 7 // entity converter-Administrator
  8. 8 /// </Summary>
  9. 9 Public sealed class adminconvertor
  10. 10 {
  11. 11/*** // <summary>
  12. 12 // convert the normal administrator entity class to the nbear dedicated administrator entity class
  13. 13 /// </Summary>
  14. 14 /// <Param name = "commonentity"> common entity class </param>
  15. 15 /// <returns> nbear special Entity class </returns>
  16. 16 public static tadmin commonentitytonbearentity (admininfo commonentity)
  17. 17 {
  18. 18 tadmin nbaerentity = new tadmin ();
  19. 19 nbaerentity. ID = commonentity. ID;
  20. 20 nbaerentity. Name = commonentity. Name;
  21. 21 nbaerentity. Password = commonentity. Password;
  22. 22
  23. 23 return nbaerentity;
  24. 24}
  25. 25
  26. 26/** // <summary>
  27. 27 // The nbear special administrator entity class is converted to the normal administrator entity class
  28. 28 /// </Summary>
  29. 29 // <Param name = "nbearentity"> nbear dedicated entity class </param>
  30. 30 /// <returns> normal entity class </returns>
  31. 31 public static admininfo nbearentitytocommonentity (tadmin nbearentity)
  32. 32 {
  33. 33 admininfo commonentity = new admininfo ();
  34. 34 commonentity. ID = nbearentity. ID;
  35. 35 commonentity. Name = nbearentity. Name;
  36. 36 commonentity. Password = nbearentity. Password;
  37. 37
  38. 38 return commonentity;
  39. 39}
  40. 40}
  41. 41}

Copy code

4. Implement the data access layer
After completing the above work, we can implement the data access layer. With the support of the nbear framework, we can easily access the database using Orm. For details about nbear, I will not go into details here. Take the Administrator as an example. The specific code is as follows:

Admindal. CS

  1. 1 using system;
  2. 2 using system. Collections. Generic;
  3. 3 using system. text;
  4. 4 using system. Data. Common;
  5. 5 using nguestbook. idal;
  6. 6 using nguestbook. entity;
  7. 7 using nbear. Common;
  8. 8 using nbear. Data;
  9. 9
  10. 10 namespace nguestbook. nbeardal
  11. 11 {
  12. 12 public class admindal: iadmindal
  13. 13 {
  14. 14/** // <summary>
  15. 15 // Insert the Administrator
  16. 16 /// </Summary>
  17. 17 // <Param name = "admin"> administrator entity class </param>
  18. 18 /// <returns> Successful </returns>
  19. 19 public bool insert (admininfo admin)
  20. 20 {
  21. 21 gateway. setdefadatabase database ("nbearconnectionstring ");
  22. 22 dbtransaction transcation = gateway. Default. begintransaction ();
  23. 23 try
  24. 24 {
  25. 25 gateway. Default. Save <tadmin> (adminconvertor. commonentitytonbearentity (Admin ));
  26. 26 transcation. Commit ();
  27. 27 return true;
  28. 28}
  29. 29 catch
  30. 30 {
  31. 31 transcation. rollback ();
  32. 32 return false;
  33. 33}
  34. 34 finally
  35. 35 {
  36. 36 gateway. Default. closetransaction (transcation );
  37. 37}
  38. 38}
  39. 39
  40. 40/** // <summary>
  41. 41 // Delete the Administrator
  42. 42 /// </Summary>
  43. 43 // <Param name = "ID"> ID of the Administrator to be deleted </param>
  44. 44 // <returns> Successful </returns>
  45. 45 public bool Delete (int id)
  46. 46 {
  47. 47 gateway. setdefadatabase database ("nbearconnectionstring ");
  48. 48 dbtransaction transcation = gateway. Default. begintransaction ();
  49. 49 try
  50. 50 {
  51. 51 gateway. Default. Delete <tadmin> (ID );
  52. 52 transcation. Commit ();
  53. 53 return true;
  54. 54}
  55. 55 catch
  56. 56 {
  57. 57 transcation. rollback ();
  58. 58 return false;
  59. 59}
  60. 60 finally
  61. 61 {
  62. 62 gateway. Default. closetransaction (transcation );
  63. 63}
  64. 64}
  65. 65
  66. 66/** // <summary>
  67. 67 // update administrator Information
  68. 68 /// </Summary>
  69. 69 // <Param name = "admin"> administrator entity class </param>
  70. 70 // <returns> Successful </returns>
  71. 71 public bool Update (admininfo admin)
  72. 72 {
  73. 73 gateway. setdefadatabase database ("nbearconnectionstring ");
  74. 74 dbtransaction transcation = gateway. Default. begintransaction ();
  75. 75 propertyitem [] properties = {
  76. 76 new propertyitem ("name "),
  77. 77 new propertyitem ("password ")
  78. 78 };
  79. 79 object [] values = {
  80. 80 Admin. Name,
  81. 81 Admin. Password
  82. 82 };
  83. 83 try
  84. 84 {
  85. 85 gateway. Default. Update <tadmin> (properties, values, null, transcation );
  86. 86 transcation. Commit ();
  87. 87 return true;
  88. 88}
  89. 89 catch
  90. 90 {
  91. 91 transcation. rollback ();
  92. 92 return false;
  93. 93}
  94. 94 finally
  95. 95 {
  96. 96 gateway. Default. closetransaction (transcation );
  97. 97}
  98. 98}
  99. 99
  100. 100/*** // <summary>
  101. 101 // obtain administrator information by ID
  102. 102 /// </Summary>
  103. 103 /// <Param name = "ID"> administrator ID </param>
  104. 104 /// <returns> administrator entity class </returns>
  105. 105 public admininfo getbyid (int id)
  106. 106 {
  107. 107 gateway. setdefadatabase database ("nbearconnectionstring ");
  108. 108 tadmin = gateway. Default. Find <tadmin> (tadmin. _. ID = ID );
  109. 109 return tadmin = NULL? Null: adminconvertor. nbearentitytocommonentity (tadmin );
  110. 110}
  111. 111
  112. 112/*** // <summary>
  113. 113 // obtain administrator information by user name and password
  114. 114 /// </Summary>
  115. 115 /// <Param name = "name"> User Name </param>
  116. 116 /// <Param name = "password"> password </param>
  117. 117 /// <returns> administrator entity class. If the object does not exist, null is returned. </returns>
  118. 118 public admininfo getbynameandpassword (string name, string password)
  119. 119 {
  120. 120 gateway. setdefadatabase database ("nbearconnectionstring ");
  121. 121 tadmin = gateway. Default. Find <tadmin> (tadmin. _. Name = Name & tadmin. _. Password = PASSWORD );
  122. 122 return tadmin = NULL? Null: adminconvertor. nbearentitytocommonentity (tadmin );
  123. 123}
  124. 124
  125. 125/*** // <summary>
  126. 126 // obtain administrator information by Administrator name
  127. 127 /// </Summary>
  128. 128 /// <Param name = "name"> Administrator name </param>
  129. 129 /// <returns> administrator entity class </returns>
  130. 130 public admininfo getbyname (string name)
  131. 131 {
  132. 132 gateway. setdefadatabase database ("nbearconnectionstring ");
  133. 133 tadmin = gateway. Default. Find <tadmin> (tadmin. _. Name = Name );
  134. 134 return tadmin = NULL? Null: adminconvertor. nbearentitytocommonentity (tadmin );
  135. 135}
  136. 136
  137. 137/*** // <summary>
  138. 138 // obtain all administrator Information
  139. 139 /// </Summary>
  140. 140 /// <returns> administrator object class set </returns>
  141. 141 public ilist <admininfo> getall ()
  142. 142 {
  143. 143 ilist <admininfo> admincollection = new list <admininfo> ();
  144. 144 gateway. setdefadatabase database ("nbearconnectionstring ");
  145. 145 tadmin [] tadmincollection = gateway. Default. findarray <tadmin> (null, tadmin. _. Id. DESC );
  146. 146 foreach (tadmin in tadmincollection)
  147. 147 {
  148. 148 admincollection. Add (adminconvertor. nbearentitytocommonentity (tadmin ));
  149. 149}
  150. 150 return admincollection;
  151. 151}
  152. 152}
  153. 153}

Copy code

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.