示例#1
0
 public static IEnumerable <PropertyInfo> GetStateProperties(this IBaseDO entity)
 {
     foreach (var p in entity.GetType().GetProperties())
     {
         var navigationProperty = ReflectionHelper.ForeignKeyNavitationProperty(entity, p);
         if (navigationProperty != null && GetGenericInterface(navigationProperty.PropertyType, typeof(IStateTemplate <>)) != null)
         {
             yield return(p);
         }
     }
 }
示例#2
0
        /// <summary>
        /// Detects any updates on specified entity and tracks them via AlertManager.TrackablePropertyUpdated alert.
        /// </summary>
        /// <param name="entity">entity</param>
        /// <param name="originalValues">original properties values</param>
        /// <param name="currentValues">current properties values</param>
        /// <param name="properties">properties to track</param>
        /// <param name="updateCallback">action to call when update is detected. If it is null it defaults to AlertManager.TrackablePropertyChanged.</param>
        /// <remarks>
        /// This method would likely be called from <see cref="IModifyHook.OnModify">OnModify</see> implementation to track properties.
        /// For tracking enum typed (state) properties see <see cref="DetectStateUpdates">DetectStateUpdates</see> method.
        /// </remarks>
        public static void DetectUpdates(this IBaseDO entity,
                                         DbPropertyValues originalValues, DbPropertyValues currentValues, PropertyInfo[] properties,
                                         Action <string, object, PropertyInfo> updateCallback = null)
        {
            if (properties == null || properties.Length == 0)
            {
                return;
            }
            if (updateCallback == null)
            {
                updateCallback =
                    (entityName, idValue, property) =>
                    EventManager.TrackablePropertyUpdated(entityName, property.Name, idValue, currentValues[property.Name]);
            }
            var type       = entity.GetType();
            var idProperty = type.GetProperty("Id");
            var id         = idProperty != null?idProperty.GetValue(entity) : null;

            foreach (var property in properties)
            {
                if (!MiscUtils.AreEqual(originalValues[property.Name], currentValues[property.Name]))
                {
                    string entityName;
                    if (type.Name.EndsWith("DO", StringComparison.Ordinal))
                    {
                        entityName = type.Name.Remove(type.Name.Length - 2);
                    }
                    else if (type.BaseType != null && type.BaseType.Name.EndsWith("DO", StringComparison.Ordinal))
                    {
                        entityName = type.BaseType.Name.Remove(type.BaseType.Name.Length - 2);
                    }
                    else
                    {
                        entityName = type.Name;
                    }
                    updateCallback(entityName, id, property);
                }
            }
        }