public void NotThrowWhenGivenNullItem() { IDynamicFieldsContainer nullItem = null; TestDelegate trySetValueCall = () => nullItem.TrySetValue("Foo", "Bar"); Assert.DoesNotThrow(trySetValueCall); }
public void ThrowWhenGivenNullItem() { IDynamicFieldsContainer nullContainer = null; TestDelegate getBooleanCall = () => nullContainer.GetBoolean(DummyDynamicFieldsContainer.BooleanFieldName); Assert.Throws <ArgumentNullException>(getBooleanCall); }
/// <summary> /// Gets Dynamic Contents DateTime Value, with current culture. If the field is not found returns /// minimum datetime value. /// </summary> /// <param name="item">Current Dynamic Content Object.</param> /// <param name="fieldName">Name of the string field.</param> /// <returns> /// Item value. /// </returns> public static DateTime GetDateTime(this IDynamicFieldsContainer item, string fieldName) { if (item.DoesFieldExist(fieldName)) { var dateTime = item.GetValue <DateTime?>(fieldName); return(dateTime.HasValue ? dateTime.Value : DateTime.MinValue); } return(DateTime.MinValue); }
/// <summary> /// Gets Dynamic Contents String Value for a given field, with current culture. /// </summary> /// <param name="item">Current Dynamic Content Object.</param> /// <param name="fieldName">Name of the string field.</param> /// <param name="defaultValue">(Optional) the default value.</param> /// <returns> /// Item value. /// </returns> public static string GetStringSafe(this IDynamicFieldsContainer item, string fieldName, string defaultValue = "") { if (item.DoesFieldExist(fieldName)) { //CONVERT VALUE AND RETURN DEFAULT VALUE IF APPLICABLE string value = Convert.ToString(item.GetValue(fieldName), CultureInfo.CurrentCulture); return(!string.IsNullOrEmpty(value) ? value : defaultValue); } return(defaultValue); }
/// <summary> /// Gets Dynamic Contents long value for a given field, with current culture. If the field is not found, returns 0 /// </summary> /// <param name="item">Current Dynamic Content Object</param> /// <param name="fieldName">Name of the string field</param> /// <returns>Item value</returns> public static long GetLong(this IDynamicFieldsContainer item, string fieldName) { if (item == null) { throw new ArgumentNullException("item"); } if (fieldName != null && item.DoesFieldExist(fieldName)) { return(Convert.ToInt64(item.GetValue(fieldName), CultureInfo.CurrentCulture)); } return(0); }
/// <summary> /// Gets Dynamic Contents DateTime Value, which can be nullable. If the value is minimum date /// time method returns null. /// </summary> /// <param name="item">Current Dynamic Content Object.</param> /// <param name="fieldName">Name of the string field.</param> /// <returns> /// Item value. /// </returns> public static DateTime?GetDateTimeSafe(this IDynamicFieldsContainer item, string fieldName) { if (item.DoesFieldExist(fieldName)) { var dateTime = item.GetValue <DateTime?>(fieldName); if (dateTime.HasValue && dateTime.Value == DateTime.MinValue) { return(null); } return(dateTime.Value); } return(null); }
/// <summary> /// Tries to set value. /// </summary> /// <param name="dataItem">The data item.</param> /// <param name="field">The field.</param> /// <param name="value">The value.</param> public static void TrySetValue(this IDynamicFieldsContainer dataItem, string field, object value) { //HANDLE FIELD ONLY IF APPLICABLE if (dataItem.DoesFieldExist(field)) { try { dataItem.SetValue(field, value); } catch (Exception) { //TODO: LOG ERROR } } }
/// <summary> /// Gets the taxons. /// </summary> /// <param name="content">The content.</param> /// <param name="taxonomyField">The taxonomy field.</param> /// <returns> /// The taxa. /// </returns> public static List <TaxonModel> GetTaxa(this IDynamicFieldsContainer content, string taxonomyField) { var taxonomyManager = TaxonomyManager.GetManager(); var list = new List <TaxonModel>(); //POPULATE CATEGORIES IF APPLICABLE if (content.DoesFieldExist(taxonomyField)) { var ids = content.GetValue <TrackedList <Guid> >(taxonomyField); if (ids.Any()) { //BUILD COLLECTION OF TAXONS foreach (Guid item in ids) { list.Add(new TaxonModel(taxonomyManager.GetTaxon(item))); } } } //RETURN CONSTRUCTED LIST return(list); }