Fixing an issue with output parameters that are of type IntPtr
[castle.git] / Facilities / ActiveRecordIntegration / Castle.Facilities.ActiveRecordIntegration / SafeSessionProxy.cs
blobc38d710dc15d0728dc05e5c34c7b629391d7d173
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.Facilities.ActiveRecordIntegration
17 using System;
18 using System.Collections;
19 using System.Data;
21 using NHibernate;
22 using NHibernate.Stat;
23 using NHibernate.Type;
25 using Castle.ActiveRecord.Framework;
27 /// <summary>
28 /// This class implements <see cref="ISession"/>
29 /// and delegates <see cref="Close"/> and
30 /// <see cref="Dispose"/> to <see cref="ISessionFactoryHolder.ReleaseSession"/>
31 /// as the session is in fact managed by ActiveRecord framework
32 /// </summary>
33 public class SafeSessionProxy : ISession, IDisposable
35 private readonly ISession innerSession;
36 private readonly ISessionFactoryHolder holder;
38 private bool wasClosed;
40 /// <summary>
41 /// Initializes a new instance of the <see cref="SafeSessionProxy"/> class.
42 /// </summary>
43 /// <param name="holder">The holder.</param>
44 /// <param name="innerSession">The inner session.</param>
45 public SafeSessionProxy(ISessionFactoryHolder holder, ISession innerSession)
47 if (innerSession == null) throw new ArgumentNullException("innerSession");
49 this.innerSession = innerSession;
50 this.holder = holder;
53 /// <summary>
54 /// Determines at which points Hibernate automatically flushes the session.
55 /// </summary>
56 /// <value></value>
57 /// <remarks>
58 /// For a readonly session, it is reasonable to set the flush mode to <c>FlushMode.Never</c>
59 /// at the start of the session (in order to achieve some extra performance).
60 /// </remarks>
61 public FlushMode FlushMode
63 get { return innerSession.FlushMode; }
64 set { innerSession.FlushMode = value; }
67 /// <summary>
68 /// Get the <see cref="T:NHibernate.ISessionFactory"/> that created this instance.
69 /// </summary>
70 /// <value></value>
71 public ISessionFactory SessionFactory
73 get { return innerSession.SessionFactory; }
76 /// <summary>
77 /// Gets the ADO.NET connection.
78 /// </summary>
79 /// <value></value>
80 /// <remarks>
81 /// Applications are responsible for calling commit/rollback upon the connection before
82 /// closing the <c>ISession</c>.
83 /// </remarks>
84 public IDbConnection Connection
86 get { return innerSession.Connection; }
89 /// <summary>
90 /// Is the <c>ISession</c> still open?
91 /// </summary>
92 /// <value></value>
93 public bool IsOpen
95 get { return innerSession.IsOpen; }
98 /// <summary>
99 /// Is the <c>ISession</c> currently connected?
100 /// </summary>
101 /// <value></value>
102 public bool IsConnected
104 get { return innerSession.IsConnected; }
107 /// <summary>
108 /// Get the current Unit of Work and return the associated <c>ITransaction</c> object.
109 /// </summary>
110 /// <value></value>
111 public ITransaction Transaction
113 get { return innerSession.Transaction; }
116 /// <summary>
117 /// Force the <c>ISession</c> to flush.
118 /// </summary>
119 /// <remarks>
120 /// Must be called at the end of a unit of work, before commiting the transaction and closing
121 /// the session (<c>Transaction.Commit()</c> calls this method). <i>Flushing</i> if the process
122 /// of synchronising the underlying persistent store with persistable state held in memory.
123 /// </remarks>
124 public void Flush()
126 innerSession.Flush();
129 /// <summary>
130 /// Disconnect the <c>ISession</c> from the current ADO.NET connection.
131 /// </summary>
132 /// <returns>
133 /// The connection provided by the application or <see langword="null"/>
134 /// </returns>
135 /// <remarks>
136 /// If the connection was obtained by Hibernate, close it or return it to the connection
137 /// pool. Otherwise return it to the application. This is used by applications which require
138 /// long transactions.
139 /// </remarks>
140 public IDbConnection Disconnect()
142 return innerSession.Disconnect();
145 /// <summary>
146 /// Obtain a new ADO.NET connection.
147 /// </summary>
148 /// <remarks>
149 /// This is used by applications which require long transactions
150 /// </remarks>
151 public void Reconnect()
153 innerSession.Reconnect();
156 /// <summary>
157 /// Reconnect to the given ADO.NET connection.
158 /// </summary>
159 /// <param name="connection">An ADO.NET connection</param>
160 /// <remarks>This is used by applications which require long transactions</remarks>
161 public void Reconnect(IDbConnection connection)
163 innerSession.Reconnect(connection);
166 /// <summary>
167 /// End the <c>ISession</c> by disconnecting from the ADO.NET connection and cleaning up.
168 /// </summary>
169 /// <returns>
170 /// The connection provided by the application or <see langword="null"/>
171 /// </returns>
172 /// <remarks>
173 /// It is not strictly necessary to <c>Close()</c> the <c>ISession</c> but you must
174 /// at least <c>Disconnect()</c> it.
175 /// </remarks>
176 public IDbConnection Close()
178 if (!wasClosed)
180 wasClosed = true;
181 holder.ReleaseSession( innerSession );
182 return null;
184 else
186 throw new InvalidOperationException("Session was closed");
190 /// <summary>
191 /// Cancel execution of the current query.
192 /// </summary>
193 /// <remarks>
194 /// May be called from one thread to stop execution of a query in another thread.
195 /// Use with care!
196 /// </remarks>
197 public void CancelQuery()
199 innerSession.CancelQuery();
202 /// <summary>
203 /// Does this <c>ISession</c> contain any changes which must be
204 /// synchronized with the database? Would any SQL be executed if
205 /// we flushed this session?
206 /// </summary>
207 /// <returns></returns>
208 public bool IsDirty()
210 return innerSession.IsDirty();
213 /// <summary>
214 /// Return the identifier of an entity instance cached by the <c>ISession</c>
215 /// </summary>
216 /// <param name="obj">a persistent instance</param>
217 /// <returns>the identifier</returns>
218 /// <remarks>
219 /// Throws an exception if the instance is transient or associated with a different
220 /// <c>ISession</c>
221 /// </remarks>
222 public object GetIdentifier(object obj)
224 return innerSession.GetIdentifier(obj);
227 /// <summary>
228 /// Is this instance associated with this Session?
229 /// </summary>
230 /// <param name="obj">an instance of a persistent class</param>
231 /// <returns>
232 /// true if the given instance is associated with this Session
233 /// </returns>
234 public bool Contains(object obj)
236 return innerSession.Contains(obj);
239 /// <summary>
240 /// Remove this instance from the session cache.
241 /// </summary>
242 /// <param name="obj">a persistent instance</param>
243 /// <remarks>
244 /// Changes to the instance will not be synchronized with the database.
245 /// This operation cascades to associated instances if the association is mapped
246 /// with <c>cascade="all"</c> or <c>cascade="all-delete-orphan"</c>.
247 /// </remarks>
248 public void Evict(object obj)
250 innerSession.Evict(obj);
253 /// <summary>
254 /// Return the persistent instance of the given entity class with the given identifier,
255 /// obtaining the specified lock mode.
256 /// </summary>
257 /// <param name="theType">A persistent class</param>
258 /// <param name="id">A valid identifier of an existing persistent instance of the class</param>
259 /// <param name="lockMode">The lock level</param>
260 /// <returns>the persistent instance</returns>
261 public object Load(Type theType, object id, LockMode lockMode)
263 return innerSession.Load(theType, id, lockMode);
266 /// <summary>
267 /// Return the persistent instance of the given entity class with the given identifier,
268 /// assuming that the instance exists.
269 /// </summary>
270 /// <param name="theType">A persistent class</param>
271 /// <param name="id">A valid identifier of an existing persistent instance of the class</param>
272 /// <returns>The persistent instance or proxy</returns>
273 /// <remarks>
274 /// You should not use this method to determine if an instance exists (use a query or
275 /// <see cref="M:NHibernate.ISession.Get(System.Type,System.Object)"/> instead). Use this only to retrieve an instance
276 /// that you assume exists, where non-existence would be an actual error.
277 /// </remarks>
278 public object Load(Type theType, object id)
280 return innerSession.Load(theType, id);
283 /// <summary>
284 /// Loads the specified id.
285 /// </summary>
286 /// <typeparam name="T"></typeparam>
287 /// <param name="id">The id.</param>
288 /// <param name="lockMode">The lock mode.</param>
289 /// <returns></returns>
290 public T Load<T>(object id, LockMode lockMode)
292 return innerSession.Load<T>(id, lockMode);
295 /// <summary>
296 /// Loads the specified id.
297 /// </summary>
298 /// <typeparam name="T"></typeparam>
299 /// <param name="id">The id.</param>
300 /// <returns></returns>
301 public T Load<T>(object id)
303 return innerSession.Load<T>(id);
306 /// <summary>
307 /// Read the persistent state associated with the given identifier into the given transient
308 /// instance.
309 /// </summary>
310 /// <param name="obj">An "empty" instance of the persistent class</param>
311 /// <param name="id">A valid identifier of an existing persistent instance of the class</param>
312 public void Load(object obj, object id)
314 innerSession.Load(obj, id);
317 /// <summary>
318 /// Return the persistent instance of the given entity class with the given identifier, or null
319 /// if there is no such persistent instance. (If the instance, or a proxy for the instance, is
320 /// already associated with the session, return that instance or proxy.)
321 /// </summary>
322 /// <param name="clazz">a persistent class</param>
323 /// <param name="id">an identifier</param>
324 /// <returns>a persistent instance or null</returns>
325 public object Get(Type clazz, object id)
327 return innerSession.Get(clazz, id);
330 /// <summary>
331 /// Return the persistent instance of the given entity class with the given identifier, or null
332 /// if there is no such persistent instance. Obtain the specified lock mode if the instance
333 /// exists.
334 /// </summary>
335 /// <param name="clazz">a persistent class</param>
336 /// <param name="id">an identifier</param>
337 /// <param name="lockMode">the lock mode</param>
338 /// <returns>a persistent instance or null</returns>
339 public object Get(Type clazz, object id, LockMode lockMode)
341 return innerSession.Get(clazz, id, lockMode);
344 /// <summary>
345 /// Return the persistent instance of the given named entity with the given identifier,
346 /// or null if there is no such persistent instance. (If the instance, or a proxy for the
347 /// instance, is already associated with the session, return that instance or proxy.)
348 /// </summary>
349 /// <param name="entityName">the entity name </param>
350 /// <param name="id">an identifier </param>
351 /// <returns> a persistent instance or null </returns>
352 public object Get(string entityName, object id)
354 return innerSession.Get(entityName, id);
357 /// <summary>
358 /// Gets the specified id.
359 /// </summary>
360 /// <typeparam name="T"></typeparam>
361 /// <param name="id">The id.</param>
362 /// <returns></returns>
363 public T Get<T>(object id)
365 return innerSession.Get<T>(id);
368 /// <summary>
369 /// Gets the specified id.
370 /// </summary>
371 /// <typeparam name="T"></typeparam>
372 /// <param name="id">The id.</param>
373 /// <param name="lockMode">The lock mode.</param>
374 /// <returns></returns>
375 public T Get<T>(object id, LockMode lockMode)
377 return innerSession.Get<T>(id, lockMode);
380 /// <summary>
381 /// Enable the named filter for this current session.
382 /// </summary>
383 /// <param name="filterName">The name of the filter to be enabled.</param>
384 /// <returns>
385 /// The Filter instance representing the enabled fiter.
386 /// </returns>
387 public IFilter EnableFilter(string filterName)
389 return innerSession.EnableFilter(filterName);
392 /// <summary>
393 /// Retrieve a currently enabled filter by name.
394 /// </summary>
395 /// <param name="filterName">The name of the filter to be retrieved.</param>
396 /// <returns>
397 /// The Filter instance representing the enabled fiter.
398 /// </returns>
399 public IFilter GetEnabledFilter(string filterName)
401 return innerSession.GetEnabledFilter(filterName);
404 /// <summary>
405 /// Disable the named filter for the current session.
406 /// </summary>
407 /// <param name="filterName">The name of the filter to be disabled.</param>
408 public void DisableFilter(string filterName)
410 innerSession.DisableFilter(filterName);
413 /// <summary>
414 /// Create a multi query, a query that can send several
415 /// queries to the server, and return all their results in a single
416 /// call.
417 /// </summary>
418 /// <returns>
419 /// An <see cref="T:NHibernate.IMultiQuery"/> that can return
420 /// a list of all the results of all the queries.
421 /// Note that each query result is itself usually a list.
422 /// </returns>
423 public IMultiQuery CreateMultiQuery()
425 return innerSession.CreateMultiQuery();
428 /// <summary>
429 /// Persist all reachable transient objects, reusing the current identifier
430 /// values. Note that this will not trigger the Interceptor of the Session.
431 /// </summary>
432 /// <param name="obj"></param>
433 /// <param name="replicationMode"></param>
434 public void Replicate(object obj, ReplicationMode replicationMode)
436 innerSession.Replicate(obj, replicationMode);
439 /// <summary>
440 /// Persist the state of the given detached instance, reusing the current
441 /// identifier value. This operation cascades to associated instances if
442 /// the association is mapped with <tt>cascade="replicate"</tt>.
443 /// </summary>
444 /// <param name="entityName"></param>
445 /// <param name="obj">a detached instance of a persistent class </param>
446 /// <param name="replicationMode"></param>
447 public void Replicate(string entityName, object obj, ReplicationMode replicationMode)
449 innerSession.Replicate(entityName, obj, replicationMode);
452 /// <summary>
453 /// Persist the given transient instance, first assigning a generated identifier.
454 /// </summary>
455 /// <param name="obj">A transient instance of a persistent class</param>
456 /// <returns>The generated identifier</returns>
457 /// <remarks>
458 /// Save will use the current value of the identifier property if the <c>Assigned</c>
459 /// generator is used.
460 /// </remarks>
461 public object Save(object obj)
463 return innerSession.Save(obj);
466 /// <summary>
467 /// Persist the given transient instance, using the given identifier.
468 /// </summary>
469 /// <param name="obj">A transient instance of a persistent class</param>
470 /// <param name="id">An unused valid identifier</param>
471 public void Save(object obj, object id)
473 innerSession.Save(obj, id);
476 /// <summary>
477 /// Persist the given transient instance, first assigning a generated identifier. (Or
478 /// using the current value of the identifier property if the <tt>assigned</tt>
479 /// generator is used.)
480 /// </summary>
481 /// <param name="entityName">The Entity name.</param>
482 /// <param name="obj">a transient instance of a persistent class </param>
483 /// <returns> the generated identifier </returns>
484 /// <remarks>
485 /// This operation cascades to associated instances if the
486 /// association is mapped with <tt>cascade="save-update"</tt>.
487 /// </remarks>
488 public object Save(string entityName, object obj)
490 return innerSession.Save(entityName, obj);
493 /// <summary>
494 /// Either <c>Save()</c> or <c>Update()</c> the given instance, depending upon the value of
495 /// its identifier property.
496 /// </summary>
497 /// <param name="obj">A transient instance containing new or updated state</param>
498 /// <remarks>
499 /// By default the instance is always saved. This behaviour may be adjusted by specifying
500 /// an <c>unsaved-value</c> attribute of the identifier property mapping
501 /// </remarks>
502 public void SaveOrUpdate(object obj)
504 innerSession.SaveOrUpdate(obj);
507 /// <summary>
508 /// Either <see cref="Save(String,Object)"/> or <see cref="Update(String,Object)"/>
509 /// the given instance, depending upon resolution of the unsaved-value checks
510 /// (see the manual for discussion of unsaved-value checking).
511 /// </summary>
512 /// <param name="entityName">The name of the entity </param>
513 /// <param name="obj">a transient or detached instance containing new or updated state </param>
514 /// <seealso cref="ISession.Save(String,Object)"/>
515 /// <seealso cref="ISession.Update(String,Object)"/>
516 /// <remarks>
517 /// This operation cascades to associated instances if the association is mapped
518 /// with <tt>cascade="save-update"</tt>.
519 /// </remarks>
520 public void SaveOrUpdate(string entityName, object obj)
522 innerSession.SaveOrUpdate(entityName, obj);
525 /// <summary>
526 /// Update the persistent instance with the identifier of the given transient instance.
527 /// </summary>
528 /// <param name="obj">A transient instance containing updated state</param>
529 /// <remarks>
530 /// If there is a persistent instance with the same identifier, an exception is thrown. If
531 /// the given transient instance has a <see langword="null"/> identifier, an exception will be thrown.
532 /// </remarks>
533 public void Update(object obj)
535 innerSession.Update(obj);
538 /// <summary>
539 /// Update the persistent state associated with the given identifier.
540 /// </summary>
541 /// <param name="obj">A transient instance containing updated state</param>
542 /// <param name="id">Identifier of persistent instance</param>
543 /// <remarks>
544 /// An exception is thrown if there is a persistent instance with the same identifier
545 /// in the current session.
546 /// </remarks>
547 public void Update(object obj, object id)
549 innerSession.Update(obj, id);
552 /// <summary>
553 /// Update the persistent instance with the identifier of the given detached
554 /// instance.
555 /// </summary>
556 /// <param name="entityName">The Entity name.</param>
557 /// <param name="obj">a detached instance containing updated state </param>
558 /// <remarks>
559 /// If there is a persistent instance with the same identifier,
560 /// an exception is thrown. This operation cascades to associated instances
561 /// if the association is mapped with <tt>cascade="save-update"</tt>.
562 /// </remarks>
563 public void Update(string entityName, object obj)
565 innerSession.Update(entityName, obj);
568 /// <summary>
569 /// Copy the state of the given object onto the persistent object with the same
570 /// identifier. If there is no persistent instance currently associated with
571 /// the session, it will be loaded. Return the persistent instance. If the
572 /// given instance is unsaved, save a copy of and return it as a newly persistent
573 /// instance. The given instance does not become associated with the session.
574 /// This operation cascades to associated instances if the association is mapped
575 /// with <tt>cascade="merge"</tt>.<br/>
576 /// <br/>
577 /// The semantics of this method are defined by JSR-220.
578 /// </summary>
579 /// <param name="obj">a detached instance with state to be copied </param>
580 /// <returns> an updated persistent instance </returns>
581 public object Merge(object obj)
583 return innerSession.Merge(obj);
586 /// <summary>
587 /// Copy the state of the given object onto the persistent object with the same
588 /// identifier. If there is no persistent instance currently associated with
589 /// the session, it will be loaded. Return the persistent instance. If the
590 /// given instance is unsaved, save a copy of and return it as a newly persistent
591 /// instance. The given instance does not become associated with the session.
592 /// This operation cascades to associated instances if the association is mapped
593 /// with <tt>cascade="merge"</tt>.<br/>
594 /// <br/>
595 /// The semantics of this method are defined by JSR-220.
596 /// </summary>
597 /// <param name="entityName">The entity name</param>
598 /// <param name="obj">a detached instance with state to be copied </param>
599 /// <returns> an updated persistent instance </returns>
600 public object Merge(string entityName, object obj)
602 return innerSession.Merge(entityName, obj);
605 /// <summary>
606 /// Make a transient instance persistent. This operation cascades to associated
607 /// instances if the association is mapped with <tt>cascade="persist"</tt>.<br/>
608 /// <br/>
609 /// The semantics of this method are defined by JSR-220.
610 /// </summary>
611 /// <param name="obj">a transient instance to be made persistent </param>
612 public void Persist(object obj)
614 innerSession.Persist(obj);
617 /// <summary>
618 /// Make a transient instance persistent. This operation cascades to associated
619 /// instances if the association is mapped with <tt>cascade="persist"</tt>.<br/>
620 /// <br/>
621 /// The semantics of this method are defined by JSR-220.
622 /// </summary>
623 /// <param name="entityName">The entity name.</param>
624 /// <param name="obj">a transient instance to be made persistent </param>
625 public void Persist(string entityName, object obj)
627 innerSession.Persist(entityName, obj);
630 /// <summary>
631 /// Copy the state of the given object onto the persistent object with the same
632 /// identifier. If there is no persistent instance currently associated with
633 /// the session, it will be loaded. Return the persistent instance. If the
634 /// given instance is unsaved or does not exist in the database, save it and
635 /// return it as a newly persistent instance. Otherwise, the given instance
636 /// does not become associated with the session.
637 /// </summary>
638 /// <param name="obj">a transient instance with state to be copied</param>
639 /// <returns>an updated persistent instance</returns>
640 public object SaveOrUpdateCopy(object obj)
642 return innerSession.SaveOrUpdateCopy(obj);
645 /// <summary>
646 /// Copy the state of the given object onto the persistent object with the
647 /// given identifier. If there is no persistent instance currently associated
648 /// with the session, it will be loaded. Return the persistent instance. If
649 /// there is no database row with the given identifier, save the given instance
650 /// and return it as a newly persistent instance. Otherwise, the given instance
651 /// does not become associated with the session.
652 /// </summary>
653 /// <param name="obj">a persistent or transient instance with state to be copied</param>
654 /// <param name="id">the identifier of the instance to copy to</param>
655 /// <returns>an updated persistent instance</returns>
656 public object SaveOrUpdateCopy(object obj, object id)
658 return innerSession.SaveOrUpdateCopy(obj, id);
661 /// <summary>
662 /// Remove a persistent instance from the datastore.
663 /// </summary>
664 /// <param name="obj">The instance to be removed</param>
665 /// <remarks>
666 /// The argument may be an instance associated with the receiving <c>ISession</c> or a
667 /// transient instance with an identifier associated with existing persistent state.
668 /// </remarks>
669 public void Delete(object obj)
671 innerSession.Delete(obj);
674 /// <summary>
675 /// Execute a query
676 /// </summary>
677 /// <param name="query">A query expressed in Hibernate's query language</param>
678 /// <returns>A distinct list of instances</returns>
679 /// <remarks>See <see cref="M:NHibernate.IQuery.List"/> for implications of <c>cache</c> usage.</remarks>
680 public IList Find(String query)
682 return innerSession.CreateQuery(query).List();
685 /// <summary>
686 /// Execute a query, binding a value to a "?" parameter in the query string.
687 /// </summary>
688 /// <param name="query">The query string</param>
689 /// <param name="value">A value to be bound to a "?" placeholder</param>
690 /// <param name="type">The Hibernate type of the value</param>
691 /// <returns>A distinct list of instances</returns>
692 /// <remarks>See <see cref="M:NHibernate.IQuery.List"/> for implications of <c>cache</c> usage.</remarks>
693 public IList Find(String query, object value, IType type)
695 // TODO: This is deprecated. Use ISession.CreateQuery().SetXYZ().List()
696 return innerSession.Find(query, value, type);
699 /// <summary>
700 /// Execute a query, binding an array of values to a "?" parameters in the query string.
701 /// </summary>
702 /// <param name="query">The query string</param>
703 /// <param name="values">An array of values to be bound to the "?" placeholders</param>
704 /// <param name="types">An array of Hibernate types of the values</param>
705 /// <returns>A distinct list of instances</returns>
706 /// <remarks>See <see cref="M:NHibernate.IQuery.List"/> for implications of <c>cache</c> usage.</remarks>
707 public IList Find(String query, object[] values, IType[] types)
709 // TODO: This is deprecated. Use ISession.CreateQuery().SetXYZ().List()
710 return innerSession.Find(query, values, types);
713 /// <summary>
714 /// Execute a query and return the results in an interator.
715 /// </summary>
716 /// <param name="query">The query string</param>
717 /// <returns>An enumerator</returns>
718 /// <remarks>
719 /// <para>
720 /// If the query has multiple return values, values will be returned in an array of
721 /// type <c>object[]</c>.
722 /// </para>
723 /// <para>
724 /// Entities returned as results are initialized on demand. The first SQL query returns
725 /// identifiers only. So <c>Enumerator()</c> is usually a less efficient way to retrieve
726 /// object than <c>List()</c>.
727 /// </para>
728 /// </remarks>
729 public IEnumerable Enumerable(String query)
731 // TODO: This is deprecated. Use ISession.CreateQuery().SetXYZ().List()
732 return innerSession.Enumerable(query);
735 /// <summary>
736 /// Execute a query and return the results in an interator,
737 /// binding a value to a "?" parameter in the query string.
738 /// </summary>
739 /// <param name="query">The query string</param>
740 /// <param name="value">A value to be written to a "?" placeholder in the query string</param>
741 /// <param name="type">The hibernate type of the value</param>
742 /// <returns>An enumerator</returns>
743 /// <remarks>
744 /// <para>
745 /// If the query has multiple return values, values will be returned in an array of
746 /// type <c>object[]</c>.
747 /// </para>
748 /// <para>
749 /// Entities returned as results are initialized on demand. The first SQL query returns
750 /// identifiers only. So <c>Enumerator()</c> is usually a less efficient way to retrieve
751 /// object than <c>List()</c>.
752 /// </para>
753 /// </remarks>
754 public IEnumerable Enumerable(String query, object value, IType type)
756 // TODO: This is deprecated. Use ISession.CreateQuery().SetXYZ().List()
757 return innerSession.Enumerable(query, value, type);
760 /// <summary>
761 /// Execute a query and return the results in an interator,
762 /// binding the values to "?"s parameters in the query string.
763 /// </summary>
764 /// <param name="query">The query string</param>
765 /// <param name="values">A list of values to be written to "?" placeholders in the query</param>
766 /// <param name="types">A list of hibernate types of the values</param>
767 /// <returns>An enumerator</returns>
768 /// <remarks>
769 /// <para>
770 /// If the query has multiple return values, values will be returned in an array of
771 /// type <c>object[]</c>.
772 /// </para>
773 /// <para>
774 /// Entities returned as results are initialized on demand. The first SQL query returns
775 /// identifiers only. So <c>Enumerator()</c> is usually a less efficient way to retrieve
776 /// object than <c>List()</c>.
777 /// </para>
778 /// </remarks>
779 public IEnumerable Enumerable(String query, object[] values, IType[] types)
781 // TODO: This is deprecated. Use ISession.CreateQuery().SetXYZ().List()
782 return innerSession.Enumerable(query, values, types);
785 /// <summary>
786 /// Apply a filter to a persistent collection.
787 /// </summary>
788 /// <param name="collection">A persistent collection to filter</param>
789 /// <param name="filter">A filter query string</param>
790 /// <returns>The resulting collection</returns>
791 /// <remarks>
792 /// A filter is a Hibernate query that may refer to <c>this</c>, the collection element.
793 /// Filters allow efficient access to very large lazy collections. (Executing the filter
794 /// does not initialize the collection.)
795 /// </remarks>
796 public ICollection Filter(object collection, String filter)
798 // TODO: This is deprecated. Use ISession.CreateQuery().SetXYZ().List()
799 return innerSession.Filter(collection, filter);
802 /// <summary>
803 /// Apply a filter to a persistent collection, binding the given parameter to a "?" placeholder
804 /// </summary>
805 /// <param name="collection">A persistent collection to filter</param>
806 /// <param name="filter">A filter query string</param>
807 /// <param name="value">A value to be written to a "?" placeholder in the query</param>
808 /// <param name="type">The hibernate type of value</param>
809 /// <returns>A collection</returns>
810 /// <remarks>
811 /// A filter is a Hibernate query that may refer to <c>this</c>, the collection element.
812 /// Filters allow efficient access to very large lazy collections. (Executing the filter
813 /// does not initialize the collection.)
814 /// </remarks>
815 public ICollection Filter(object collection, String filter, object value, IType type)
817 // TODO: This is deprecated. Use ISession.CreateQuery().SetXYZ().List()
818 return innerSession.Filter(collection, filter, value, type);
821 /// <summary>
822 /// Apply a filter to a persistent collection, binding the given parameters to "?" placeholders.
823 /// </summary>
824 /// <param name="collection">A persistent collection to filter</param>
825 /// <param name="filter">A filter query string</param>
826 /// <param name="values">The values to be written to "?" placeholders in the query</param>
827 /// <param name="types">The hibernate types of the values</param>
828 /// <returns>A collection</returns>
829 /// <remarks>
830 /// A filter is a Hibernate query that may refer to <c>this</c>, the collection element.
831 /// Filters allow efficient access to very large lazy collections. (Executing the filter
832 /// does not initialize the collection.)
833 /// </remarks>
834 public ICollection Filter(object collection, String filter, object[] values, IType[] types)
836 // TODO: This is deprecated. Use ISession.CreateQuery().SetXYZ().List()
837 return innerSession.Filter(collection, filter, values, types);
840 /// <summary>
841 /// Return the entity name for a persistent entity
842 /// </summary>
843 /// <param name="obj">a persistent entity</param>
844 /// <returns>the entity name</returns>
845 public string GetEntityName(object obj)
847 return innerSession.GetEntityName(obj);
850 /// <summary>
851 /// Sets the batch size of the session
852 /// </summary>
853 /// <param name="batchSize"></param>
854 /// <returns></returns>
855 public ISession SetBatchSize(int batchSize)
857 return innerSession.SetBatchSize(batchSize);
860 /// <summary>
861 /// An <see cref="T:NHibernate.IMultiCriteria"/> that can return a list of all the results
862 /// of all the criterias.
863 /// </summary>
864 /// <returns></returns>
865 public IMultiCriteria CreateMultiCriteria()
867 return innerSession.CreateMultiCriteria();
870 /// <summary>
871 /// The current cache mode.
872 /// </summary>
873 /// <value></value>
874 /// <remarks>
875 /// Cache mode determines the manner in which this session can interact with
876 /// the second level cache.
877 /// </remarks>
878 public CacheMode CacheMode
880 get { return innerSession.CacheMode; }
881 set { innerSession.CacheMode = value; }
884 /// <summary>
885 /// Get the statistics for this session.
886 /// </summary>
887 /// <value></value>
888 public ISessionStatistics Statistics
890 get { return innerSession.Statistics; }
893 /// <summary>
894 /// Delete all objects returned by the query.
895 /// </summary>
896 /// <param name="query">The query string</param>
897 /// <returns>Returns the number of objects deleted.</returns>
898 public int Delete(String query)
900 return innerSession.Delete(query);
903 /// <summary>
904 /// Delete all objects returned by the query.
905 /// </summary>
906 /// <param name="query">The query string</param>
907 /// <param name="value">A value to be written to a "?" placeholer in the query</param>
908 /// <param name="type">The hibernate type of value.</param>
909 /// <returns>The number of instances deleted</returns>
910 public int Delete(String query, object value, IType type)
912 return innerSession.Delete(query, value, type);
915 /// <summary>
916 /// Delete all objects returned by the query.
917 /// </summary>
918 /// <param name="query">The query string</param>
919 /// <param name="values">A list of values to be written to "?" placeholders in the query</param>
920 /// <param name="types">A list of Hibernate types of the values</param>
921 /// <returns>The number of instances deleted</returns>
922 public int Delete(String query, object[] values, IType[] types)
924 return innerSession.Delete(query, values, types);
927 /// <summary>
928 /// Obtain the specified lock level upon the given object.
929 /// </summary>
930 /// <param name="obj">A persistent instance</param>
931 /// <param name="lockMode">The lock level</param>
932 public void Lock(object obj, LockMode lockMode)
934 innerSession.Lock(obj, lockMode);
937 /// <summary>
938 /// Obtain the specified lock level upon the given object.
939 /// </summary>
940 /// <param name="entityName">The Entity name.</param>
941 /// <param name="obj">a persistent or transient instance </param>
942 /// <param name="lockMode">the lock level </param>
943 /// <remarks>
944 /// This may be used to perform a version check (<see cref="LockMode.Read"/>), to upgrade to a pessimistic
945 /// lock (<see cref="LockMode.Upgrade"/>), or to simply reassociate a transient instance
946 /// with a session (<see cref="LockMode.None"/>). This operation cascades to associated
947 /// instances if the association is mapped with <tt>cascade="lock"</tt>.
948 /// </remarks>
949 public void Lock(string entityName, object obj, LockMode lockMode)
951 innerSession.Lock(entityName, obj, lockMode);
954 /// <summary>
955 /// Re-read the state of the given instance from the underlying database.
956 /// </summary>
957 /// <param name="obj">A persistent instance</param>
958 /// <remarks>
959 /// <para>
960 /// It is inadvisable to use this to implement long-running sessions that span many
961 /// business tasks. This method is, however, useful in certain special circumstances.
962 /// </para>
963 /// <para>
964 /// For example,
965 /// <list>
966 /// <item>Where a database trigger alters the object state upon insert or update</item>
967 /// <item>After executing direct SQL (eg. a mass update) in the same session</item>
968 /// <item>After inserting a <c>Blob</c> or <c>Clob</c></item>
969 /// </list>
970 /// </para>
971 /// </remarks>
972 public void Refresh(object obj)
974 innerSession.Refresh(obj);
977 /// <summary>
978 /// Re-read the state of the given instance from the underlying database, with
979 /// the given <c>LockMode</c>.
980 /// </summary>
981 /// <param name="obj">a persistent or transient instance</param>
982 /// <param name="lockMode">the lock mode to use</param>
983 /// <remarks>
984 /// It is inadvisable to use this to implement long-running sessions that span many
985 /// business tasks. This method is, however, useful in certain special circumstances.
986 /// </remarks>
987 public void Refresh(object obj, LockMode lockMode)
989 innerSession.Refresh(obj,lockMode);
992 /// <summary>
993 /// Determine the current lock mode of the given object
994 /// </summary>
995 /// <param name="obj">A persistent instance</param>
996 /// <returns>The current lock mode</returns>
997 public LockMode GetCurrentLockMode(object obj)
999 return innerSession.GetCurrentLockMode(obj);
1002 /// <summary>
1003 /// Begin a unit of work and return the associated <c>ITransaction</c> object.
1004 /// </summary>
1005 /// <returns>A transaction instance</returns>
1006 /// <remarks>
1007 /// If a new underlying transaction is required, begin the transaction. Otherwise
1008 /// continue the new work in the context of the existing underlying transaction.
1009 /// The class of the returned <see cref="T:NHibernate.ITransaction"/> object is determined by
1010 /// the property <c>transaction_factory</c>
1011 /// </remarks>
1012 public ITransaction BeginTransaction()
1014 return innerSession.BeginTransaction();
1017 /// <summary>
1018 /// Begin a transaction with the specified <c>isolationLevel</c>
1019 /// </summary>
1020 /// <param name="isolationLevel">Isolation level for the new transaction</param>
1021 /// <returns>
1022 /// A transaction instance having the specified isolation level
1023 /// </returns>
1024 public ITransaction BeginTransaction(IsolationLevel isolationLevel)
1026 return innerSession.BeginTransaction(isolationLevel);
1029 /// <summary>
1030 /// Creates a new <c>Criteria</c> for the entity class.
1031 /// </summary>
1032 /// <param name="persistentClass">The class to Query</param>
1033 /// <returns>An ICriteria object</returns>
1034 public ICriteria CreateCriteria(Type persistentClass)
1036 return innerSession.CreateCriteria(persistentClass);
1039 /// <summary>
1040 /// Creates a new <c>Criteria</c> for the entity class with a specific alias
1041 /// </summary>
1042 /// <param name="persistentClass">The class to Query</param>
1043 /// <param name="alias">The alias of the entity</param>
1044 /// <returns>An ICriteria object</returns>
1045 public ICriteria CreateCriteria(Type persistentClass, string alias)
1047 return innerSession.CreateCriteria(persistentClass, alias);
1050 /// <summary>
1051 /// Create a new instance of <c>Query</c> for the given query string
1052 /// </summary>
1053 /// <param name="queryString">A hibernate query string</param>
1054 /// <returns>The query</returns>
1055 public IQuery CreateQuery(String queryString)
1057 return innerSession.CreateQuery(queryString);
1060 /// <summary>
1061 /// Create a new instance of <c>Query</c> for the given collection and filter string
1062 /// </summary>
1063 /// <param name="collection">A persistent collection</param>
1064 /// <param name="queryString">A hibernate query</param>
1065 /// <returns>A query</returns>
1066 public IQuery CreateFilter(object collection, String queryString)
1068 return innerSession.CreateFilter(collection, queryString);
1071 /// <summary>
1072 /// Obtain an instance of <see cref="T:NHibernate.IQuery"/> for a named query string defined in the
1073 /// mapping file.
1074 /// </summary>
1075 /// <param name="queryName">The name of a query defined externally.</param>
1076 /// <returns>
1077 /// An <see cref="T:NHibernate.IQuery"/> from a named query string.
1078 /// </returns>
1079 /// <remarks>
1080 /// The query can be either in <c>HQL</c> or <c>SQL</c> format.
1081 /// </remarks>
1082 public IQuery GetNamedQuery(String queryName)
1084 return innerSession.GetNamedQuery(queryName);
1087 /// <summary>
1088 /// Create a new instance of <see cref="T:NHibernate.ISQLQuery"/> for the given SQL query string.
1089 /// </summary>
1090 /// <param name="queryString">a query expressed in SQL</param>
1091 /// <returns>
1092 /// An <see cref="T:NHibernate.ISQLQuery"/> from the SQL string
1093 /// </returns>
1094 public ISQLQuery CreateSQLQuery(string queryString)
1096 return innerSession.CreateSQLQuery(queryString);
1099 /// <summary>
1100 /// Create a new instance of <c>IQuery</c> for the given SQL string.
1101 /// </summary>
1102 /// <param name="sql">a query expressed in SQL</param>
1103 /// <param name="returnAlias">a table alias that appears inside <c>{}</c> in the SQL string</param>
1104 /// <param name="returnClass">the returned persistent class</param>
1105 /// <returns>
1106 /// An <see cref="T:NHibernate.IQuery"/> from the SQL string
1107 /// </returns>
1108 public IQuery CreateSQLQuery(String sql, String returnAlias, Type returnClass)
1110 return innerSession.CreateSQLQuery(sql, returnAlias, returnClass);
1113 /// <summary>
1114 /// Create a new instance of <see cref="T:NHibernate.IQuery"/> for the given SQL string.
1115 /// </summary>
1116 /// <param name="sql">a query expressed in SQL</param>
1117 /// <param name="returnAliases">an array of table aliases that appear inside <c>{}</c> in the SQL string</param>
1118 /// <param name="returnClasses">the returned persistent classes</param>
1119 /// <returns>
1120 /// An <see cref="T:NHibernate.IQuery"/> from the SQL string
1121 /// </returns>
1122 public IQuery CreateSQLQuery(String sql, String[] returnAliases, Type[] returnClasses)
1124 return innerSession.CreateSQLQuery(sql, returnAliases, returnClasses);
1127 /// <summary>
1128 /// Completely clear the session. Evict all loaded instances and cancel all pending
1129 /// saves, updates and deletions. Do not close open enumerables or instances of
1130 /// <c>ScrollableResults</c>.
1131 /// </summary>
1132 public void Clear()
1134 innerSession.Clear();
1137 /// <summary>
1138 /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
1139 /// </summary>
1140 public void Dispose()
1142 if (!wasClosed)
1144 holder.ReleaseSession( innerSession );
1146 else
1148 throw new InvalidOperationException("Session was closed");
1152 /// <summary>
1153 /// Gets the session implementation.
1154 /// </summary>
1155 /// <returns>
1156 /// An NHibernate implementation of the <seealso cref="T:NHibernate.Engine.ISessionImplementor"/> interface
1157 /// </returns>
1158 /// <remarks>
1159 /// This method is provided in order to get the <b>NHibernate</b> implementation of the session from wrapper implementions.
1160 /// Implementors of the <seealso cref="T:NHibernate.ISession"/> interface should return the NHibernate implementation of this method.
1161 /// </remarks>
1162 public NHibernate.Engine.ISessionImplementor GetSessionImplementation()
1164 return innerSession.GetSessionImplementation();
1167 /// <summary>
1168 /// Starts a new Session with the given entity mode in effect. This secondary
1169 /// Session inherits the connection, transaction, and other context
1170 /// information from the primary Session. It doesn't need to be flushed
1171 /// or closed by the developer.
1172 /// </summary>
1173 /// <param name="entityMode">The entity mode to use for the new session.</param>
1174 /// <returns>The new session</returns>
1175 public ISession GetSession(EntityMode entityMode)
1177 return innerSession.GetSession(entityMode);