private void SaveUpdates(UserContacts userContacts)
        {
            var streamName = ContactStreamNames.UserContacts(userContacts.UserId);
            var payload = userContacts.GetChangesSnapshot().Select(change=>change.ToJson()).ToArray();

            SaveBatch(streamName, userContacts.Version, ContactEventType.UserAggregateContactUpdate, payload)
                .Wait();
        }
        private UserContacts GetUserContacts(AccountContactBatchUpdateRecord payload)
        {
            if (!_accountIdToUserId.ContainsKey(payload.AccountId))
            {
                Logger.Error("AccountId '{0}' not found in cache.", payload.AccountId);
                Logger.Info("Existing keys : {0}", string.Join(",", _accountIdToUserId.Keys));
            }

            var userId = _accountIdToUserId[payload.AccountId];

            //I believe that this is serialized and non reentrant, thus thread safe.
            UserContacts userContacts;
            if (!_users.TryGetValue(userId, out userContacts))
            {
                userContacts = new UserContacts(userId);
                _users.Add(userId, userContacts);
            }
            return userContacts;
        }
 public void Given_a_populated_UserContacts_instance(params IAccountContactSummary[] contacts)
 {
     _userContacts = new UserContacts(Guid.NewGuid());
     using (_userContacts.TrackChanges())
     {
         foreach (var contact in contacts)
         {
             _userContacts.Add(contact);
         }
         _userContacts.GetChangesSnapshot();
         _userContacts.CommitChanges();
     }
 }
 public UserState(Guid userId, IReadOnlyDictionary<string, IAccountContactProvider> accountContactProviders)
 {
     _userId = userId; 
     _contactAggregator = new UserContacts(userId);
     _providers = accountContactProviders;
     _contactUpdates.Select(evt => evt.EventId)
         .Subscribe(_head);
 }
 public void Given_a_UserContacts_instance()
 {
     _userContacts = new UserContacts(Guid.NewGuid());
     _userContacts.TrackChanges();
 }