After the release of .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.
在微软发布 .NET Framework 3.5 SP1 之后,ADO.NET Framework 吸引了越来越多的开发人员和将它 用到了项目的数据访问层中。但是,当前版本的 Spring.NET 只支持原始的 ADO.NET 和 NHibernate,大 家不知道该如何将 EF 和 Spring.NET 进行集成并从 Spring.NET 的事务管理和强壮的 AOP 中获得好处 。我也遇到了相同的问题,在网上也没有找到任何结果。今天在进行研究之后,我写了一个关于这个的例 子。
The most efficient way of controlling transaction with EF is using System.Transactions.TransactionScope, so in the example we also use it.
在使用 EF 的时候,最有效的是使用 System.Transactions.TransactionScope 来控制事务,所以这 个例子也是使用的它。
First of all, I think that we need to understand the mechanism 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 illustrate the mechanism.
在开始之前,我想我们先需要理解 Spring.NET 管理事务的机制。如我们所知道的,Spring.NET 的事 物控制是基于它的 AOP 的,这意味着,事物控制代码是在运行时被动态插入那些被配置为需要事物的方 法的。下面的代码描绘了这个机制。
Our real code:
我们的代码:
[Transaction]
static void DbOperation()
{
// Our data access code
}
AOP acted on code:
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.