Open Closed

Read entity state on Change Event #7712


0
andmattia created

I try to use access to prev state of an enity before save on db. I use UpdatingEvent via EventBus, but on event the state of item is already changed. I try to re read from DB via UnitOfWork Manger but i get the same value. How can access to the prev state?

using (var uow = _unitOfWorkManager.Begin(new UnitOfWorkOptions
            {
                Scope = TransactionScopeOption.RequiresNew,
                IsTransactional = true,
                IsolationLevel = IsolationLevel.ReadUncommitted
            }))
            {
                if (outerUow != null)
                {
                    _unitOfWorkManager.Current.SetTenantId(outerUow.GetTenantId());
                }
                oldEntity = _entityRepository
                    .GetAll()
                    .AddEntityFilterId(eventData.Entity).FirstOrDefault();
                uow.Complete();
            }
            
            // if change RequiresNew to Suppress I get a timeout from Database

6 Answer(s)
  • 0
    maliming created

    Can you share more code?

  • 0
    andmattia created

    this is the full class

    public abstract class WorkFlowBase<TEntity,TEntityKey> : 
            IDomainService, 
            IWorkFlow,
            IEventHandler<EntityCreatingEventData<TEntity>>,
            IEventHandler<EntityUpdatingEventData<TEntity>>,
            IEventHandler<EntityDeletingEventData<TEntity>>
        where TEntity : class, IEntity<TEntityKey>, new()
        {
            private readonly IWorkFlowManager _workFlowManager;
            private readonly IUnitOfWorkManager _unitOfWorkManager;
            private readonly IRepository<TEntity, TEntityKey> _entityRepository;
    
            public ILogger Log { get; set; }
            protected WorkFlowBase(IWorkFlowManager workFlowManager, IUnitOfWorkManager unitOfWorkManager, 
                IRepository<TEntity, TEntityKey> entityRepository)
            {
                _workFlowManager = workFlowManager;
                _unitOfWorkManager = unitOfWorkManager;
                _entityRepository = entityRepository;
                Log = NullLogger.Instance;
            }
    
            public void HandleEvent(EntityCreatingEventData<TEntity> eventData)
            {
                CallWorkFlow(eventData, EventOperation.Creating);
            }
    
            private void CallWorkFlow(EntityChangingEventData<TEntity> eventData, EventOperation operation)
            {
                var type = typeof(TEntity);
                // Gat snapshot of data
                var oldEntity = new TEntity();
                var outerUow = _unitOfWorkManager.Current;
                using (var uow = _unitOfWorkManager.Begin(new UnitOfWorkOptions
                {
                    Scope = TransactionScopeOption.RequiresNew,
                    IsTransactional = true,
                    IsolationLevel = IsolationLevel.ReadUncommitted
                }))
                {
                    if (outerUow != null)
                    {
                        _unitOfWorkManager.Current.SetTenantId(outerUow.GetTenantId());
                    }
                    oldEntity = _entityRepository
                        .GetAll()
                        .AddEntityFilterId(eventData.Entity).FirstOrDefault();
                    uow.Complete();
                }
    
               
            }
    
            public void HandleEvent(EntityDeletingEventData<TEntity> eventData)
            {
                CallWorkFlow(eventData, EventOperation.Deleting);
            }
    
            public void HandleEvent(EntityUpdatingEventData<TEntity> eventData)
            {
                CallWorkFlow(eventData, EventOperation.Deleting);
            }
        }
    
        public enum EventOperation
        {
            Creating,
            Updating,
            Deleting
    
        }
    
  • 0
    andmattia created

    Update I think EventBus Event Updateing is too late to implements my need. I think the only way is override SaveChanges

  • 0
    maliming created

    Why do you set the transaction isolation level to IsolationLevel.ReadUncommitted.

  • 0
    andmattia created

    Because I need to get data on Prev state. The Goal is to get data for entity before save and new value beacuse I need to compare one value and decide if is changed I need to do an operation.

    EventBus Changeing is too late for that goal beacuse happen after saveChanges and the data is alredy into the database.

    So now I try to override the saveChanges method to achive my goal

  • 0
    ismcagdas created

    This issue has been automatically closed because it has not had recent activity.