Setting ModifiedDate

0 views
Skip to first unread message

epitka

unread,
Jan 16, 2009, 2:24:49 PM1/16/09
to nhusers
How would one go about updating a ModifiedDate and CreatedDate columns
if I do not want to expose setters on my class. So when record is
being persisted it should NH should auto set these two properties to
Now() and so that ModifiedDate is always updated and CreatedDate only
when record is new. Is there a way to do that.

I know I can write interceptors etc. but I do not want these to have
setters at all.


Nexus

unread,
Jan 22, 2009, 10:09:50 AM1/22/09
to nhusers
Write interceptor it doesn't need a setter :)

Will Shaver

unread,
Jan 22, 2009, 12:12:24 PM1/22/09
to nhu...@googlegroups.com
With the interceptor you can:

public class MyInterceptor : EmptyInterceptor
{
public override bool OnFlushDirty(object entity, object id, object[]
currentState, object[] previousState, string[] propertyNames, IType[]
types)
{
int md = FindProperty("ModifiedDate", propertyNames);
if(md >= 0)
currentState[i] = DateTime.Now; //or via IoC
}

public override bool OnSave(object entity, object id, object[] state,
string[] propertyNames, IType[] types)
{
int md = FindProperty("CreatedDate", propertyNames);
if(md >= 0)
state[i] = DateTime.Now; //or via IoC

}

private static int FindProperty(string property, string[] propertyNames)
{
for (int i = 0; i < propertyNames.Length; i++)
{
if (propertyNames[i] == property)
return i;
}
return -1;

epitka

unread,
Jan 23, 2009, 8:24:14 AM1/23/09
to nhusers
I wondered if there was a way to specify something like that through
the mapping.
Reply all
Reply to author
Forward
0 new messages