Exemplo n.º 1
0
        public void Handle(UserMementoPropagatedEvent @event)
        {
            using (var db = new DisciturContext())
            {
                int itemId = _identityMapper.GetModelId <User>(@event.Memento.Id);
                if (itemId.Equals(0))
                {
                    // User not exists
                    // Add new User to Read-Model
                    string _picture = Constants.USER_DEFAULT_PICTURE;
                    string _thumb   = null;
                    if (@event.Memento.Picture != null)
                    {
                        char[] bytes = new char[@event.Memento.Picture.Length * sizeof(byte)];
                        System.Buffer.BlockCopy(@event.Memento.Picture, 0, bytes, 0, bytes.Length);
                        var _str = new string(bytes);
                        _picture = _thumb = _str;
                    }

                    User discuser = new User
                    {
                        Name     = @event.Memento.Name,
                        Surname  = @event.Memento.Surname,
                        Email    = @event.Memento.Email,
                        UserName = @event.Memento.UserName,
                        Picture  = _picture,
                        Thumb    = _thumb
                    };
                    db.Users.Add(discuser);

                    if ([email protected])
                    {
                        // Add new User-Activation Key to Read-Model
                        UserActivation userActivation = new UserActivation
                        {
                            UserName = @event.Memento.UserName,
                            Key      = @event.Memento.Id.ToString()
                        };
                        db.UserActivations.Add(userActivation);
                    }
                    db.SaveChanges();
                    _identityMapper.Map <User>(discuser.UserId, @event.Memento.Id);
                }
                // otherwise it could be used for maintenance purposes
            }
        }
Exemplo n.º 2
0
        private void ExecuteUserMigration()
        {
            foreach (var user in _db.Users)
            {
                if (_db.IdMaps.GetAggregateId <User>(user.UserId).Equals(Guid.Empty))
                {
                    // Get fresh new ID
                    Guid entityId = Guid.NewGuid();
                    while (!_db.IdMaps.GetModelId <User>(entityId).Equals(0))
                    {
                        entityId = Guid.NewGuid();
                    }

                    //Check for User Activation
                    //bool isActivated = !_db.UserActivations.Any(a => a.UserName.Equals(user.UserName));
                    // Migration starts only if no pending user activations exists (all users are already activated)
                    bool isActivated = true;

                    // Save Ids mapping
                    _db.IdMaps.Map <User>(user.UserId, entityId);

                    // Create Memento from ReadModel
                    var entity = new Discitur.Domain.Model.UserMemento(entityId, 1, user.UserName, user.Surname, user.Email, user.UserName, _imageConverter.ToPictureBytes(user.Picture), isActivated);

                    //At this point the entity migration is complete, but an event is needed in order
                    //to replay correctly all events (for brand new projections, upgrade read-model, etc)
                    //Create a fake External event: Memento Propagation Events
                    using (var stream = _store.OpenStream(entityId, 0, int.MaxValue))
                    {
                        // Memento Propagation Event
                        var propagationEvent = new UserMementoPropagatedEvent(entity);
                        stream.AddMigrationCommit(entity.GetType(), propagationEvent);
                    }

                    // Save Snapshot from entity's Memento image
                    _store.Advanced.AddSnapshot(new Snapshot(entity.Id.ToString(), entity.Version, entity));

                    Trace.WriteLine(String.Format("Successfully migrated User id: {0}, Guid: {1}, UserName:{2}", user.UserId, entityId.ToString(), user.UserName), "Migration Process");
                    _logs.Add(String.Format("{0} - Successfully migrated User id: {1}, Guid: {2}, UserName:{3}", DateTime.Now, user.UserId, entityId.ToString(), user.UserName));
                }
            }
        }