Пример #1
0
        /// <summary>
        /// Build command
        /// </summary>
        void BuildCommand()
        {
            //resolve records
            ResolveActivationRecord();
            var recordIds = activationMaxRecordIds.Values.OrderBy(c => c);

            commandEngineGroups          = new Dictionary <string, Tuple <ICommandExecutor, List <ICommand> > >(activationMaxRecordIds.Count);
            commandCollection            = new List <ICommand>(activationMaxRecordIds.Count);
            allowEmptyResultCommandCount = 0;
            foreach (var recordId in recordIds)
            {
                if (activationRecordValues.TryGetValue(recordId, out var record) && record != null)
                {
                    var command = record.GetExecuteCommand();
                    if (command?.IsObsolete ?? true)
                    {
                        if (allowTraceLog)
                        {
                            LogManager.LogInformation <DefaultWork>($"Execution command created based on the activity record {record.IdentityValue} are null or obsolete");
                        }
                        continue;
                    }

                    //trigger command executing event
                    var eventResult = command.TriggerStartingEvent();
                    if (eventResult?.BreakCommand ?? false)
                    {
                        if (allowTraceLog)
                        {
                            LogManager.LogInformation <DefaultWork>($"The execution command created by active record {record.IdentityValue} is blocked by the event handler:{eventResult?.Message}");
                        }
                    }

                    commandCollection.Add(command);
                    if (!command.MustReturnValueOnSuccess)
                    {
                        allowEmptyResultCommandCount += 1;
                    }
                    var commandEngines = CommandExecuteManager.GetCommandExecutors(command);
                    if (commandEngines.IsNullOrEmpty())
                    {
                        continue;
                    }
                    foreach (var engine in commandEngines)
                    {
                        if (engine == null)
                        {
                            continue;
                        }
                        var engineKey = engine.IdentityKey;
                        if (!commandEngineGroups.TryGetValue(engineKey, out Tuple <ICommandExecutor, List <ICommand> > engineValues))
                        {
                            engineValues = new Tuple <ICommandExecutor, List <ICommand> >(engine, new List <ICommand>(activationRecordValues.Count));
                        }
                        engineValues.Item2.Add(command);
                        commandEngineGroups[engineKey] = engineValues;
                    }
                }
            }
        }
Пример #2
0
 /// <summary>
 /// query
 /// </summary>
 /// <typeparam name="T">data type</typeparam>
 /// <param name="cmd">query command</param>
 /// <returns>datas</returns>
 public static async Task <IEnumerable <T> > QueryAsync <T>(ICommand cmd)
 {
     if (cmd?.IsObsolete ?? true)
     {
         return(new List <T>(0));
     }
     return(await CommandExecuteManager.QueryAsync <T>(cmd).ConfigureAwait(false));
 }
Пример #3
0
 /// <summary>
 /// query datas with paging
 /// </summary>
 /// <typeparam name="T">data type</typeparam>
 /// <param name="cmd">command</param>
 /// <returns>datas</returns>
 public static async Task <IPaging <T> > QueryPagingAsync <T>(ICommand cmd) where T : BaseEntity <T>, new()
 {
     if (cmd?.IsObsolete ?? true)
     {
         return(Paging <T> .EmptyPaging());
     }
     return(await CommandExecuteManager.QueryPagingAsync <T>(cmd).ConfigureAwait(false));
 }
Пример #4
0
 /// <summary>
 /// query single data
 /// </summary>
 /// <typeparam name="T">data type</typeparam>
 /// <param name="cmd">command</param>
 /// <returns>data</returns>
 public static async Task <T> QuerySingleAsync <T>(ICommand cmd)
 {
     if (cmd?.IsObsolete ?? true)
     {
         return(default(T));
     }
     return(await CommandExecuteManager.QuerySingleAsync <T>(cmd).ConfigureAwait(false));
 }
Пример #5
0
 /// <summary>
 /// determine whether data is exist
 /// </summary>
 /// <param name="cmd">command</param>
 /// <returns>whether data is exist</returns>
 public static async Task <bool> QueryAsync(ICommand cmd)
 {
     if (cmd?.IsObsolete ?? true)
     {
         return(false);
     }
     return(await CommandExecuteManager.QueryAsync(cmd).ConfigureAwait(false));
 }
