/// <summary> /// Adds the specified country. /// </summary> /// <param name="country">The country to add.</param> /// <returns>The success or failure of the addition.</returns> public ManagerResponse Add(Country country) { ManagerResponse response = ValidateCountryEntity(country, true); if (response != ManagerResponse.Success) { return response; } using (var context = ObjectContextFactory.CreateObjectContext()) { var objectSet = context.CreateObjectSet<Country>(); objectSet.AddObject(country); response = context.SaveChanges() == 1 ? ManagerResponse.Success : ManagerResponse.UnknownFailure; } return response; }
private void FixupCountry(Country previousValue) { if (previousValue != null && previousValue.Locations.Contains(this)) { previousValue.Locations.Remove(this); } if (Country != null) { if (!Country.Locations.Contains(this)) { Country.Locations.Add(this); } if (CountryId != Country.Id) { CountryId = Country.Id; } } else if (!_settingFK) { CountryId = null; } }
/// <summary> /// Validates the members of the specified country entity. /// </summary> /// <param name="country">The country.</param> /// <param name="isNewEntity">If set to <c>true</c> the entity represents a new country.</param> /// <returns>A response indicating the validity of the <paramref name="country"/>.</returns> private static ManagerResponse ValidateCountryEntity(Country country, bool isNewEntity) { const int maximumName = 50; // Validate all country entity members if (country == null) return ManagerResponse.MissingData; if (isNewEntity) { if (country.Id != 0) return ManagerResponse.InvalidData; } else { if (country.Id <= 0) return ManagerResponse.InvalidData; } if (String.IsNullOrWhiteSpace(country.Name) || (country.Name.Length > maximumName)) return ManagerResponse.InvalidData; foreach (State state in country.States) { if (String.IsNullOrWhiteSpace(state.Name) || (state.Name.Length > maximumName)) return ManagerResponse.InvalidData; if (state.CountryId <= 0) return ManagerResponse.InvalidData; } return ManagerResponse.Success; }
/// <summary> /// Updates the specified country. /// </summary> /// <param name="country">The country to update.</param> /// <returns>The success or failure of the update.</returns> public ManagerResponse Update(Country country) { // Validate parameters ManagerResponse response = ValidateCountryEntity(country, false); if (response != ManagerResponse.Success) { return response; } using (var context = ObjectContextFactory.CreateObjectContext()) { var objectSet = context.CreateObjectSet<Country>(); objectSet.Single(c => c.Id == country.Id); objectSet.ApplyCurrentValues(country); response = context.SaveChanges() == 1 ? ManagerResponse.Success : ManagerResponse.UnknownFailure; } return response; }