1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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
22 /// Define session scope types
24 public enum SessionScopeType
27 /// Undefined type of session scope.
28 /// This value probably should never exist
32 /// Simple - non transactional session scope
36 /// Transactional session scope
40 /// Custom implementation of session scope.
46 /// Contract for implementation of scopes.
49 /// A scope can implement a logic that affects
50 /// AR for the scope lifetime. Session cache and
51 /// transaction are the best examples, but you
52 /// can create new scopes adding new semantics.
54 /// The methods on this interface are mostly invoked
55 /// by the <see cref="Castle.ActiveRecord.Framework.ISessionFactoryHolder"/>
59 public interface ISessionScope
: IDisposable
62 /// Returns the <see cref="FlushAction"/> defined
65 FlushAction FlushAction { get; }
68 /// Returns the <see cref="SessionScopeType"/> defined
71 SessionScopeType ScopeType { get; }
74 /// Flushes the sessions that this scope
80 /// Evicts the specified instance from the session cache.
82 /// <param name="instance">The instance.</param>
83 void Evict(object instance
);
86 /// This method is invoked when no session was available
87 /// at and the <see cref="Castle.ActiveRecord.Framework.ISessionFactoryHolder"/>
88 /// just created one. So it registers the session created
89 /// within this scope using a key. The scope implementation
90 /// shouldn't make any assumption on what the key
91 /// actually is as we reserve the right to change it
92 /// <seealso cref="IsKeyKnown"/>
94 /// <param name="key">an object instance</param>
95 /// <param name="session">An instance of <c>ISession</c></param>
96 void RegisterSession(object key
, ISession session
);
99 /// This method is invoked when the
100 /// <see cref="Castle.ActiveRecord.Framework.ISessionFactoryHolder"/>
101 /// instance needs a session instance. Instead of creating one it interrogates
102 /// the active scope for one. The scope implementation must check if it
103 /// has a session registered for the given key.
104 /// <seealso cref="RegisterSession"/>
106 /// <param name="key">an object instance</param>
107 /// <returns><c>true</c> if the key exists within this scope instance</returns>
108 bool IsKeyKnown(object key
);
111 /// This method should return the session instance associated with the key.
113 /// <param name="key">an object instance</param>
114 /// <returns>the session instance or null if none was found</returns>
115 ISession
GetSession(object key
);
118 /// Implementors should return true if they
119 /// want that their scope implementation
120 /// be in charge of creating the session
122 bool WantsToCreateTheSession { get; }
125 /// If the <see cref="WantsToCreateTheSession"/> returned
126 /// <c>true</c> then this method is invoked to allow
127 /// the scope to create a properly configured session
129 /// <param name="sessionFactory">From where to open the session</param>
130 /// <param name="interceptor">the NHibernate interceptor</param>
131 /// <returns>the newly created session</returns>
132 ISession
OpenSession(ISessionFactory sessionFactory
, IInterceptor interceptor
);
135 /// This method will be called if a session action fails.
136 /// The scope may then decide to use an different approach to flush/dispose it.
138 /// <param name="session">The session that failed</param>
139 void FailSession(ISession session
);