Пример #6
0
        /// <summary>
        /// Commit Work
        /// </summary>
        /// <returns></returns>
        public async Task <CommitResult> CommitAsync()
        {
            try
            {
                //build commands
                BuildCommand();
                //object command
                if (commandGroup.IsNullOrEmpty())
                {
                    return(new CommitResult()
                    {
                        CommitCommandCount = 0,
                        ExecutedDataCount = 0
                    });
                }
                bool beforeExecuteResult = await ExecuteCommandBeforeExecuteAsync().ConfigureAwait(false);

                if (!beforeExecuteResult)
                {
                    throw new Exception("any command BeforeExecute event return fail");
                }
                var executeOption = GetCommandExecuteOption();
                var result        = await CommandExecuteManager.ExecuteAsync(executeOption, commandGroup.Values).ConfigureAwait(false);

                var commitResult = new CommitResult()
                {
                    CommitCommandCount           = commandList.Count,
                    ExecutedDataCount            = result,
                    AllowEmptyResultCommandCount = allowEmptyResultCommandCount
                };
                await ExecuteCommandCallbackAsync(commitResult.EmptyResultOrSuccess).ConfigureAwait(false);

                if (commitResult.EmptyResultOrSuccess)
                {
                    InvokeCommitSuccessEventHandler(commitResult);                                    //local unit work success callback
                    WorkFactory.InvokeWorkCommitSuccessEventHandler(this, commitResult, commandList); //unit work global success callback
                    await ExecuteWorkCompletedDomainEventAsync().ConfigureAwait(false);               //execute domain event
                }
                return(commitResult);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                CommitCompleted();
            }
        }
Пример #7
0
 /// <summary>
 /// Query data paging
 /// </summary>
 /// <typeparam name="T">Data type</typeparam>
 /// <param name="command">Command</param>
 /// <returns>Return data paging</returns>
 public static async Task <PagingInfo <T> > QueryPagingAsync <T>(ICommand command) where T : BaseEntity <T>, new()
 {
     if (command?.IsObsolete ?? true)
     {
         return(Pager.Empty <T>());
     }
     if (command.Query == null)
     {
         command.Query = QueryManager.CreateByEntity <T>();
     }
     if (command.Query.PagingInfo == null)
     {
         command.Query.SetPaging(new PagingFilter());
     }
     return(await CommandExecuteManager.QueryPagingAsync <T>(command).ConfigureAwait(false));
 }
        /// <summary>
        /// Commit Work
        /// </summary>
        /// <returns></returns>
        public async Task <CommitResult> CommitAsync()
        {
            try
            {
                //build commands
                BuildCommand();
                //object command
                if (commandGroup.IsNullOrEmpty())
                {
                    return(new CommitResult()
                    {
                        CommitCommandCount = 0,
                        ExecutedDataCount = 0
                    });
                }
                bool beforeExecuteResult = await ExecuteCommandBeforeExecuteAsync().ConfigureAwait(false);

                if (!beforeExecuteResult)
                {
                    throw new Exception("any command BeforeExecute event return fail");
                }
                var result = await CommandExecuteManager.ExecuteAsync(commandGroup.Values).ConfigureAwait(false);

                var commitResult = new CommitResult()
                {
                    CommitCommandCount           = commandList.Count,
                    ExecutedDataCount            = result,
                    AllowEmptyResultCommandCount = allowEmptyResultCommandCount
                };
                await ExecuteCommandCallbackAsync(commitResult.EmptyResultOrSuccess).ConfigureAwait(false);

                if (commitResult.EmptyResultOrSuccess)
                {
                    CommitSuccessCallbackEvent?.Invoke();
                    WorkFactory.InvokeCommitSuccessEvent();
                }
                return(commitResult);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                CommitCompleted();
            }
        }
Пример #9
0
        /// <summary>
        /// Commit work
        /// </summary>
        /// <returns>Return work commit result</returns>
        public async Task <WorkCommitResult> CommitAsync()
        {
            try
            {
                //build commands
                BuildCommand();

                if (commandEngineGroups.IsNullOrEmpty())
                {
                    return(WorkCommitResult.Empty());
                }
                var executeOption = GetCommandExecuteOption();
                var result        = await CommandExecuteManager.ExecuteAsync(executeOption, commandEngineGroups.Values).ConfigureAwait(false);

                var commitResult = new WorkCommitResult()
                {
                    CommitCommandCount           = commandCollection.Count,
                    ExecutedDataCount            = result,
                    AllowEmptyResultCommandCount = allowEmptyResultCommandCount
                };

                // trigger command callback event
                TriggerCommandCallbackEvent(commitResult.EmptyResultOrSuccess);
                if (commitResult.EmptyResultOrSuccess)
                {
                    //Trigger commit success event
                    TriggerCommitSuccessEvent(commitResult);
                    //Trigger work global success event
                    WorkManager.TriggerWorkCommitSuccessEvent(this, commitResult, commandCollection);
                    //Execute domain event
                    TriggerWorkCompletedDomainEvent();
                    //Execute data access event
                    TriggerDataAccessEvent();
                }
                else
                {
                    //Trigger work global commit fail event
                    WorkManager.TriggerWorkCommitFailEvent(this, commitResult, commandCollection);
                }
                return(commitResult);
            }
            catch (Exception ex)
            {
                Reset();
                throw ex;
            }
        }
