ASP. net mvc + EF framework + EasyUI permission management series (2)-database access layer design Demo

Source: Internet
Author: User

In this blog, we continue to implement my permission series. This blog has not been written for a while. The point is that I want to write it, but I decided to write it again, this is because we want to learn. When someone else puts forward their opinions, we can refer to and adopt them. However, we do not have to adopt them. We have used CodeFirst for many blogs, at present, few people use this method to implement it, But CodeFirst is used to implement it. However, the purpose of writing this blog is not to learn many things, but to learn a programming idea, so I will continue to talk about this topic today.

1. Model Design

(1) Today, we will first preliminarily design the model building, that is, we will build our own entity objects under the edmx file, the steps for adding an object are not long-winded here. You can design the steps as shown in the figure ,:

(2) explanation: the maximum length of the UName attribute in the UserInfo (User table) Table is 32, which can be Null or true. By default, the maximum length of the Pwd attribute is 16, it can be Null or true. Other Default values,

(3) the maximum length of the RoleName attribute in the Role table is 32. It can be Null or true.

(4) Note: When we add an object, when we select to add an object set, there will be a word "set" next to it. Remember to delete it.

(5) After designing this simple Demo, we will use the model relationship to generate a database. I will not write this step out, I believe that everyone who has a basic knowledge knows how to implement it.

2. Build architecture-design the data access layer

(1) At the database access layer (LYZJ. userLimitMVC. add UserInfoRepository (User warehouse) and RoleRepository (role warehouse) below. These functions are used to perform database operations, that is, add, delete, modify, and query. Note that there is a problem here, all our users and roles Use Database Operations (add, delete, modify, and query). What should we do? We must be clear about what to do here, when we encounter public things, we 'd better abstract a base class that implements a fixed function, and then we only inherit from it.

(2) architecture of the database access layer designed in our Demo:

3. Implement database operations (add, delete, modify, and query) for the BaseRepository class.

(1) Add reference

When we operate the base class, we need to use the DLL we just created and the DLL that uses the Entity FrameWork to operate the database. Below we add these two references in the database access layer, they are: LYZJ. userLimitMVC. model, System. data. entity.

(2) The implementation of database operations on the BaseRepository class includes addition, deletion, modification, and query by page. The Code is as follows:


1 using System. Data;
2
3 using LYZJ. UserLimitMVC. Model;
4
5 using System;
6
7 using System. Collections. Generic;
8
9 using System. Linq;
10
11 using System. Text;
12
13 using System. Threading. Tasks;
14
15
16
17 namespace LYZJ. UserLimitMVC. DAL
18
19 {
20
21 /// <summary>
22
23 // implement the base class for database operations (add, delete, modify, and query)
24
25 /// </summary>
26
27 // <typeparam name = "T"> define a generic type and constrain it as a class </typeparam>
28
29 public class BaseRepository <T> where T: class
30
31 {
32
33 // context for creating the EF framework
34
35 private DataModelContainer db = new DataModelContainer ();
36
37
38
39 // Add database and reference EF framework
40
41 public T AddEntity (T entity)
42
43 {
44
45 // Add an object in the format of EF4.0
46
47 // db. CreateObjectSet <T> (). AddObject (entity );
48
49 // EF5.0
50
51 db. Entry <T> (entity). State = EntityState. Added;
52
53
54
55 // The following statements are unified
56
57 db. SaveChanges ();
58
59 return entity;
60
61}
62
63
64
65 // modify the database
66
67 public bool UpdateEntity (T entity)
68
69 {
70
71 // EF4.0
72
73 // db. CreateObjectSet <T> (). Addach (entity );
74
75 // db. ObjectStateManager. ChangeObjectState (entity, EntityState. Modified );
76
77 // EF5.0
78
79 db. Set <T> (). Attach (entity );
80
81 db. Entry <T> (entity). State = EntityState. Modified;
82
83
84
85 return db. SaveChanges ()> 0;
86
87}
88
89
90
91 // Delete the database
92
93 public bool DeleteEntity (T entity)
94
95 {
96
97 // EF4.0
98
99 // db. CreateObjectSet <T> (). Addach (entity );
100
101 // db. ObjectStateManager. ChangeObjectState (entity, EntityState. Deleted );
102
103 // EF5.0
104
105 db. Set <T> (). Attach (entity );
106
107 db. Entry <T> (entity). State = EntityState. Deleted;
108
109
110
111 return db. SaveChanges ()> 0;
112
113}
114
115
116
117 // query the database-simple query
118
119 public IQueryable <T> LoadEntities (Func <T, bool> wherelamties)
120
121 {
122
123 // EF4.0
124
125 // return db. CreateObjectSet <T> (). Where <T> (wherelamset). AsQueryable ();
126
127 // EF5.0
128
129 return db. Set <T> (). Where <T> (whereLambda). AsQueryable ();
130
131}
132
133
134
135 /// <summary>
136
137 // implement paging query of data
138
139 /// </summary>
140
141 /// <typeparam name = "S"> sort by class </typeparam>
142
143 /// <param name = "pageIndex"> current page </param>
144
145 /// <param name = "pageSize"> How many data entries are displayed on a page </param>
146
147 /// <param name = "total"> total number of entries </param>
148
149 /// <param name = "whereLambda"> obtain the sorting condition </param>
150
151 /// <param name = "isAsc"> sort by reverse or ascending order </param>
152
153 /// <param name = "orderByLambda"> sort by that field </param>
154
155 /// <returns> </returns>
156
157 public IQueryable <T> LoadPageEntities <S> (int pageIndex, int pageSize, out int total, Func <T, bool> wherelamties,
158
159 bool isAsc, Func <T, S> orderByLambda)
160
161 {
162
163 // EF4.0 is the same as the query above
164
165 // EF5.0
166
167 var temp = db. Set <T> (). Where <T> (wherelam.pdf );
168
169 total = temp. Count (); // obtain the total number of entries.
170
171 // sort to get the data on the current page
172
173 if (isAsc)
174
175 {
176
177 temp = temp. OrderBy <T, S> (orderByLambda)
178
179. Skip <T> (pageSize * (pageIndex-1) // number of entries crossed
180
181. Take <T> (pageSize). AsQueryable (); // how many records are retrieved
182
183}
184
185 else
186
187 {
188
189 temp = temp. OrderByDescending <T, S> (orderByLambda)
190
191. Skip <T> (pageSize * (pageIndex-1) // number of entries crossed
192
193. Take <T> (pageSize). AsQueryable (); // how many records are retrieved
194
195}
196
197 return temp. AsQueryable ();
198
199}
200
201}
202
203} 4. inherit the database operation methods for users and Roles

(1) When we finish writing the base class for database operations, we need to perform database operations on users and roles. As I said above, at this time, we can use the inheritance base class to directly perform database operations.

(2) UserInfoRepository inherits the code of the base class


1 namespace LYZJ. UserLimitMVC. DAL
2
3 {
4 public class UserInfoRepository: BaseRepository <UserInfo>
5
6 {
7
8}
9
10} (3) RoleRepository inherits the code of the base class


1 namespace LYZJ. UserLimitMVC. DAL
2
3 {
4 public class RoleRepository: BaseRepository <Role>
5
6 {
7
8}
9}

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.