1 // Copyright 2004-2007 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
.Facilities
.ActiveRecordIntegration
18 using System
.Collections
;
22 using NHibernate
.Type
;
24 using Castle
.ActiveRecord
.Framework
;
27 /// This class implements <see cref="ISession"/>
28 /// and delegates <see cref="Close"/> and
29 /// <see cref="Dispose"/> to <see cref="ISessionFactoryHolder.ReleaseSession"/>
30 /// as the session is in fact managed by ActiveRecord framework
32 public class SafeSessionProxy
: ISession
, IDisposable
34 private readonly ISession innerSession
;
35 private readonly ISessionFactoryHolder holder
;
37 private bool wasClosed
;
39 public SafeSessionProxy(ISessionFactoryHolder holder
, ISession innerSession
)
41 if (innerSession
== null) throw new ArgumentNullException("innerSession");
43 this.innerSession
= innerSession
;
47 public FlushMode FlushMode
49 get { return innerSession.FlushMode; }
50 set { innerSession.FlushMode = value; }
53 public ISessionFactory SessionFactory
55 get { return innerSession.SessionFactory; }
58 public IDbConnection Connection
60 get { return innerSession.Connection; }
65 get { return innerSession.IsOpen; }
68 public bool IsConnected
70 get { return innerSession.IsConnected; }
73 public ITransaction Transaction
75 get { return innerSession.Transaction; }
83 public IDbConnection
Disconnect()
85 return innerSession
.Disconnect();
88 public void Reconnect()
90 innerSession
.Reconnect();
93 public void Reconnect(IDbConnection connection
)
95 innerSession
.Reconnect(connection
);
98 public IDbConnection
Close()
103 holder
.ReleaseSession( innerSession
);
108 throw new InvalidOperationException("Session was closed");
112 public void CancelQuery()
114 innerSession
.CancelQuery();
117 public bool IsDirty()
119 return innerSession
.IsDirty();
122 public object GetIdentifier(object obj
)
124 return innerSession
.GetIdentifier(obj
);
127 public bool Contains(object obj
)
129 return innerSession
.Contains(obj
);
132 public void Evict(object obj
)
134 innerSession
.Evict(obj
);
137 public object Load(Type theType
, object id
, LockMode lockMode
)
139 return innerSession
.Load(theType
, id
, lockMode
);
142 public object Load(Type theType
, object id
)
144 return innerSession
.Load(theType
, id
);
147 public T Load
<T
>(object id
, LockMode lockMode
)
149 return innerSession
.Load
<T
>(id
, lockMode
);
152 public T Load
<T
>(object id
)
154 return innerSession
.Load
<T
>(id
);
157 public void Load(object obj
, object id
)
159 innerSession
.Load(obj
, id
);
162 public object Get(Type clazz
, object id
)
164 return innerSession
.Get(clazz
, id
);
167 public object Get(Type clazz
, object id
, LockMode lockMode
)
169 return innerSession
.Get(clazz
, id
, lockMode
);
172 public T Get
<T
>(object id
)
174 return innerSession
.Get
<T
>(id
);
177 public T Get
<T
>(object id
, LockMode lockMode
)
179 return innerSession
.Get
<T
>(id
, lockMode
);
182 public IFilter
EnableFilter(string filterName
)
184 return innerSession
.EnableFilter(filterName
);
187 public IFilter
GetEnabledFilter(string filterName
)
189 return innerSession
.GetEnabledFilter(filterName
);
192 public void DisableFilter(string filterName
)
194 innerSession
.DisableFilter(filterName
);
197 public IMultiQuery
CreateMultiQuery()
199 return innerSession
.CreateMultiQuery();
202 public void Replicate(object obj
, ReplicationMode replicationMode
)
204 innerSession
.Replicate(obj
, replicationMode
);
207 public object Save(object obj
)
209 return innerSession
.Save(obj
);
212 public void Save(object obj
, object id
)
214 innerSession
.Save(obj
, id
);
217 public void SaveOrUpdate(object obj
)
219 innerSession
.SaveOrUpdate(obj
);
222 public void Update(object obj
)
224 innerSession
.Update(obj
);
227 public void Update(object obj
, object id
)
229 innerSession
.Update(obj
, id
);
232 public object SaveOrUpdateCopy(object obj
)
234 return innerSession
.SaveOrUpdateCopy(obj
);
237 public object SaveOrUpdateCopy(object obj
, object id
)
239 return innerSession
.SaveOrUpdateCopy(obj
, id
);
242 public void Delete(object obj
)
244 innerSession
.Delete(obj
);
247 public IList
Find(String query
)
249 return innerSession
.CreateQuery(query
).List();
252 public IList
Find(String query
, object value, IType type
)
254 // TODO: This is deprecated. Use ISession.CreateQuery().SetXYZ().List()
255 return innerSession
.Find(query
, value, type
);
258 public IList
Find(String query
, object[] values
, IType
[] types
)
260 // TODO: This is deprecated. Use ISession.CreateQuery().SetXYZ().List()
261 return innerSession
.Find(query
, values
, types
);
264 public IEnumerable
Enumerable(String query
)
266 // TODO: This is deprecated. Use ISession.CreateQuery().SetXYZ().List()
267 return innerSession
.Enumerable(query
);
270 public IEnumerable
Enumerable(String query
, object value, IType type
)
272 // TODO: This is deprecated. Use ISession.CreateQuery().SetXYZ().List()
273 return innerSession
.Enumerable(query
, value, type
);
276 public IEnumerable
Enumerable(String query
, object[] values
, IType
[] types
)
278 // TODO: This is deprecated. Use ISession.CreateQuery().SetXYZ().List()
279 return innerSession
.Enumerable(query
, values
, types
);
282 public ICollection
Filter(object collection
, String filter
)
284 // TODO: This is deprecated. Use ISession.CreateQuery().SetXYZ().List()
285 return innerSession
.Filter(collection
, filter
);
288 public ICollection
Filter(object collection
, String filter
, object value, IType type
)
290 // TODO: This is deprecated. Use ISession.CreateQuery().SetXYZ().List()
291 return innerSession
.Filter(collection
, filter
, value, type
);
294 public ICollection
Filter(object collection
, String filter
, object[] values
, IType
[] types
)
296 // TODO: This is deprecated. Use ISession.CreateQuery().SetXYZ().List()
297 return innerSession
.Filter(collection
, filter
, values
, types
);
300 public int Delete(String query
)
302 return innerSession
.Delete(query
);
305 public int Delete(String query
, object value, IType type
)
307 return innerSession
.Delete(query
, value, type
);
310 public int Delete(String query
, object[] values
, IType
[] types
)
312 return innerSession
.Delete(query
, values
, types
);
315 public void Lock(object obj
, LockMode lockMode
)
317 innerSession
.Lock(obj
, lockMode
);
320 public void Refresh(object obj
)
322 innerSession
.Refresh(obj
);
325 public void Refresh(object obj
, LockMode lockMode
)
327 innerSession
.Refresh(obj
,lockMode
);
330 public LockMode
GetCurrentLockMode(object obj
)
332 return innerSession
.GetCurrentLockMode(obj
);
335 public ITransaction
BeginTransaction()
337 return innerSession
.BeginTransaction();
340 public ITransaction
BeginTransaction(IsolationLevel isolationLevel
)
342 return innerSession
.BeginTransaction(isolationLevel
);
345 public ICriteria
CreateCriteria(Type persistentClass
)
347 return innerSession
.CreateCriteria(persistentClass
);
350 public ICriteria
CreateCriteria(Type persistentClass
, string alias)
352 return innerSession
.CreateCriteria(persistentClass
, alias);
355 public IQuery
CreateQuery(String queryString
)
357 return innerSession
.CreateQuery(queryString
);
360 public IQuery
CreateFilter(object collection
, String queryString
)
362 return innerSession
.CreateFilter(collection
, queryString
);
365 public IQuery
GetNamedQuery(String queryName
)
367 return innerSession
.GetNamedQuery(queryName
);
370 public ISQLQuery
CreateSQLQuery(string queryString
)
372 return innerSession
.CreateSQLQuery(queryString
);
375 public IQuery
CreateSQLQuery(String sql
, String returnAlias
, Type returnClass
)
377 return innerSession
.CreateSQLQuery(sql
, returnAlias
, returnClass
);
380 public IQuery
CreateSQLQuery(String sql
, String
[] returnAliases
, Type
[] returnClasses
)
382 return innerSession
.CreateSQLQuery(sql
, returnAliases
, returnClasses
);
387 innerSession
.Clear();
390 public void Dispose()
394 holder
.ReleaseSession( innerSession
);
398 throw new InvalidOperationException("Session was closed");
402 public NHibernate
.Engine
.ISessionImplementor
GetSessionImplementation()
404 return innerSession
.GetSessionImplementation();