Search code examples
c#sqlnhibernaterowversion

Row versioning in NHibernate


I'm developing a social app, which will provide users to create walls for messages (like on FB) and invite their friends. I have a table in my DB that contains the walls and their version (in TimeStamp), meaning - their last update time.

When someone updates a wall, the version will be updated and I want the user to receive this version. I'm using NHibernate and defined my version row to be type of Version, but I couldn't find a way to update a row AND receiving the new version in one atomic action, to prevent concurrency problems.

Is there a way to do this? Is there a better way to solve this problem?


Solution

  • the base entity,

    public abstract class AuditBase
    {       
        private int? _rowVersion = 0;
    
        public virtual int? RowVersion
        {
            get { return _rowVersion; }
            set { _rowVersion = value; }
        }
    }
    

    the entity,

        public partial class AXSERVICES : AuditBase
    {}
    

    you can try DefaultUpdateEventListener and DefaultSaveEventListener

    for example

        public class CustomSaveEventListener : DefaultSaveEventListener
    {
    
        protected override object PerformSave(object entity, object id, IEntityPersister persister, bool useIdentityColumn, object anything, IEventSource source, bool requiresImmediateIdAccess)
        {
            ((AuditBase)entity).RowVersion = 0;
            object obj = base.PerformSave(entity, id, persister, useIdentityColumn, anything, source, requiresImmediateIdAccess); 
    
    return obj;
            }
        }