/// <summary>
 /// Called before a remove database command execute
 /// </summary>
 /// <typeparam name="T">Data type</typeparam>
 /// <param name="removeByQueryContext">Remove by query context</param>
 /// <returns>Return policy result</returns>
 public virtual StartingResult OnRemoveByQueryStarting <T>(RemoveByQueryContext <T> removeByQueryContext) where T : BaseEntity <T>, new()
 {
     if (removeByQueryContext == null)
     {
         return(StartingResult.Success($"Parameter:{nameof(removeByQueryContext)} is null"));
     }
     try
     {
         RemoveCacheData(removeByQueryContext.Datas);
     }
     catch (Exception ex)
     {
         return(DataCacheBehavior.GetStartingResult(DataCacheOperation.RemoveData, ex));
     }
     return(StartingResult.Success());
 }
        /// <summary>
        /// Called before a update database command execute
        /// </summary>
        /// <typeparam name="T">Data type</typeparam>
        /// <param name="updateAllContext">Update all context</param>
        /// <returns>Return policy result</returns>
        public virtual StartingResult OnUpdateAllStarting <T>(UpdateAllContext <T> updateAllContext) where T : BaseEntity <T>, new()
        {
            if (updateAllContext == null)
            {
                return(StartingResult.Success($"Parameter:{nameof(updateAllContext)} is null"));
            }
            var entityType = typeof(T);

            try
            {
                RemoveCacheDataByType(entityType);
            }
            catch (Exception ex)
            {
                return(DataCacheBehavior.GetStartingResult(DataCacheOperation.RemoveData, ex));
            }
            return(StartingResult.Success());
        }
 /// <summary>
 /// Called before a database command execute
 /// </summary>
 /// <param name="addDataContext">Add data context</param>
 /// <returns>Return the policy result</returns>
 public virtual StartingResult OnAddStarting <T>(AddDataContext <T> addDataContext) where T : BaseEntity <T>, new()
 {
     if (addDataContext == null)
     {
         return(StartingResult.Success($"Parameter:{nameof(addDataContext)} is null"));
     }
     try
     {
         AddCacheData(new QueryDataCallbackContext <T>()
         {
             Datas = addDataContext.Datas,
             Query = addDataContext.DatabaseCommand?.Query
         });
     }
     catch (Exception ex)
     {
         return(DataCacheBehavior.GetStartingResult(DataCacheOperation.AddData, ex));
     }
     return(StartingResult.Success());
 }
        /// <summary>
        /// Called before a update database command execute
        /// </summary>
        /// <typeparam name="T">Data type</typeparam>
        /// <param name="updateByQueryContext">Update by query context</param>
        /// <returns>Return policy result</returns>
        public virtual StartingResult OnUpdateByQueryStarting <T>(UpdateByQueryContext <T> updateByQueryContext) where T : BaseEntity <T>, new()
        {
            if (updateByQueryContext == null)
            {
                return(StartingResult.Success($"Parameter:{nameof(updateByQueryContext)} is null"));
            }
            if (updateByQueryContext.GetDatasProxy == null)
            {
                return(StartingResult.Success($"Parameter:{nameof(UpdateByQueryContext<T>.GetDatasProxy)} is null"));
            }
            var datas = updateByQueryContext.GetDatasProxy(updateByQueryContext.Query);

            try
            {
                RemoveCacheData(datas);
            }
            catch (Exception ex)
            {
                return(DataCacheBehavior.GetStartingResult(DataCacheOperation.RemoveData, ex));
            }
            return(StartingResult.Success());
        }
Exemple #5
0
        /// <summary>
        /// Command starting event handler
        /// </summary>
        /// <typeparam name="T">Data type</typeparam>
        /// <param name="parameter">Command starting event parameter</param>
        /// <returns>Return command starting event execute result</returns>
        CommandStartingEventExecuteResult CommandStartingEventHandler <T>(CommandStartingEventParameter parameter) where T : BaseEntity <T>, new()
        {
            if (parameter == null || parameter.Command == null)
            {
                return(CommandStartingEventExecuteResult.DefaultSuccess);
            }
            StartingResult startingResult = null;

            switch (parameter.CommandBehavior)
            {
            case CommandBehavior.Add:
                startingResult = dataCachePolicy.OnAddStarting(new AddDataContext <T>()
                {
                    Datas = new List <T>(1)
                    {
                        parameter.Data as T
                    }, DatabaseCommand = parameter.Command
                });
                break;

            case CommandBehavior.RemoveByQuery:
                Tuple <IEnumerable <T>, IQuery> removeOption = parameter.Data as Tuple <IEnumerable <T>, IQuery>;
                startingResult = dataCachePolicy.OnRemoveByQueryStarting(new RemoveByQueryContext <T>()
                {
                    DatabaseCommand = parameter.Command,
                    Datas           = removeOption?.Item1,
                    Query           = removeOption?.Item2
                });
                break;

            case CommandBehavior.UpdateByQuery:
                Tuple <Func <IQuery, List <T> >, IQuery> updateOption = parameter.Data as Tuple <Func <IQuery, List <T> >, IQuery>;
                startingResult = dataCachePolicy.OnUpdateByQueryStarting(new UpdateByQueryContext <T>()
                {
                    DatabaseCommand = parameter.Command,
                    GetDatasProxy   = updateOption?.Item1,
                    Query           = updateOption?.Item2
                });
                break;

            case CommandBehavior.RemoveObjectType:
                Tuple <Func <IQuery, List <T> >, IQuery> removeAllOption = parameter.Data as Tuple <Func <IQuery, List <T> >, IQuery>;
                startingResult = dataCachePolicy.OnRemoveAllStarting(new RemoveAllContext <T>()
                {
                    DatabaseCommand = parameter.Command,
                    Query           = removeAllOption?.Item2,
                    GetDatasProxy   = removeAllOption?.Item1
                });
                break;

            case CommandBehavior.UpdateObjectType:
                Tuple <Func <IQuery, List <T> >, IQuery> updateAllOption = parameter.Data as Tuple <Func <IQuery, List <T> >, IQuery>;
                startingResult = dataCachePolicy.OnUpdateAllStarting(new UpdateAllContext <T>()
                {
                    DatabaseCommand = parameter.Command,
                    Query           = updateAllOption?.Item2,
                    GetDatasProxy   = updateAllOption?.Item1
                });
                break;

            case CommandBehavior.RemoveData:
                startingResult = dataCachePolicy.OnRemoveStarting(new RemoveDataContext <T>()
                {
                    DatabaseCommand = parameter.Command,
                    Datas           = new List <T>(1)
                    {
                        parameter.Data as T
                    }
                });
                break;

            case CommandBehavior.Update:
                startingResult = dataCachePolicy.OnUpdateStarting(new UpdateDataContext <T>()
                {
                    DatabaseCommand = parameter.Command,
                    Datas           = new List <T>(1)
                    {
                        parameter.Data as T
                    }
                });
                break;

            default:
                break;
            }
            CommandStartingEventExecuteResult result = new CommandStartingEventExecuteResult
            {
                BreakCommand = startingResult?.BreakDatabaseCommand ?? false,
                Message      = startingResult?.Message ?? string.Empty
            };

            return(result);
        }