public void Apply( Changeset changeset, SqlConnection connection, SqlTransaction transaction, RepositoryManager repositoryManager ) { var manager = this.managerFactory.CreateDataManager(connection, transaction); var basketIdRange = manager.ReserveBasketIds(changeset.CountryBasketChanges.Count()); var countryRepository = repositoryManager.ClaimCountryRepository(manager); var basketRepository = manager.GetAllCountryBaskets(); foreach (var change in changeset.CountryBasketChanges) { int basketId; var basketInfo = basketRepository.Where(b => b.IsoCountryCode == change.UnsavedBasketCountry.Country.IsoCode).SingleOrDefault(); if (basketInfo != null) { basketId = basketInfo.Id; } else { if (!basketIdRange.MoveNext()) { throw new ApplicationException("There is no ID for a basket reserved."); } basketId = basketIdRange.Current; this.ApplyCountryBasketChange(change, basketId, manager); } change.UnsavedBasketCountry.BasketId = basketId; var country = change.UnsavedBasketCountry.BasketCountry; country.Basket = new CountryBasket(basketId, change.UnsavedBasketCountry.Country); change.Other.BasketCountries.Add(country); change.Other.UnsavedBasketCountries.Remove(change.UnsavedBasketCountry); } if (changeset.TaxonomyChange != null) { ApplyTaxonomyChange(changeset, manager); } }