After the release of the. NET Framework 3.5 SP1 from Microsoft, more and more developers interested in Ado.net Entity K and hope to use the IT in their applications ' data access layer. But the current version of Spring.net data access I 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 "the", I wrote an example Describes this.
After Microsoft releases the. NET Framework 3.5 SP1, the Ado.net framework attracts more and more developers and uses it in the project's data access layer. However, the current version of Spring.net only supports the original ado.net and NHibernate, and you don't know how to integrate EF and spring.net and derive benefits from Spring.net transaction management and strong AOP. I also encountered the same problem, and did not find any results on the Internet. Today, after the study, I wrote an example of this.
The most efficient way of controlling transaction with EF are using System.Transactions.TransactionScope, so in the example We also use it.
When using EF, it is most effective to use System.Transactions.TransactionScope to control the transaction, so this example is also used.
The "All", I "I", "We need to understand" mechanism of how Spring.net manages transaction. As we known, spring.net transaction control are base on it AOP, which means the transaction control codes are Injected into methods configured with transaction need at runtime. The following codes illustrate the mechanism.
Before I begin, I think we need to understand the mechanism of spring.net management services first. As we know, Spring.net's control of things is based on its AOP, which means that the control code is dynamically inserted into methods that are configured to require things at runtime. The following code depicts this mechanism.
Our Real code:
Our code:
[Transaction]
static void DbOperation()
{
// Our data access code
}
AOP acted on code:
The code after the AOP action:
[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 so all we only need to do are using AOP ability from spring.ne T to inject transaction code arround ourself code. 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.