Пример #10
0
        /// <summary>
        /// parse activation record
        /// </summary>
        void BuildCommand()
        {
            ResolveActivationRecord();//resolve records
            var recordIds = activationRecordCollection.Values.OrderBy(c => c);

            commandGroup = new Dictionary <string, Tuple <ICommandEngine, List <ICommand> > >(activationRecordCollection.Count);
            commandList  = new List <ICommand>(activationRecordCollection.Count);
            allowEmptyResultCommandCount = 0;
            foreach (var recordId in recordIds)
            {
                if (activationRecordValueCollection.TryGetValue(recordId, out var record) && record != null)
                {
                    var command = record.GetExecuteCommand();
                    if (command?.IsObsolete ?? true)
                    {
                        continue;
                    }
                    commandList.Add(command);
                    if (!command.MustReturnValueOnSuccess)
                    {
                        allowEmptyResultCommandCount += 1;
                    }
                    var commandEngines = CommandExecuteManager.GetCommandEngines(command);
                    if (commandEngines.IsNullOrEmpty())
                    {
                        continue;
                    }
                    foreach (var engine in commandEngines)
                    {
                        if (engine == null)
                        {
                            continue;
                        }
                        var engineKey = engine.IdentityKey;
                        if (!commandGroup.TryGetValue(engineKey, out Tuple <ICommandEngine, List <ICommand> > engineValues))
                        {
                            engineValues = new Tuple <ICommandEngine, List <ICommand> >(engine, new List <ICommand>(activationRecordValueCollection.Count));
                        }
                        engineValues.Item2.Add(command);
                        commandGroup[engineKey] = engineValues;
                    }
                }
            }
        }
Пример #11
0
        /// <summary>
        /// Commit Work
        /// </summary>
        /// <returns></returns>
        public async Task <CommitResult> CommitAsync()
        {
            try
            {
                if (commandList.Count <= 0)
                {
                    return(new CommitResult()
                    {
                        CommitCommandCount = 0,
                        ExecutedDataCount = 0
                    });
                }
                var  exectCommandList    = commandList.Where(c => !c.IsObsolete).ToList();
                bool beforeExecuteResult = await ExecuteCommandBeforeExecuteAsync(exectCommandList).ConfigureAwait(false);

                if (!beforeExecuteResult)
                {
                    throw new Exception("Any Command BeforeExecute Event Return Fail");
                }
                var result = await CommandExecuteManager.ExecuteAsync(exectCommandList).ConfigureAwait(false);
                await ExecuteCommandCallbackAsync(exectCommandList, result > 0).ConfigureAwait(false);

                var commitResult = new CommitResult()
                {
                    CommitCommandCount          = exectCommandList.Count,
                    ExecutedDataCount           = result,
                    AllowNoneResultCommandCount = exectCommandList.Count(c => c.VerifyResult?.Invoke(0) ?? false)
                };
                if (commitResult.NoneCommandOrSuccess)
                {
                    CommitSuccessCallbackEvent?.Invoke();
                    UnitOfWork.InvokeCommitSuccessEvent();
                }
                return(commitResult);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                Dispose();
            }
        }
Пример #12
0
 /// <summary>
 /// Commit Command
 /// </summary>
 /// <returns></returns>
 public CommitResult Commit()
 {
     try
     {
         if (commandList.Count <= 0)
         {
             return(new CommitResult()
             {
                 CommitCommandCount = 0,
                 ExecutedDataCount = 0
             });
         }
         var  exectCommandList    = commandList.Select(c => c).ToList();
         bool beforeExecuteResult = ExecuteCommandBeforeExecute(exectCommandList);
         if (!beforeExecuteResult)
         {
             throw new Exception("Any Command BeforeExecute Event Return Fail");
         }
         var result = CommandExecuteManager.Execute(exectCommandList);
         ExecuteCommandCallback(exectCommandList, result > 0);
         var commitResult = new CommitResult()
         {
             CommitCommandCount          = exectCommandList.Count,
             ExecutedDataCount           = result,
             AllowNoneResultCommandCount = exectCommandList.Count(c => c.VerifyResult?.Invoke(0) ?? false)
         };
         if (commitResult.NoneCommandOrSuccess)
         {
             CommitSuccessCallbackEvent?.Invoke();
             UnitOfWork.InvokeCommitSuccessEvent();
         }
         return(commitResult);
     }
     catch (Exception ex)
     {
         throw ex;
     }
     finally
     {
         Dispose();
     }
 }
