Base solution for your next web application
Open Closed

Domain events are not fired #2460


User avatar
0
alexzapros created

Hi,

I want to sync 2 entites, and create syncronized class. But events is not fired. Could you please tell me what I missed? From my point of view events should fire when I change user settings under top right menu.

public class UserFisherProfileSynchronizer :
        IEventHandler<EntityCreatedEventData<AbpUserBase>>,
        IEventHandler<EntityDeletedEventData<AbpUserBase>>,
        IEventHandler<EntityUpdatedEventData<AbpUserBase>>,
        ITransientDependency
    {
        private readonly IRepository<FisherProfile.FisherProfile, Guid> _fisherProfileRepository;
        private readonly IUnitOfWorkManager _unitOfWorkManager;

        /// <summary>
        /// Constructor
        /// </summary>
        public UserFisherProfileSynchronizer(
            IRepository<FisherProfile.FisherProfile, Guid> fisherProfileRepository,
            IUnitOfWorkManager unitOfWorkManager)
        {
            _fisherProfileRepository = fisherProfileRepository;
            _unitOfWorkManager = unitOfWorkManager;
        }

        /// <summary>
        /// Handles creation event of user
        /// </summary>
        [UnitOfWork]
        public virtual void HandleEvent(EntityCreatedEventData<AbpUserBase> eventData)
        {
            using (_unitOfWorkManager.Current.SetTenantId(null))
            {
                var e = new FisherProfile.FisherProfile()
                {
                    CreatorUserId = eventData.Entity.Id,
                    UserId = eventData.Entity.Id,
                    Email = eventData.Entity.EmailAddress,
                    LastModificationTime = eventData.Entity.LastLoginTime
                };

                if (eventData.Entity is User)
                {
                    e.FirstName = ((User) eventData.Entity).Name;
                    e.LastName = ((User)eventData.Entity).Surname;
                    e.MiddleName = ((User)eventData.Entity).MiddleName;
                    e.MobilePhoneNumbers = ((User)eventData.Entity).PhoneNumber;
                }

                _fisherProfileRepository.Insert(e);
            }
        }

        /// <summary>
        /// Handles deletion event of user
        /// </summary>
        /// <param name="eventData"></param>
        [UnitOfWork]
        public virtual void HandleEvent(EntityDeletedEventData<AbpUserBase> eventData)
        {
            using (_unitOfWorkManager.Current.SetTenantId(null))
            {
                var fisherProfile =
                    _fisherProfileRepository.FirstOrDefault(
                        ua => ua.UserId == eventData.Entity.Id);
                if (fisherProfile != null)
                {
                    _fisherProfileRepository.Delete(fisherProfile);
                }
            }
        }

        /// <summary>
        /// Handles update event of user
        /// </summary>
        /// <param name="eventData"></param>
        [UnitOfWork]
        public virtual void HandleEvent(EntityUpdatedEventData<AbpUserBase> eventData)
        {
            using (_unitOfWorkManager.Current.SetTenantId(null))
            {
                var fisherProfile = _fisherProfileRepository.FirstOrDefault(ua =>  ua.UserId == eventData.Entity.Id);
                if (fisherProfile != null)
                {
                    if (eventData.Entity is User)
                    {
                        fisherProfile.FirstName = ((User)eventData.Entity).Name;
                        fisherProfile.LastName = ((User)eventData.Entity).Surname;
                        fisherProfile.MiddleName = ((User)eventData.Entity).MiddleName;
                        fisherProfile.MobilePhoneNumbers = ((User)eventData.Entity).PhoneNumber;
                    }

                    fisherProfile.Email = eventData.Entity.EmailAddress;
                    fisherProfile.LastModificationTime = eventData.Entity.LastLoginTime;
                    _fisherProfileRepository.Update(fisherProfile);
                }
            }
        }
    }

1 Answer(s)
  • User Avatar
    0
    alexzapros created

    I figure out what happend - my issue.