public void UpdateZipCity(IEnumerable <ZipCityData> zipCityData) { IZipCodeRepository zipCodeRepository = _ZipCodeRepository ?? new ZipCodeRepository(); //Dictionary<string,string> cityBatch = new Dictionary<string, string>(); //foreach (ZipCityData zipCityItem in zipCityData) //{ // cityBatch.Add(zipCityItem.ZipCode,zipCityItem.City); //} //zipCodeRepository.UpdateCityBatch(cityBatch); int counter = 0; foreach (var zipCityItem in zipCityData) { counter++; ZipCode zipEntity = zipCodeRepository.GetByZip(zipCityItem.ZipCode); if (zipEntity != null) { zipEntity.City = zipCityItem.City; zipCodeRepository.Update(zipEntity); ZipCode updateItem = zipCodeRepository.Update(zipEntity); } } }
public void UpdateZipCity(string zip, string city) { ZipCode zipEntity = _ZipCodeRepository.GetByZip(zip); if (zipEntity != null) { zipEntity.City = city; _ZipCodeRepository.Update(zipEntity); } }
public void UpdateZipCty(IEnumerable <ZipCityData> zipCityData) { IZipCodeRepository rep = _repZip ?? new ZipCodeRepository(); //This below would be the efficient way to do //var cityBatch = zipCityData.ToDictionary(c => c.ZipCode, c => c.City); //rep.UpdateZipBatch(cityBatch); //For simulate error in a transaction we gonna use this ugly code below int counter = 0; foreach (var cityData in zipCityData) { // in a operation without transaction only the first one will succeed. //counter++; //if (counter==2) throw new FaultException("Sorry, no can do !"); ZipCode zip = rep.GetByZip(cityData.ZipCode); zip.City = cityData.City; ZipCode updatedZipCode = rep.Update(zip); } // To regain control of when and where the transaction is closed // it needs to set the TransactionAutoCompleteto false, because the default is true // This code below closes the transaction OperationContext.Current.SetTransactionComplete(); }
public void UpdateZipCity(IEnumerable <ZipCityData> zipCityData) { IZipCodeRepository repository = _ZipCodeRepository ?? new ZipCodeRepository(); //Dictionary<string, string> cityBatch = new Dictionary<string, string>(); //foreach (var zipCity in zipCityData) //{ // cityBatch.Add(zipCity.ZipCode, zipCity.City); //} //repository.UpdateBatch(cityBatch); int counter = 0; foreach (var zipCityItem in zipCityData) { counter++; if (counter == 2) { throw new FaultException("Sorry you cannot do that."); } ZipCode zipEntity = repository.GetByZip(zipCityItem.ZipCode); if (zipEntity != null) { zipEntity.City = zipCityItem.City; repository.Update(zipEntity); } } OperationContext.Current.SetTransactionComplete(); }
public void UpdateZipCity(IEnumerable <ZipCityData> zipCityData) { IZipCodeRepository zipCodeRepository = _ZipCodeRepository ?? new ZipCodeRepository(); int counter = 0; foreach (ZipCityData zipCityItem in zipCityData) { counter++; //if (counter == 2) //{ // throw new FaultException("sorry, no can do."); //} ZipCode zipCodeEntity = zipCodeRepository.GetByZip(zipCityItem.ZipCode); zipCodeEntity.City = zipCityItem.City; ZipCode updateItem = zipCodeRepository.Update(zipCodeEntity); } //using (TransactionScope scope = new TransactionScope()) //{ // int counter = 0; // foreach (ZipCityData zipCityItem in zipCityData) // { // counter++; // if (counter == 2) // { // throw new FaultException("sorry, no can do."); // } // ZipCode zipCodeEntity = zipCodeRepository.GetByZip(zipCityItem.ZipCode); // zipCodeEntity.City = zipCityItem.City; // ZipCode updateItem = zipCodeRepository.Update(zipCodeEntity); // } // scope.Complete(); //} //OperationContext.Current.SetTransactionComplete(); //IZipCodeRepository zipCodeRepository = _ZipCodeRepository ?? new ZipCodeRepository(); //Dictionary<string, string> cityBatch = new Dictionary<string, string>(); //foreach (ZipCityData zipCityItem in zipCityData) //{ // cityBatch.Add(zipCityItem.ZipCode, zipCityItem.City); //} //zipCodeRepository.UpdateCityBatch(cityBatch); }
public void UpdateZipCity(string zip, string city) { IZipCodeRepository repository = _ZipCodeRepository ?? new ZipCodeRepository(); ZipCode zipEntity = repository.GetByZip(zip); if (zipEntity != null) { zipEntity.City = city; repository.Update(zipEntity); } }
public void UpdateZipCity(string zip, string city) { IZipCodeRepository rep = _repZip ?? new ZipCodeRepository(); ZipCode zipCode = rep.GetByZip(zip); if (zipCode == null) { return; } zipCode.City = city; rep.Update(zipCode); }
//[OperationBehavior(TransactionScopeRequired = true, TransactionAutoComplete = false)] //[OperationBehavior(TransactionScopeRequired = false)] public void UpdateZipCity(IEnumerable <ZipCityData> zipCityData) { IZipCodeRepository zipCodeRepository = _ZipCodeRepository ?? new ZipCodeRepository(); #region efficient way of handling batch update //Dictionary<string, string> cityBatch = new Dictionary<string, string>(); // //foreach (ZipCityData zipCityItem in zipCityData) // cityBatch.Add(zipCityItem.ZipCode, zipCityItem.City); // //zipCodeRepository.UpdateCityBatch(cityBatch); #endregion #region more inefficient, but allows for failing part of it int counter = 0; foreach (ZipCityData zipCityItem in zipCityData) { counter++; //if (counter == 2) // throw new FaultException("Sorry, no can do."); ZipCode zipCodeEntity = zipCodeRepository.GetByZip(zipCityItem.ZipCode); zipCodeEntity.City = zipCityItem.City; ZipCode updatedItem = zipCodeRepository.Update(zipCodeEntity); } //OperationContext.Current.SetTransactionComplete(); #endregion #region manual transaction handling //using (TransactionScope scope = new TransactionScope()) //{ // int counter = 0; // foreach (ZipCityData zipCityItem in zipCityData) // { // counter++; // if (counter == 2) // throw new FaultException("Sorry, no can do."); // ZipCode zipCodeEntity = zipCodeRepository.GetByZip(zipCityItem.ZipCode); // zipCodeEntity.City = zipCityItem.City; // ZipCode updatedItem = zipCodeRepository.Update(zipCodeEntity); // } // scope.Complete(); //} #endregion }
public void Can_Add_Get_And_Update_Entity() { var getEntity = zRepo.Add(zipStubs[1]); getEntity.state = "confusion"; getEntity.city = "random"; getEntity.country = "ml"; getEntity.updatedby = "me"; var updateEntity = zRepo.Update(getEntity); var newEntity = zRepo.Get(zipStubs[1].zipcodeid); Assert.AreEqual(updateEntity.zipcodeid, newEntity.zipcodeid); Assert.AreEqual(updateEntity.city, newEntity.city); Assert.AreEqual(updateEntity.state, newEntity.state); Assert.AreEqual(updateEntity.country, newEntity.country); Assert.AreEqual(updateEntity.updatedby, newEntity.updatedby); }
public void UpdateZipCity(IEnumerable <ZipCityData> zipCityData) { IZipCodeRepository zipCodeRepository = _ZipCodeRepository ?? new ZipCodeRepository(); foreach (ZipCityData zipCityItem in zipCityData) { ZipCode zipCodeEntity = zipCodeRepository.GetByZip(zipCityItem.ZipCode); zipCodeEntity.City = zipCityItem.City; ZipCode updatedItem = zipCodeRepository.Update(zipCodeEntity); IUpdateZipCallback callback = OperationContext.Current.GetCallbackChannel <IUpdateZipCallback>(); if (callback != null) { callback.ZipUpdated(zipCityItem); } } }
//[PrincipalPermission(SecurityAction.Demand, Role = "Администраторы")] public int UpdateZipCity(IEnumerable <ZipCityData> zipCityData) { string hostIdentity = WindowsIdentity.GetCurrent().Name; string primaryIdentity = ServiceSecurityContext.Current.PrimaryIdentity.Name; string windowsIdentity = ServiceSecurityContext.Current.WindowsIdentity.Name; string threadIdentity = Thread.CurrentPrincipal.Identity.Name; IZipCodeRepository zipCodeRepository = _ZipCodeRepository ?? new ZipCodeRepository(); //Dictionary<string, string> cityBatch = new Dictionary<string, string>(); //foreach (ZipCityData zipCityItem in zipCityData) //{ // cityBatch.Add(zipCityItem.ZipCode, zipCityItem.City); //} //zipCodeRepository.UpdateCityBatch(cityBatch); int counter = 0; foreach (var zipCityItem in zipCityData) { counter++; ZipCode zipCodeEntity = zipCodeRepository.GetByZip(zipCityItem.ZipCode); zipCodeEntity.City = zipCityItem.City; ZipCode updateItem = zipCodeRepository.Update(zipCodeEntity); //if (counter == 3) // throw new FaultException("Manually caused error."); IUpdateZipCallback callback = OperationContext.Current.GetCallbackChannel <IUpdateZipCallback>(); if (callback != null) { callback.ZipUpdated(zipCityItem); Thread.Sleep(100); } } return(counter); }
public int UpdateZipCity(IEnumerable <ZipCityData> zipCityData) { IZipCodeRepository zipCodeRepository = _ZipCodeRepository ?? new ZipCodeRepository(); int counter = 0; foreach (ZipCityData zipCityItem in zipCityData) { ZipCode zipCodeEntity = zipCodeRepository.GetByZip(zipCityItem.ZipCode); zipCodeEntity.City = zipCityItem.City; ZipCode updatedItem = zipCodeRepository.Update(zipCodeEntity); IUpdateZipCallback callback = OperationContext.Current.GetCallbackChannel <IUpdateZipCallback>(); if (callback != null) { callback.ZipUpdated(zipCityItem); Thread.Sleep(500); } counter++; } return(counter); }
//[OperationBehavior(TransactionScopeRequired = false)] public void UpdateZipCity(IEnumerable <ZipCityData> zipCityData) { IZipCodeRepository zipCodeRepository = _zipCodeRepository ?? new ZipCodeRepository(); #region Good code. Try to do this way always. //Dictionary<string, string> cityBatch = new Dictionary<string, string>(); //foreach (ZipCityData zipCityItem in zipCityData) //{ // cityBatch.Add(zipCityItem.Zip, zipCityItem.City); //} //zipCodeRepository.UpdateCityBatch(cityBatch); #endregion #region Throw Error int counter = 0; foreach (ZipCityData zipCityItem in zipCityData) { counter++; //This comment is to test throwing the error at the client. //if (counter == 2) //{ // //If TransactionAutoComplete = false, the below throw can not be raised, instead, do not "void", // //return something through the method. // throw new FaultException("Sorry, can't touch this!"); //} ZipCode zipCodeEntity = zipCodeRepository.GetByZip(zipCityItem.Zip); zipCodeEntity.City = zipCityItem.City; ZipCode updateItem = zipCodeRepository.Update(zipCodeEntity); } #endregion #region Manual Transaction Programming //Default is TransactionScopeOption.Required //using (TransactionScope scope = new TransactionScope()) //{ // int counter = 0; // foreach (ZipCityData zipCityItem in zipCityData) // { // counter++; // if (counter == 2) // { // throw new FaultException("Sorry, can't touch this!"); // } // ZipCode zipCodeEntity = zipCodeRepository.GetByZip(zipCityItem.Zip); // zipCodeEntity.City = zipCityItem.City; // ZipCode updateItem = zipCodeRepository.Update(zipCodeEntity); // } // scope.Complete(); //} #endregion //To perform manual transaction commit, set TransactionAutoComplete = false, and proceed the line below: //OperationContext.Current.SetTransactionComplete(); }