Original: ASP. NET MVC+EF Framework +easyui Implementation Rights Management Series (8)-dbsession thread within a unique
ASP. NET Mvc+ef Framework +easyui implement permission tube series
(opening) (1): Frame construction     (2): Database access layer Design demo   (3): interface-oriented programming (4): Encapsulation of the business Logic layer
(5): The front desk jquery Easyui implementation (6): EF Context Instance Management (7) package of:D bsession
Preface: Through the previous blog we completed the Dbsession code, Dbsession is equivalent to the database access layer of the portal, as long as we want to operate the database we can go from here to the database, and can take all the objects of the entity, these points of knowledge I in the previous blog basically has been said , Bo friends can go to see, now the series has been written to 8, this period thanks to the broad masses of bloggers support and reply, your comments will be my motivation to study, I hope you can comment on my blog. This blog we continue to say that the implementation of our bottom, probably most bo friends are a bit impatient, when can see the page, now fast, when I introduced the TT template and source code management tools we can enter the permissions of the settings, adhere to the victory.
1. Constraints on the Dbsession interface
(1) Dbsession is the entrance to our entire database access layer, then we dbsession must also be constrained by an interface, then we are at the Database Access interface layer (LYZJ. Userlimitmvc.idal class Library) add an interface constraint and create a new interface idbsession. There are several methods in Idbsession, the code is as follows:
1 namespaceLyzj. Userlimitmvc.idal2 3 {4 5 Public Interfaceidbsession6 7 {8 9 //entity warehousing objects for each tableTen OneIdal. Irolerepository Rolerepository {Get; } A -Idal. Iuserinforepository Userinforepository {Get; } - the //updates the current application with changes to all entities in the database's session database - - intSaveChanges (); - + //methods for executing SQL statements - + //the writing of EF4.0 A at //int Excutesql (string strSQL, objectparameter[] parameters); - - //the writing of EF5.0 - - intExcutesql (stringstrSQL, dbparameter[] parameters); - in } - to}
(2) The first thing to add is the entity storage object corresponding to each table,
(3) Adding an interface to the SaveChanges method
(4) Add the interface of the Excutesql method, int excutesql (string strsql,objectparameter[] parameters), its function is to execute SQL script, then if we add interface to the database interface layer, The dbsession of our database access layer will inherit from the Idbsession interface, and then implement all the methods in the interface, then the code in the modified Dbsession is as follows:
1 namespaceLyzj. Userlimitmvc.dal2 3 {4 5 //a session that interacts with the database at a time6 7 Public classDbsession:idbsession//represents a single session between an application and a database, and a unified portal to the database access layer8 9 {Ten One PublicIdal. Irolerepository rolerepository A - { - the Get{return Newrolerepository ();} - - } - + PublicIdal. Iuserinforepository userinforepository - + { A at Get{return Newuserinforepository ();} - - } - - //represents: Changes to the current application and all entities within the database's session, the update database - in Public intSaveChanges () - to { + - //invoke the SaveChanges method of the EF context the * returnDAL. Efcontextfactory.getcurrentdbcontext (). SaveChanges (); $ Panax Notoginseng } - the //methods for executing SQL scripts + A Public intExcutesql (stringstrSQL, system.data.common.dbparameter[] parameters) the + { - $ //Ef4.0 method of execution ObjectContext $ - //encapsulates a code that executes a SQL script - the //return DAL. Efcontextfactory.getcurrentdbcontext (). ExecuteFunction (strSQL, parameters); - Wuyi Throw Newnotimplementedexception (); the - } Wu - } About $}
2. A small note on the EF operation SQL statement
(1) here, if you look at my above code, you will see the way to execute the SQL statement is an empty method, I would like to ask you in EF5.0 inside the method of executing SQL statements, I only know the method of 4.0, if you know the words please give me a message, thank you.
(2) We can also use the EF framework to work with the ADO, they are not mutually exclusive, our project in some special functions can also be used in the implementation of ADO.
3. Business Logic layer dbsession unique within thread
(1) Through the previous blog we know that we have the database access layer Baserepository (warehousing) using a simple factory to implement the thread within the unique definition, the code is as follows:
1 // Gets the context instance inside the current thread, and guarantees that the context within the thread is unique 2 3 Private DbContext db = Efcontextfactory.getcurrentdbcontext ();
(2) According to the above statement, then our business logic layer to get Dbsession is also used simple factory to implement the thread within the unique, then we modify the definition of dbsession as follows:
1 // Storage of Dbsession 2 3 Public Idbsession _dbsession = Dbsessionfactory.getcurrenntdbsession ();
(3) According to the Code inside (2) we can see that at this time we need the database access layer, that is (LYZJ. Userlimitmvc.dal) Create a dbsessionfactory class below, we use this class to implement the Getcurrentdbsession method, so that our dbsession also implements the thread within the unique.
(4) Then finally we implement the Dbsessionfactory code as shown below, because in the previous I have introduced the implementation thread within the unique, so here is not detailed introduction, appropriate comment: The code is as follows:
1 namespaceLyzj. Userlimitmvc.dal2 3 {4 5 Public classdbsessionfactory6 7 {8 9 //Guaranteed Line Range Dbsession Instance uniqueTen One Public Staticidbsession getcurrenntdbsession () A - { - the //The key of the GetData () method here cannot be the same as the context - -Idbsession _dbsession = CallContext.GetData ("dbsession") asidbsession; - + if(_dbsession = =NULL) - + { A at_dbsession =Newdbsession (); - - //set the value to the data slot. - -Callcontext.setdata ("dbsession", _dbsession); - in } - to return_dbsession; + - } the * } $ Panax Notoginseng}
(5) What to note here is when we use the simple factory, when to use new? That is the life cycle of our instance, we have to control him, and if we do not control then we can use new directly. For example, our table entities if we want to create, then we can directly new one, and do not need to create a simple factory.
4. Summary
(1) The bottom frame of this demo is finished here, it is less written today because only a knowledge point is implemented, which is the only access to the thread within the dbsession. Starting tomorrow I will continue to write how to use the T4 template and source control tools, when the introduction of these, I will formally begin to introduce the authority system. Thank you for all your support.
(2) Here because the underlying things are basically these things, the back of the things are on these bases to expand, so now look at the entire system architecture:
Kencery back to the beginning of this series
ASP. NET Mvc+ef Framework +easyui Implementation Rights Management Series (8)-dbsession thread unique