Пример #13
0
 /// <summary>
 /// quer
 /// </summary>
 /// <typeparam name="T">data type</typeparam>
 /// <param name="cmd">query command</param>
 /// <returns>datas</returns>
 public static IEnumerable <T> Query <T>(ICommand cmd)
 {
     return(CommandExecuteManager.Query <T>(cmd));
 }
Пример #14
0
 /// <summary>
 /// query datas with paging
 /// </summary>
 /// <typeparam name="T">data type</typeparam>
 /// <param name="cmd">command</param>
 /// <returns>datas</returns>
 public static IPaging <T> QueryPaging <T>(ICommand cmd) where T : CommandEntity <T>
 {
     return(CommandExecuteManager.QueryPaging <T>(cmd));
 }
Пример #15
0
        /// <summary>
        /// Commit work
        /// </summary>
        /// <returns>Return work commit result</returns>
        public async Task <WorkCommitResult> CommitAsync()
        {
            try
            {
                if (allowTraceLog)
                {
                    LogManager.LogInformation <DefaultWork>($"===== Work:{WorkId} commit begin =====");
                }

                //build commands
                BuildCommand();
                WorkCommitResult commitResult = null;
                if (commandEngineGroups.IsNullOrEmpty())
                {
                    commitResult = WorkCommitResult.Empty();
                }
                else
                {
                    var executeOptions = GetCommandExecuteOptions();

                    if (allowTraceLog)
                    {
                        LogManager.LogInformation <DefaultWork>($"Work:{WorkId},Command execute options:{JsonSerializeHelper.ObjectToJson(executeOptions)}");
                        LogManager.LogInformation <DefaultWork>($"Work:{WorkId},Command engine keys:{string.Join(",", commandEngineGroups.Keys)},Command count:{commandCollection.Count}");
                    }

                    int returnValue = await CommandExecuteManager.ExecuteAsync(executeOptions, commandEngineGroups.Values).ConfigureAwait(false);

                    commitResult = new WorkCommitResult()
                    {
                        CommitCommandCount           = commandCollection.Count,
                        ExecutedDataCount            = returnValue,
                        AllowEmptyResultCommandCount = allowEmptyResultCommandCount
                    };
                }

                // trigger command callback event
                TriggerCommandCallbackEvent(commitResult.EmptyResultOrSuccess);
                if (commitResult.EmptyResultOrSuccess)
                {
                    //Trigger commit success event
                    TriggerCommitSuccessEvent(commitResult);
                    //Trigger work global success event
                    WorkManager.TriggerWorkCommitSuccessEvent(this, commitResult, commandCollection);
                    //Execute domain event
                    TriggerWorkCompletedDomainEvent();
                    //Execute data access event
                    TriggerDataAccessEvent();
                }
                else
                {
                    //Trigger work global commit fail event
                    WorkManager.TriggerWorkCommitFailEvent(this, commitResult, commandCollection);
                }

                if (allowTraceLog)
                {
                    LogManager.LogInformation <DefaultWork>($"Work:{WorkId},Commit command count:{commitResult.CommitCommandCount},Execute data count:{commitResult.ExecutedDataCount},Allow empty result command result:{allowEmptyResultCommandCount}");
                }

                return(commitResult);
            }
            catch (Exception ex)
            {
                Reset();
                if (allowTraceLog)
                {
                    LogManager.LogInformation <DefaultWork>($"Work:{WorkId},Exception message : {ex.Message}");
                }
                throw ex;
            }
            finally
            {
                if (allowTraceLog)
                {
                    LogManager.LogInformation <DefaultWork>($"===== Work:{WorkId} commit end =====");
                }
            }
        }
Пример #16
0
 /// <summary>
 /// determine whether data is exist
 /// </summary>
 /// <param name="cmd">command</param>
 /// <returns>whether data is exist</returns>
 public static bool Query(ICommand cmd)
 {
     return(CommandExecuteManager.Query(cmd));
 }
Пример #17
0
 /// <summary>
 /// query single data
 /// </summary>
 /// <typeparam name="T">data type</typeparam>
 /// <param name="cmd">command</param>
 /// <returns>data</returns>
 public static T QuerySingle <T>(ICommand cmd)
 {
     return(CommandExecuteManager.QuerySingle <T>(cmd));
 }