Ricardo,
I found some information about ILifecycle in
http://alexander.lds.lg.ua/2010/04/nhibernate-%E2%80%93-part-2/
I'm reading "NHibernate provides an interface that *your classes can
implement *that alert NHibernate that instances of the class want to be
notified of those lifecycle events.". I have around 300 entities. Should I
add NHibernate in all of them for versioning? it is not a good solution.
IPreInsertEventListener, IPostInsertEventListener, IPreUpdateEventListener,
IPostUpdateEventListener are exactly what I need: centralized, short code
and maintainable. But as I told you I don't understand why in
IPostUpdateEventListener @event.OldState is returning null ...
public class PostUpdate : IPostUpdateEventListener
{
public void OnPostUpdate(PostUpdateEvent @event)
{
var audit = @event.Entity as IMustAuditTransactions;
if (audit == null)
return;
var dirtyPropertyIndexes =
@event.Persister.FindDirty
(
@event.State,
@event.OldState, <--- !! null !!
@event.Entity,
@event.Session
);
var session = @event.Session.GetSession(EntityMode.Poco);
var sysVersion = Common.Get(@event.Persister, @event.State);
string[] names = @event.Persister.PropertyNames;
foreach (var index in dirtyPropertyIndexes)
{
var propertyOldValue = Common.Get(@event.OldState, index);
var propertyNewValue = Common.Get(@event.State, index);
session.Save(new LogAuditTransactions
{
EntityFullName = @event.Entity.GetType().FullName,
EntityShortName = @event.Entity.GetType().Name,
EntityId = (int)@event.Id,
PropertyName = @event.Persister.PropertyNames[index],
Operation = "UPD",
OperationDate = DateTime.Now,
PropertyNewValue = propertyNewValue,
PropertyOldValue = propertyOldValue,
User_Guid = "test",
Version = sysVersion
});
}
}
}
internal class Common
{
internal static string Get(object[] state, int index)
{
var properyValue = state[index];
if (properyValue == null)
return string.Empty;
else
return state[index] as string;
}
internal static Int32 Get(IEntityPersister persister, object[]
state)
{
var index = Array.IndexOf(persister.PropertyNames,
"sysVersion");
return (Int32)state[index];
}
}
Any suggestion? :
El dilluns, 9 març de 2015 19:02:05 UTC+1, JJ BB va escriure:
>
> Hi folks,
>
> I'm into a record versioning requirement of all mapped entities (using
> hbm.xml files).
>
> public class User
> {
> public virtual String UserName { get; set; }
> public virtual String Password { get; set; }
> (...)
> public virtual Int32 SysVersion { get; set; }
> }
>
> I'm invoking IPostInsertEventListener from nhibernate.cfg.xml:
>
> <event type="post-insert">
> <listener class="myEvents.PostInsert, myAssembly />
> </event>
>
> This is the code:
>
> public class PostInsert : IPostInsertEventListener
> {
> //IPreInsertEventListener
> //public bool OnPreInsert(PreInsertEvent preInsertEvent)
> //{
> // Correction(preInsertEvent.Entity);
> // return false;
> //}
>
> //internal static void Correction(object entity)
> //{
> // var sysVersion =
> entity.GetType().GetProperties().FirstOrDefault
> // (
> // p => p.PropertyType == typeof(Int32) &&
> p.Name.ToLower() == "sysversion"
> // );
>
> // if (sysVersion == null)
> // return;
>
> // sysVersion.SetValue(entity, 1, null);
> //}
> public void OnPostInsert(PostInsertEvent @event)
> {
> //@event.Entity
>
> var sysVersion =
> @event.Entity.GetType().GetProperties().FirstOrDefault
> (
> p => p.PropertyType == typeof(Int32) &&
> p.Name.ToLower() == "sysversion"
> );
>
> if (sysVersion == null)
> return;
>
> sysVersion.SetValue(@event.Entity, 1, null);
> }
>
> I need centralize sysVersion Insert and Update and then audit both
> operations in an audit table. Code runs sysVersion.SetValue, but, NH does
> not save sysVersion=1 into the table.
>
> Please can you help me ?
>
--
You received this message because you are subscribed to the Google Groups
"nhusers" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/nhusers.
For more options, visit https://groups.google.com/d/optout.