forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIInterceptor.cs
202 lines (186 loc) · 8.6 KB
/
IInterceptor.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
using System.Collections;
using System.Data.Common;
using NHibernate.SqlCommand;
using NHibernate.Type;
namespace NHibernate
{
/// <summary>
/// Allows user code to inspect and/or change property values before they are written and after they
/// are read from the database
/// </summary>
/// <remarks>
/// <para>
/// There might be a single instance of <c>IInterceptor</c> for a <c>SessionFactory</c>, or a new
/// instance might be specified for each <c>ISession</c>. Whichever approach is used, the interceptor
/// must be serializable if the <c>ISession</c> is to be serializable. This means that <c>SessionFactory</c>
/// -scoped interceptors should implement <c>ReadResolve()</c>.
/// </para>
/// <para>
/// The <c>ISession</c> may not be invoked from a callback (nor may a callback cause a collection or
/// proxy to be lazily initialized).
/// </para>
/// </remarks>
public interface IInterceptor
{
/// <summary>
/// Called just before an object is initialized
/// </summary>
/// <param name="entity"></param>
/// <param name="id"></param>
/// <param name="propertyNames"></param>
/// <param name="state"></param>
/// <param name="types"></param>
/// <remarks>
/// The interceptor may change the <c>state</c>, which will be propagated to the persistent
/// object. Note that when this method is called, <c>entity</c> will be an empty
/// uninitialized instance of the class.</remarks>
/// <returns><see langword="true" /> if the user modified the <c>state</c> in any way</returns>
bool OnLoad(object entity, object id, object[] state, string[] propertyNames, IType[] types);
/// <summary>
/// Called when an object is detected to be dirty, during a flush.
/// </summary>
/// <param name="currentState"></param>
/// <param name="entity"></param>
/// <param name="id"></param>
/// <param name="previousState"></param>
/// <param name="propertyNames"></param>
/// <param name="types"></param>
/// <remarks>
/// The interceptor may modify the detected <c>currentState</c>, which will be propagated to
/// both the database and the persistent object. Note that all flushes end in an actual
/// synchronization with the database, in which as the new <c>currentState</c> will be propagated
/// to the object, but not necessarily (immediately) to the database. It is strongly recommended
/// that the interceptor <b>not</b> modify the <c>previousState</c>.
/// </remarks>
/// <returns><see langword="true" /> if the user modified the <c>currentState</c> in any way</returns>
bool OnFlushDirty(object entity, object id, object[] currentState, object[] previousState, string[] propertyNames,
IType[] types);
/// <summary>
/// Called before an object is saved
/// </summary>
/// <param name="entity"></param>
/// <param name="id"></param>
/// <param name="propertyNames"></param>
/// <param name="state"></param>
/// <param name="types"></param>
/// <remarks>
/// The interceptor may modify the <c>state</c>, which will be used for the SQL <c>INSERT</c>
/// and propagated to the persistent object
/// </remarks>
/// <returns><see langword="true" /> if the user modified the <c>state</c> in any way</returns>
bool OnSave(object entity, object id, object[] state, string[] propertyNames, IType[] types);
/// <summary>
/// Called before an object is deleted
/// </summary>
/// <param name="entity"></param>
/// <param name="id"></param>
/// <param name="propertyNames"></param>
/// <param name="state"></param>
/// <param name="types"></param>
/// <remarks>
/// It is not recommended that the interceptor modify the <c>state</c>.
/// </remarks>
void OnDelete(object entity, object id, object[] state, string[] propertyNames, IType[] types);
/// <summary> Called before a collection is (re)created.</summary>
void OnCollectionRecreate(object collection, object key);
/// <summary> Called before a collection is deleted.</summary>
void OnCollectionRemove(object collection, object key);
/// <summary> Called before a collection is updated.</summary>
void OnCollectionUpdate(object collection, object key);
/// <summary>
/// Called before a flush
/// </summary>
/// <param name="entities">The entities</param>
void PreFlush(ICollection entities);
/// <summary>
/// Called after a flush that actually ends in execution of the SQL statements required to
/// synchronize in-memory state with the database.
/// </summary>
/// <param name="entities">The entities</param>
void PostFlush(ICollection entities);
/// <summary>
/// Called when a transient entity is passed to <c>SaveOrUpdate</c>.
/// </summary>
/// <remarks>
/// The return value determines if the object is saved
/// <list>
/// <item><see langword="true" /> - the entity is passed to <c>Save()</c>, resulting in an <c>INSERT</c></item>
/// <item><see langword="false" /> - the entity is passed to <c>Update()</c>, resulting in an <c>UPDATE</c></item>
/// <item><see langword="null" /> - Hibernate uses the <c>unsaved-value</c> mapping to determine if the object is unsaved</item>
/// </list>
/// </remarks>
/// <param name="entity">A transient entity</param>
/// <returns>Boolean or <see langword="null" /> to choose default behaviour</returns>
bool? IsTransient(object entity);
/// <summary>
/// Called from <c>Flush()</c>. The return value determines whether the entity is updated
/// </summary>
/// <remarks>
/// <list>
/// <item>an array of property indicies - the entity is dirty</item>
/// <item>an empty array - the entity is not dirty</item>
/// <item><see langword="null" /> - use Hibernate's default dirty-checking algorithm</item>
/// </list>
/// </remarks>
/// <param name="entity">A persistent entity</param>
/// <param name="currentState"></param>
/// <param name="id"></param>
/// <param name="previousState"></param>
/// <param name="propertyNames"></param>
/// <param name="types"></param>
/// <returns>An array of dirty property indicies or <see langword="null" /> to choose default behavior</returns>
int[] FindDirty(object entity, object id, object[] currentState, object[] previousState, string[] propertyNames,
IType[] types);
/// <summary>
/// Instantiate the entity class. Return <see langword="null" /> to indicate that Hibernate should use the default
/// constructor of the class
/// </summary>
/// <param name="entityName">the name of the entity </param>
/// <param name="id">the identifier of the new instance </param>
/// <returns>An instance of the class, or <see langword="null" /> to choose default behaviour</returns>
/// <remarks>
/// The identifier property of the returned instance
/// should be initialized with the given identifier.
/// </remarks>
object Instantiate(System.String entityName, object id);
/// <summary> Get the entity name for a persistent or transient instance</summary>
/// <param name="entity">an entity instance </param>
/// <returns> the name of the entity </returns>
string GetEntityName(object entity);
/// <summary> Get a fully loaded entity instance that is cached externally</summary>
/// <param name="entityName">the name of the entity </param>
/// <param name="id">the instance identifier </param>
/// <returns> a fully initialized entity </returns>
object GetEntity(string entityName, object id);
/// <summary>
/// Called when a NHibernate transaction is begun via the NHibernate <see cref="ITransaction" />
/// API. Will not be called if transactions are being controlled via some other mechanism.
/// </summary>
void AfterTransactionBegin(ITransaction tx);
/// <summary>
/// Called before a transaction is committed (but not before rollback).
/// </summary>
void BeforeTransactionCompletion(ITransaction tx);
/// <summary>
/// Called after a transaction is committed or rolled back.
/// </summary>
void AfterTransactionCompletion(ITransaction tx);
/// <summary> Called when sql string is being prepared. </summary>
/// <param name="sql">sql to be prepared </param>
/// <returns> original or modified sql </returns>
SqlString OnPrepareStatement(SqlString sql);
#region NH specific
/// <summary>
/// Called when a session-scoped (and <b>only</b> session scoped) interceptor is attached
/// to a session
/// </summary>
/// <remarks>
/// session-scoped-interceptor is an instance of the interceptor used only for one session.
/// The use of singleton-interceptor may cause problems in multi-thread scenario.
/// </remarks>
/// <seealso cref="ISessionFactory.OpenSession(IInterceptor)"/>
/// <seealso cref="ISessionFactory.OpenSession(DbConnection,IInterceptor)"/>
void SetSession(ISession session);
#endregion
}
}