程序中事务控制相关2
前一篇文章提到希望把事务从业务逻辑里面抽取出来,而且事务控制也是符合面向方面的规则的,通过artech的文章,我们可以很好的理解其中的原理,但是artech的事务辅助类还是需要参杂到业务里面,这里借助aop可以很方便的把事务的控制挪到业务的外围,实际上spring.net里面就是这样做的,spring.net在ado的事务控制方面借助的就是TransactionInterceptor这个方法拦截器,里面的代码大概如下
public object Invoke(IMethodInvocation invocation){ // Work out the target class: may be <code>null</code>. // The TransactionAttributeSource should be passed the target class // as well as the method, which may be from an interface.Type targetType = ( invocation.This != null ) ? invocation.This.GetType() : null; // If the transaction attribute is null, the method is non-transactional.TransactionInfo txnInfo = CreateTransactionIfNecessary( invocation.Method, targetType );object returnValue = null;try{ // This is an around advice. // Invoke the next interceptor in the chain. // This will normally result in a target object being invoked.returnValue = invocation.Proceed();} catch ( Exception ex ){ // target invocation exceptionCompleteTransactionAfterThrowing( txnInfo, ex ); throw;} finally{CleanupTransactionInfo( txnInfo );}CommitTransactionAfterReturning( txnInfo );return returnValue;}?