/// <summary> /// Performs soft delete for given id. /// </summary> /// <param name="id">id to soft delete</param> /// <returns>Process task.</returns> protected async Task SoftDeleteEntity(TId id) { TEntity entity; try { entity = await FindById(id); } catch (GrException grException) { Logger.Warning("Soft delete failed due to non existing entity.", grException); return; } var softDeleteAttr = ObjectAttributesUtils.GetAttributeForAnyFieldIn <IsDeletedFlagColumnAttribute>(entity.GetType()); if (softDeleteAttr == null) { Logger.Warning("Not able to perform soft-delete due to non existing soft delete column."); return; } entity.SetValueForAttribute <IsDeletedFlagColumnAttribute>(softDeleteAttr.IsDeletedValue); await Save(entity); }
/// <summary> /// Gets the value of the first Type property that contains the provided attribute. /// </summary> /// <param name="instance">Instance where to extract the value.</param> /// <typeparam name="TObject">Expected property return type.</typeparam> /// <typeparam name="TAttribute">Expected Attribute type</typeparam> /// <returns>Found property value for the attribute.</returns> public static TObject GetPropertyValueForAttribute <TObject, TAttribute>( this object instance) where TAttribute : Attribute { var propInfo = ObjectAttributesUtils.GetPropertyInfoForAttribute <TAttribute>(instance.GetType()); var value = propInfo?.GetValue(instance); return(value is TObject val ? val : default);
/// <summary> /// Gets a given attribute for a given class if it exists, otherwise returns null. /// </summary> /// <param name="type">type to validate</param> /// <typeparam name="TAttribute">Attribute to find.</typeparam> /// <returns>Found Attribute if exists</returns> public static TAttribute?GetAttributeIfExists <TAttribute>(this Type type) where TAttribute : Attribute { return(ObjectAttributesUtils.GetAttributeFor <TAttribute>(type)); }
/// <summary> /// Detects whether or not given type has given attribute. /// </summary> /// <param name="type">type to validate.</param> /// <typeparam name="TAttribute">Attribute to find</typeparam> /// <returns>Whether or not type has attribute</returns> public static bool HasAttribute <TAttribute>(this Type type) where TAttribute : Attribute { return(ObjectAttributesUtils.HasAttribute <TAttribute>(type)); }
/// <summary> /// Gets given attribute from property info if it exists. /// </summary> /// <param name="propertyInfo">self</param> /// <typeparam name="TAttribute">Attribute to find.</typeparam> /// <returns>Found attribute or default.</returns> public static TAttribute?GetAttribute <TAttribute>(this PropertyInfo propertyInfo) where TAttribute : Attribute { return(ObjectAttributesUtils.GetAttributeFromPropertyInfo <TAttribute>(propertyInfo)); }