Fixing an issue with output parameters that are of type IntPtr
[castle.git] / Samples / InversionOfControl / AnotherExtendingSample / TransactionInterceptor.cs
blob5db41c24c7b5793f58082a0315aea401b959a0bc
1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Extending2
17 using System;
19 using Castle.Core.Interceptor;
21 /// <summary>
22 /// Summary description for TransactionInterceptor.
23 /// </summary>
24 public class TransactionInterceptor : IMethodInterceptor
26 private ITransactionManager _transactionManager;
27 private TransactionConfigHolder _transactionConfHolder;
29 public TransactionInterceptor(ITransactionManager transactionManager,
30 TransactionConfigHolder transactionConfHolder)
32 _transactionManager = transactionManager;
33 _transactionConfHolder = transactionConfHolder;
36 public object Intercept(IMethodInvocation invocation, params object[] args)
38 if (_transactionManager.CurrentTransaction != null)
40 // No support for nested transactions
41 // is necessary
42 return invocation.Proceed(args);
45 TransactionConfig config =
46 _transactionConfHolder.GetConfig(
47 invocation.Method.DeclaringType );
49 if (config != null && config.IsMethodTransactional( invocation.Method ))
51 ITransaction transaction =
52 _transactionManager.CreateTransaction();
54 object value = null;
56 try
58 value = invocation.Proceed(args);
60 transaction.Commit();
62 catch(Exception ex)
64 transaction.Rollback();
66 throw ex;
68 finally
70 _transactionManager.Release(transaction);
73 return value;
75 else
77 return invocation.Proceed(args);