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
18 using System
.Collections
;
20 using NHibernate
.Type
;
23 /// Base class for ActiveRecord entities
24 /// that are interested in NHibernate's hooks.
27 public abstract class ActiveRecordHooksBase
30 /// Hook to change the object state
33 /// <param name="state"></param>
34 /// <returns>Return <c>true</c> if you have changed the state. <c>false</c> otherwise</returns>
35 protected virtual internal bool BeforeSave(IDictionary state
)
43 /// Hook to transform the read data
44 /// from the database before populating
45 /// the object instance
47 /// <param name="id">id of the obejct</param>
48 /// <param name="adapter">list of properties and their values</param>
49 /// <returns>Return <c>true</c> if you have changed the state. <c>false</c> otherwise</returns>
50 protected virtual internal bool BeforeLoad(object id
, IDictionary adapter
)
58 /// Hook to perform additional tasks
59 /// before removing the object instance representation
60 /// from the database.
62 /// <param name="adapter"></param>
63 protected virtual internal void BeforeDelete(IDictionary adapter
)
69 /// Called before a flush
71 protected virtual internal void PreFlush()
76 /// Called after a flush that actually ends in execution of the SQL statements required to
77 /// synchronize in-memory state with the database.
79 protected virtual internal void PostFlush()
84 /// Called when a transient entity is passed to <c>SaveOrUpdate</c>.
87 /// The return value determines if the object is saved
89 /// <item><c>true</c> - the entity is passed to <c>Save()</c>, resulting in an <c>INSERT</c></item>
90 /// <item><c>false</c> - the entity is passed to <c>Update()</c>, resulting in an <c>UPDATE</c></item>
91 /// <item><c>null</c> - Hibernate uses the <c>unsaved-value</c> mapping to determine if the object is unsaved</item>
94 /// <returns></returns>
95 protected virtual internal bool? IsUnsaved()
101 /// Called from <c>Flush()</c>. The return value determines whether the entity is updated
105 /// <item>an array of property indicies - the entity is dirty</item>
106 /// <item>an empty array - the entity is not dirty</item>
107 /// <item><c>null</c> - use Hibernate's default dirty-checking algorithm</item>
110 /// <param name="id"></param>
111 /// <param name="previousState"></param>
112 /// <param name="currentState"></param>
113 /// <param name="types"></param>
114 /// <returns>An array of dirty property indicies or <c>null</c> to choose default behavior</returns>
115 protected virtual internal int[] FindDirty(object id
, IDictionary previousState
, IDictionary currentState
,
124 /// <param name="id"></param>
125 /// <param name="previousState"></param>
126 /// <param name="currentState"></param>
127 /// <param name="types"></param>
128 /// <returns></returns>
129 protected virtual internal bool OnFlushDirty(object id
, IDictionary previousState
, IDictionary currentState
,
137 // These methods present the same functions as the old lifecycle implementation
138 // but they are now driven by the hook functions instead.
141 /// Lifecycle method invoked during Save of the entity
143 protected virtual void OnSave()
148 /// Lifecycle method invoked during Update of the entity
150 protected virtual void OnUpdate()
155 /// Lifecycle method invoked during Delete of the entity
157 protected virtual void OnDelete()
162 /// Lifecycle method invoked during Load of the entity
164 protected virtual void OnLoad(object id
)