Removed untyped contructor from ComponentRegistration and add a protected setter.
[castle.git] / ActiveRecord / Castle.ActiveRecord / Framework / ActiveRecordHooksBase.cs
blobff91a51ebfc4efd236086f2543f590db3a21a3f5
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;
20 using NHibernate.Type;
22 /// <summary>
23 /// Base class for ActiveRecord entities
24 /// that are interested in NHibernate's hooks.
25 /// </summary>
26 [Serializable]
27 public abstract class ActiveRecordHooksBase
29 /// <summary>
30 /// Hook to change the object state
31 /// before saving it.
32 /// </summary>
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)
37 OnSave();
39 return false;
42 /// <summary>
43 /// Hook to transform the read data
44 /// from the database before populating
45 /// the object instance
46 /// </summary>
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)
52 OnLoad(id);
54 return false;
57 /// <summary>
58 /// Hook to perform additional tasks
59 /// before removing the object instance representation
60 /// from the database.
61 /// </summary>
62 /// <param name="adapter"></param>
63 protected virtual internal void BeforeDelete(IDictionary adapter)
65 OnDelete();
68 /// <summary>
69 /// Called before a flush
70 /// </summary>
71 protected virtual internal void PreFlush()
75 /// <summary>
76 /// Called after a flush that actually ends in execution of the SQL statements required to
77 /// synchronize in-memory state with the database.
78 /// </summary>
79 protected virtual internal void PostFlush()
83 /// <summary>
84 /// Called when a transient entity is passed to <c>SaveOrUpdate</c>.
85 /// </summary>
86 /// <remarks>
87 /// The return value determines if the object is saved
88 /// <list>
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>
92 /// </list>
93 /// </remarks>
94 /// <returns></returns>
95 protected virtual internal bool? IsUnsaved()
97 return null;
100 /// <summary>
101 /// Called from <c>Flush()</c>. The return value determines whether the entity is updated
102 /// </summary>
103 /// <remarks>
104 /// <list>
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>
108 /// </list>
109 /// </remarks>
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,
116 IType[] types)
118 return null;
121 /// <summary>
122 ///
123 /// </summary>
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,
130 IType[] types)
132 OnUpdate();
134 return false;
137 // These methods present the same functions as the old lifecycle implementation
138 // but they are now driven by the hook functions instead.
140 /// <summary>
141 /// Lifecycle method invoked during Save of the entity
142 /// </summary>
143 protected virtual void OnSave()
147 /// <summary>
148 /// Lifecycle method invoked during Update of the entity
149 /// </summary>
150 protected virtual void OnUpdate()
154 /// <summary>
155 /// Lifecycle method invoked during Delete of the entity
156 /// </summary>
157 protected virtual void OnDelete()
161 /// <summary>
162 /// Lifecycle method invoked during Load of the entity
163 /// </summary>
164 protected virtual void OnLoad(object id)