after the release. net Framework 3.5 SP1 from Microsoft, more and more developers interested in ADO. NET Entity Framework and hope to use it in their applications 'data access layer. but the current version of spring. NET data access part only supports primitive ADO. net and nhibenate, they don't know how to integrate EF into spring. net and get benefits from spring. net Transaction Management and Strength AOP ability. because I also encountered same problem and did not get result from Internet, today after the research, I wrote an example describes this.
released at Microsoft. net Framework 3.5 SP1, ADO. net Framework attracts more and more developers and uses it to the data access layer of the project. However, the current version of spring. net only supports the original ADO. net and nhib.pdf, you do not know how to change EF and spring. net integration and from spring. net transaction management and strong AOP. I also encountered the same problem and did not find any results online. After my research today, I wrote an example about this.
The most efficient way of controlling transaction with EF is using system. Transactions. transactionscope, so in the example we also use it.
When using EF, the most effective method is to use system. Transactions. transactionscope to control transactions, so this example also uses it.
First of all, I think that we need to understand the mechanic of how spring. net manages transaction. as we known, spring. net transaction control is base on its AOP, which means the transaction control codes are dynamically injected into methods configured with transaction need at runtime. the following Codes extends strate the mechanic.
Before starting, I think we need to understand the mechanism for managing transactions in spring. net. As we know, spring. Net's thing control is based on its AOP, which means that thing controlCodeThe Methods configured as required are dynamically inserted during runtime. The following code depicts this mechanism.
Our real code:
Our code:
[Transaction]
Static Void Dboperation ()
{
// Our data access code
}
AOP acted on code:
Code after AOP:
[Transaction]
Static Void Dboperation ()
{
Transactionscope TS = New Transactionscope ();
Try
{
// Our data access code
TS. Complete ();
}
Finally
{
TS. Dispose ();
}
}
After the understanding base on the previous code, we know that all we only need to do is using AOP ability from spring. net to inject transaction code arround ourself code of method. because EF Code needs a system. data. objects. objectcontext object to interact with underlying database, so we can provide a base class for our data access classes.
After understanding the above Code, we know that all we need to do is to use spring. net's AOP capability will insert the transaction code and use the transaction code to "Wrap" the code of our own method. Because EF Code requires a system. Data. Objects. objectcontext object to interact with the underlying database, we need to provide a base class for our data category class.
Final code:
The final code:
Public Class Efsupport < Tobjectcontext > : Daosupport
Where Tobjectcontext: objectcontext
{
Public Tobjectcontext database { Get ; Set ;}
Protected Override Void Checkdaoconfig ()
{
If ( This . Database = Null ) Throw New Argumentexception ( " Database cannot be null. " );
}
}
Public InterfaceIorderdao
{
VoidAddorder (order );
VoidAddorderitems (ilist<Orderitem>Orderitems );
}
Public Class Orderdao: efsupport < Demoentities > , Iorderdao
{
[Transaction]
Public Void Addorder (order)
{
This . Database. addtoorderset (order );
This . Database. savechanges ();
}
[Transaction]
Public Void Addorderitems (ilist < Orderitem > Orderitems)
{
Foreach (VAR item In Orderitems)
{
This . Database. addtoorderitemset (item );
}
This . Database. savechanges ();
}
}
< Objects Xmlns = "Http://www.springframework.net" >
< Object ID = "Eftxsampleentities" Type = "Demo. eftxsample. eftxsampleentities" >
< Constructor-Arg Name = "Connectionstring" Value = "Metadata = Res: // *; provider = system. data. sqlclient; provider connection string = & quot; Data Source = HOME-WZH-02; initial catalog = demo; Integrated Security = true; multipleactiveresultsets = true & quot ;" />
</ Object >
< Object ID = "Orderdao" Type = "Demo. eftxsample. orderdao" >
< Property Name = "Database" Ref = "Eftxsampleentities" />
</ Object >
< Object ID = "Transactionmanager"
Type = "Spring. Data. Core. txscopetransactionmanager, spring. Data" >
</ Object >
< Object ID = "Autoproxycreator"
Type = "Spring. AOP. Framework. autoproxy. defaultadvisorautoproxycreator, spring. AOP" />
< Object ID = "Transactionadvisor"
Type = "Spring. transaction. Interceptor. transactionattributesourceadvisor, spring. Data" >
< Property Name = "Transactioninterceptor" Ref = "Transactioninterceptor" />
</ Object >
< Object ID = "Transactioninterceptor"
Type = "Spring. transaction. Interceptor. transactioninterceptor, spring. Data" >
< Property Name = "Transactionmanager" Ref = "Transactionmanager" />
< Property Name = "Transactionattributesource" Ref = "Attributetransactionattributesource" />
</ Object >
< Object ID = "Attributetransactionattributesource"
Type = "Spring. transaction. Interceptor. attributestransactionattributesource, spring. Data" />
</ Objects >
The attachment is a complete example.
The attachment provides a complete example.
Download example code)