Added RedirectUsingNamedRoute
[castle.git] / Services / Transaction / Castle.Services.Transaction / TransactionScopeResourceAdapter.cs
bloba8920ae8b0b9e8634530de8ddee2876de5835e2c
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 #if !MONO
17 namespace Castle.Services.Transaction
19 using System.Transactions;
21 public class TransactionScopeResourceAdapter : IResource
23 private readonly TransactionMode mode;
24 private readonly IsolationMode isolationMode;
25 private TransactionScope scope;
27 /// <summary>
28 /// Initializes a new instance of the <see cref="TransactionScopeResourceAdapter"/> class.
29 /// </summary>
30 /// <param name="mode">The mode.</param>
31 /// <param name="isolationMode">The isolation mode.</param>
32 public TransactionScopeResourceAdapter(TransactionMode mode, IsolationMode isolationMode)
34 this.mode = mode;
35 this.isolationMode = isolationMode;
38 /// <summary>
39 /// Implementors should start the
40 /// transaction on the underlying resource
41 /// </summary>
42 public void Start()
44 TransactionScopeOption scopeOption = mode == TransactionMode.Requires ? TransactionScopeOption.Required : TransactionScopeOption.RequiresNew;
46 TransactionOptions options = new TransactionOptions();
48 switch(isolationMode)
50 case IsolationMode.ReadCommitted:
51 options.IsolationLevel = IsolationLevel.ReadCommitted;
52 break;
53 case IsolationMode.Chaos:
54 options.IsolationLevel = IsolationLevel.Chaos;
55 break;
56 case IsolationMode.ReadUncommitted:
57 options.IsolationLevel = IsolationLevel.ReadUncommitted;
58 break;
59 case IsolationMode.Serializable:
60 options.IsolationLevel = IsolationLevel.Serializable;
61 break;
62 case IsolationMode.Unspecified:
63 options.IsolationLevel = IsolationLevel.Unspecified;
64 break;
67 scope = new TransactionScope(scopeOption, options);
70 /// <summary>
71 /// Implementors should commit the
72 /// transaction on the underlying resource
73 /// </summary>
74 public void Commit()
76 scope.Complete();
77 scope.Dispose();
80 /// <summary>
81 /// Implementors should rollback the
82 /// transaction on the underlying resource
83 /// </summary>
84 public void Rollback()
86 scope.Dispose();
91 #endif