1. Unique review in the EFContextFactory thread
(1) Here we will explain that the thread is unique. In the instructor's statement, we assume that this thread is a dress, and each dress has a pocket on it, then we can regard this pocket as a data slot (the memory space that CallContext points to), and each dress will have its own pocket (except for special ones ), at this time, you need to take out a sugar from the clothes pocket. At this time, I need to go to the pocket and check whether there is any sugar. If there is a sugar in the clothes pocket, we will take it out, in charge, I put the sugar in the clothes pocket first, and you will continue to judge whether there is any sugar next time. In addition, each dress has its own pocket, which is unrelated to each other, ensuring that each dress contains sugar (unique in the thread ).
(2) CallContext can help us ensure the uniqueness in the thread.
2. DbSession
(1) The DbSession encapsulates all the attributes of our warehousing. In the attributes, we can get the instances of our warehousing. Then DbSession can be seen as a unified portal for the entire database access layer, in addition, we have encapsulated a SaveChanges method in DbSession. How can we do this in SaveChanges? See the following code:
Namespace LYZJ. UserLimitMVC. DAL
{
// A session that interacts with the database
Public class DbSession // indicates a session between the application and the database, which is also the unified entrance to the database access layer.
{
Public IDAL. IRoleRepository RoleRepository
{
Get {return new RoleRepository ();}
}
Public IDAL. IUserInfoRepository UserInfoRepository
{
Get {return new UserInfoRepository ();}
}
// Represents the changes of all entities in the painting of the current application and database. The updated database
Public int SaveChanges ()
{
// Call the SaveChanges method of the EF Context
Return DAL. EFContextFactory. GetCurrentDbContext (). SaveChanges ();
}
}
} (2) If we encapsulate a SaveChanges method, it will directly obtain the context in the current thread and then call the SaveChanges method of the context, it is equivalent to directly submitting changes to all entities in the current thread to the database. Most people may have no idea about the above code. What is this? At this time, I am working on adding the database of the BaseRepository (warehouse) deletion method. when SaveChanges () is deleted, we will find the benefit. That is, although the called methods in our database access layer are not actually saved to the database, that is to say, we put all the SaveChanges into the DbSession for implementation. Then we can regard the DbSession as a real session.
(3) That is to say, after we call the added, deleted, and modified entities (operate on many tables) many times, we only need to call a SaveChanges method in DbSession, all changes to table entities can be placed in the database.
3. Benefits of putting the SaveChanges method into DbSession
(1) So what are the benefits of putting the SaveChanges method into DbSession? Let's take another example in the project as an example: Let's go back to BaseService to add a warehouse. Suppose we add an object here, and then we create an object for adding a user, then there is another status change in our business, so our code is as follows:
1 // Add a database
2
3 public T AddEntity (T entity)
4
5 {
6
7 // call the warehouse corresponding to T for adding
8
9 CurrentRepository. AddEntity (entity );
10
11 CurrentRepository. UpdateEntity (entity );
12
13} (2) Here, we may operate many tables in one business scenario. In the previous practice, all the addition, deletion, and modification methods will execute SaveChanges once each time they are called, for example, to add a user, when we add a user, we need to SaveChangers once and then pay the permission once. In this way, we have interacted with the database many times, the SaveChangers method is extracted to DbSession.
(3) If we extract SaveChangers to the DbSession, we will call the DbSession implementation directly after operating multiple entities, at this time, the method added above can be implemented as follows:
1 // DbSession Storage
2
3 public DbSession _ DbSession = new DbSession ();
4
5 // base class Constructor
6
7 public BaseService ()
8
9 {
10
11 SetCurrentRepository (); // called in the constructor. This sets the abstract method of the current warehouse.
12
13}
14
15 public abstract void SetCurrentRepository (); // The subclass must be implemented.
16
17 // Add a database
18
19 public T AddEntity (T entity)
20
21 {
22
23 // call the warehouse corresponding to T for adding
24
25 CurrentRepository. AddEntity (entity );
26
27 CurrentRepository. UpdateEntity (entity );
28
29 _ DbSession. SaveChanges ();
30
31} (4) in this case, we encapsulate the DbSession so that the DbSession is very flexible, that is, the SaveChangers right is upgraded from the database access layer to the business logic layer, the business logic layer is used to control the SaveChangers method, while the database access layer does not need the SaveChangers method. Therefore, you can ensure that only one commit is required to operate multiple tables in one business scenario, reduces the number of interactions with the database.
4. Modify BaseService
(1) through the above introduction, we will modify BaseService. The final code for the modification is as follows:
1 namespace LYZJ. UserLimitMVC. BLL
2
3 {
4
5 public abstract class BaseService <T> where T: class, new ()
6
7 {
8
9 // current storage
10
11 public IDAL. IBaseRepository <T> CurrentRepository {get; set ;}
12
13 // DbSession Storage
14
15 public DbSession _ DbSession = new DbSession ();
16
17 // base class Constructor
18
19 public BaseService ()
20
21 {
22
23 SetCurrentRepository (); // called in the constructor. This sets the abstract method of the current warehouse.
24
25}
26
27 public abstract void SetCurrentRepository (); // The subclass must be implemented.
28
29 // Add a database
30
31 public T AddEntity (T entity)
32
33 {
34
35 // call the warehouse corresponding to T for adding
36
37 var AddEntity = CurrentRepository. AddEntity (entity );
38
39 _ DbSession. SaveChanges ();
40
41 return AddEntity;
42
43}
44 // implement the data modification function
45
46 public bool UpdateEntity (T entity)
47
48 {
49
50 CurrentRepository. UpdateEntity (entity );
51
52 return _ DbSession. SaveChanges ()> 0;
53
54}
55 // Delete the database
56
57 public bool DeleteEntity (T entity)
58
59 {
60
61 CurrentRepository. DeleteEntity (entity );
62
63 return _ DbSession. SaveChanges ()> 0;
64
65}
66 // query the database-simple query
67
68 public IQueryable <T> LoadEntities (Func <T, bool> wherelamties)
69
70 {
71
72 return CurrentRepository. LoadEntities (wherelamties );
73
74}
75
76 // <summary>
77
78 // implement paging query of data
79
80 /// </summary>
81
82 // <typeparam name = "S"> sort by class </typeparam>
83
84 /// <param name = "pageIndex"> current page </param>
85
86 // <param name = "pageSize"> how many pieces of data are displayed on a page </param>
87
88 // <param name = "total"> total number of entries </param>
89
90 // <param name = "whereLambda"> obtain the sorting condition </param>
91
92 // <param name = "isAsc"> sort by reverse or ascending order </param>
93
94 // <param name = "orderByLambda"> sort by that field </param>
95
96 /// <returns> </returns>
97
98 public IQueryable <T> LoadPageEntities <S> (int pageIndex, int pageSize, out int total, Func <T, bool> wherelamties,
99
100 bool isAsc, Func <T, S> orderByLambda)
101
102 {
103
104 return CurrentRepository. LoadPageEntities (pageIndex, pageSize, out total, wherelamties, isAsc, orderByLambda );
105
106}
107
108}
109
110} (2) Although DbSession encapsulation is simple, it takes into account the simple factory mode and the SaveChangers method (an important feature of the current session). Although the SaveChangers method is simple with several lines of code, but here we implement a mode, that is, the unit work mode (UintWork ).
(3) The unit work mode refers to submitting database operations in batches to the database, or encapsulating a series of database operations into a unit work, all changes in the unit work are submitted to the database at one time. This is the unit work mode, which aims to improve the efficiency of interaction with the database, reduce the number of interactions with the database.
5. Summary
(1) At the end of this blog, I want to give some bloggers a message to me in front of me. First, do I write permission management? My answer must be yes, but you haven't seen it before, because I am still writing the underlying information, because when we write the underlying information, is the interface and business logic worse.
(2) my current project is a bit complicated. Many people say there is no practical value. What I want to talk about here is also, at the beginning of this blog, I spoke about this series of blogs, not how to do projects, but ideas. I believe that as long as we understand the object-oriented thinking, many problems can be solved.
(3) Finally, I would like to thank my blogs for reading my blog. Thank you for your support. Your support is my motivation.