protected override void PersistNewItem(ILanguage entity) { // validate iso code and culture name if (entity.IsoCode.IsNullOrWhiteSpace() || entity.CultureName.IsNullOrWhiteSpace()) { throw new InvalidOperationException("Cannot save a language without an ISO code and a culture name."); } entity.AddingEntity(); // deal with entity becoming the new default entity if (entity.IsDefault) { // set all other entities to non-default // safe (no race cond) because the service locks languages var setAllDefaultToFalse = Sql() .Update <LanguageDto>(u => u.Set(x => x.IsDefault, false)); Database.Execute(setAllDefaultToFalse); } // fallback cycles are detected at service level // insert var dto = LanguageFactory.BuildDto(entity); var id = Convert.ToInt32(Database.Insert(dto)); entity.Id = id; entity.ResetDirtyProperties(); }
protected override void PersistUpdatedItem(ILanguage entity) { // validate iso code and culture name if (entity.IsoCode.IsNullOrWhiteSpace() || entity.CultureName.IsNullOrWhiteSpace()) { throw new InvalidOperationException("Cannot save a language without an ISO code and a culture name."); } entity.UpdatingEntity(); if (entity.IsDefault) { // deal with entity becoming the new default entity // set all other entities to non-default // safe (no race cond) because the service locks languages var setAllDefaultToFalse = Sql() .Update <LanguageDto>(u => u.Set(x => x.IsDefault, false)); Database.Execute(setAllDefaultToFalse); } else { // deal with the entity not being default anymore // which is illegal - another entity has to become default var selectDefaultId = Sql() .Select <LanguageDto>(x => x.Id) .From <LanguageDto>() .Where <LanguageDto>(x => x.IsDefault); var defaultId = Database.ExecuteScalar <int>(selectDefaultId); if (entity.Id == defaultId) { throw new InvalidOperationException($"Cannot save the default language ({entity.IsoCode}) as non-default. Make another language the default language instead."); } } if (entity.IsPropertyDirty(nameof(ILanguage.IsoCode))) { //if the iso code is changing, ensure there's not another lang with the same code already assigned var sameCode = Sql() .SelectCount() .From <LanguageDto>() .Where <LanguageDto>(x => x.IsoCode == entity.IsoCode && x.Id != entity.Id); var countOfSameCode = Database.ExecuteScalar <int>(sameCode); if (countOfSameCode > 0) { throw new InvalidOperationException($"Cannot update the language to a new culture: {entity.IsoCode} since that culture is already assigned to another language entity."); } } // fallback cycles are detected at service level // update var dto = LanguageFactory.BuildDto(entity); Database.Update(dto); entity.ResetDirtyProperties(); }
protected override void PersistUpdatedItem(ILanguage entity) { ((Entity)entity).UpdatingEntity(); var factory = new LanguageFactory(); var dto = factory.BuildDto(entity); Database.Update(dto); ((ICanBeDirty)entity).ResetDirtyProperties(); }
protected override void PersistNewItem(ILanguage entity) { ((Entity)entity).AddingEntity(); var factory = new LanguageFactory(); var dto = factory.BuildDto(entity); var id = Convert.ToInt32(Database.Insert(dto)); entity.Id = id; entity.ResetDirtyProperties(); }
protected override void PersistUpdatedItem(ILanguage entity) { ((Entity)entity).UpdatingEntity(); var factory = new LanguageFactory(); var dto = factory.BuildDto(entity); Database.Update(dto); entity.ResetDirtyProperties(); //Clear the cache entries that exist by key/iso RuntimeCache.ClearCacheItem(GetCacheIdKey <ILanguage>(entity.IsoCode)); RuntimeCache.ClearCacheItem(GetCacheIdKey <ILanguage>(entity.CultureName)); }