Removed untyped contructor from ComponentRegistration and add a protected setter.
[castle.git] / ActiveRecord / Castle.ActiveRecord / Framework / Scopes / SessionScope.cs
blob364171e7c9b0e89359d9d6d006da4688de3b7487
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 Castle.ActiveRecord
17 using System;
18 using System.Collections.Generic;
19 using Castle.ActiveRecord.Framework.Scopes;
20 using NHibernate;
22 /// <summary>
23 /// Implementation of <see cref="ISessionScope"/> to
24 /// augment performance by caching the session, thus
25 /// avoiding too much opens/flushes/closes.
26 /// </summary>
27 public class SessionScope : AbstractScope
29 /// <summary>
30 /// Is set to true if the session went stalled due to an error (usually db operations)
31 /// </summary>
32 private bool hasSessionError;
34 /// <summary>
35 /// Initializes a new instance of the <see cref="SessionScope"/> class.
36 /// </summary>
37 /// <param name="flushAction">The flush action.</param>
38 /// <param name="type">The type.</param>
39 protected SessionScope(FlushAction flushAction, SessionScopeType type) : base(flushAction, type)
43 /// <summary>
44 /// Initializes a new instance of the <see cref="SessionScope"/> class.
45 /// </summary>
46 public SessionScope() : this(FlushAction.Auto)
50 /// <summary>
51 /// Initializes a new instance of the <see cref="SessionScope"/> class.
52 /// </summary>
53 /// <param name="flushAction">The flush action.</param>
54 public SessionScope(FlushAction flushAction) : base(flushAction, SessionScopeType.Simple)
58 /// <summary>
59 /// Deprecated! Disposes the specified discard changes. Please use new SessionScope(FlushAction.Never)
60 /// </summary>
61 /// <param name="discardChanges">if set to <c>true</c> [discard changes].</param>
62 [Obsolete("This useage is deprecated - please use new SessionScope(FlushAction.Never)")]
63 public void Dispose(bool discardChanges)
65 ThreadScopeAccessor.Instance.UnRegisterScope(this);
67 PerformDisposal(key2Session.Values, !discardChanges, true);
69 key2Session.Clear();
70 key2Session = null;
73 /// <summary>
74 /// Performs the disposal.
75 /// </summary>
76 /// <param name="sessions">The sessions.</param>
77 protected override void PerformDisposal(ICollection<ISession> sessions)
79 if (hasSessionError || FlushAction == FlushAction.Never)
81 PerformDisposal(sessions, false, true);
83 else if (FlushAction == FlushAction.Auto)
85 PerformDisposal(sessions, true, true);
89 /// <summary>
90 /// This is called when an action on a session fails
91 /// </summary>
92 /// <param name="session">The session</param>
93 public override void FailSession(ISession session)
95 hasSessionError = true;
98 /// <summary>
99 /// Gets or sets a flag indicating whether this instance has session error.
100 /// </summary>
101 /// <value>
102 /// <c>true</c> if this instance has session error; otherwise, <c>false</c>.
103 /// </value>
104 public bool HasSessionError
106 get { return hasSessionError; }
107 set { hasSessionError = true; }
110 /// <summary>
111 /// Gets the current scope
112 /// </summary>
113 /// <value>The current.</value>
114 public static ISessionScope Current
118 if (ThreadScopeAccessor.Instance.HasInitializedScope)
120 return ThreadScopeAccessor.Instance.GetRegisteredScope();
123 return null;