ASP. net mvc + EF framework + EasyUI permission management series (8)-unique within the DbSession thread

Source: Internet
Author: User

Through the previous blog, we wrote the DbSession code. DbSession is equivalent to the database access layer. As long as we want to operate the database, we can access the database from here, in addition, we can get all the objects of entities. I have already mentioned these knowledge points in my previous blog. You can go and see them. Now the series has been written to 8, during this period, I would like to thank many bloggers for their support and replies. Your comments will be the motivation for my learning. I hope you can comment on my blog. In this blog, we continue to talk about our underlying implementation. Most bloggers may be impatient. When can we see the page? It's almost time, after introducing the TT template and source code management tools, we can set the permissions. Persistence is a victory.

1. DbSession interface Constraints

(1) DbSession is the entrance to our entire database access layer. Therefore, we must be constrained by an interface, so we are at the database access interface layer (LYZJ. userLimitMVC. IDAL class library) adds an interface constraint to create an interface IDbSession. There are several methods in IDbSession, the Code is as follows:


1 namespace LYZJ. UserLimitMVC. IDAL
2
3 {
4
5 public interface IDbSession
6
7 {
8
9 // object storage objects corresponding to each table
10
11 IDAL. IRoleRepository RoleRepository {get ;}
12
13 IDAL. IUserInfoRepository UserInfoRepository {get ;}
14
15 // update the changes of all objects in the current application and database sessions to the database
16
17 int SaveChanges ();
18
19 // SQL statement execution Method
20
21 // EF4.0
22
23 // int ExcuteSql (string strSql, ObjectParameter [] parameters );
24
25 // EF5.0
26
27 int ExcuteSql (string strSql, DbParameter [] parameters );
28
29}
30
31} (2) First, the object warehouse object corresponding to each table is added,

(3) interfaces for adding the SaveChanges Method

(4) added the ExcuteSql method interface, int ExcuteSql (string strSql, ObjectParameter [] parameters), which is used to execute an SQL script, if we add an interface to the database interface layer, the DbSession of the database access layer will inherit from the IDbSession interface and then implement all the methods in the interface, the modified DbSession code is as follows:


1 namespace LYZJ. UserLimitMVC. DAL
2
3 {
4
5 // a database interaction session
6
7 public class DbSession: IDbSession // represents a session between the application and the database. It is also the unified entrance to the database access layer.
8
9 {
10
11 public IDAL. IRoleRepository RoleRepository
12
13 {
14
15 get {return new RoleRepository ();}
16
17}
18
19 public IDAL. IUserInfoRepository UserInfoRepository
20
21 {
22
23 get {return new UserInfoRepository ();}
24
25}
26
27 // representative: changes to all entities in the session between the current application and the database.
28
29 public int SaveChanges ()
30
31 {
32
33 // call the SaveChanges method of EF Context
34
35 return DAL. EFContextFactory. GetCurrentDbContext (). SaveChanges ();
36
37}
38
39 // SQL script execution Method
40
41 public int ExcuteSql (string strSql, System. Data. Common. DbParameter [] parameters)
42
43 {
44
45 // Ef4.0 execution method ObjectContext
46
47 // encapsulate the code for executing the SQL script
48
49 // return DAL. EFContextFactory. GetCurrentDbContext (). ExecuteFunction (strSql, parameters );
50
51 throw new NotImplementedException ();
52
53}
54
55}
56
57} 2. Notes on ef SQL statements

(1) If you look at the code above, you will see that the method for executing SQL statements is an empty method. What is the method for executing SQL statements in EF5.0, I only know the 4.0 method. If you know it, please leave a message. Thank you.

(2) We can also use the EF framework with ADO. they are not mutually exclusive. In some special functions of our project, we can still use ADO.. NET.

3. unique in the business logic layer DbSession thread

(1) through the previous blog, we know that we use a simple factory to implement the unique definition of BaseRepository in the database access layer. The Code is as follows:

1 // obtain the context instance in the current thread and ensure that the context in the thread is unique.
2
3 private DbContext db = EFContextFactory. getCurrentDbContext (); (2) according to the above statement, when we use the business logic layer to obtain the DbSession, we also use a simple factory to implement unique within the thread, the definition of DbSession is modified as follows:

1 // DbSession Storage
2
3 public IDbSession _ DbSession = DbSessionFactory. getCurrenntDbSession (); (3) According to the Code in (2), we can see that at this time we need to go to the database access layer, that is, (LYZJ. userLimitMVC. DAL) create a DbSessionFactory class. We use this class to implement the GetCurrentDbSession method, so that our DbSession implements the unique within the thread.

(4) The code for implementing DbSessionFactory is as follows, because it is unique in the implementation thread as I have introduced earlier, so I will not detail it here. Make a proper comment: the Code is as follows:


1 namespace LYZJ. UserLimitMVC. DAL
2
3 {
4
5 public class DbSessionFactory
6
7 {
8
9 // ensure that the DbSession instance in the thread is unique
10
11 public static IDbSession GetCurrenntDbSession ()
12
13 {
14
15 // The key of the GetData () method cannot be the same as that of the context.
16
17 IDbSession _ dbSession = CallContext. GetData ("DbSession") as IDbSession;
18
19 if (_ dbSession = null)
20
21 {
22
23 _ dbSession = new DbSession ();
24
25 // set the value to the data slot
26
27 CallContext. SetData ("DbSession", _ dbSession );
28
29}
30
31 return _ dbSession;
32
33}
34
35}
36
37} (5) When should we use a simple factory and new? That is, we need to control the life cycle of our instance. If we do not control it, we can use new directly. For example, if we want to create a table object, we can simply create a new one without creating a simple factory.

4. Summary

(1) We have finished implementing the underlying framework of this Demo. Today we write less, because only one knowledge point is implemented, that is, the unique access to the DbSession thread. Starting tomorrow, I will continue to write about how to use the T4 template and source code management tools. When I finish this introduction, I will officially start introducing the permission system. Thank you for your support.

(2) Here, the underlying things are basically these things, and the things behind them are all extended on these foundations. So now let's look at the overall system architecture:

        

 

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.