Ejemplo n.º 1
0
        public ITaskResult<IdentityWithResult[]> Execute(Layer layer, Func<Identity, TaskContext> contextFactory, ITaskInterceptor interceptor)
        {
            IList<IdentityWithResult> resultStorage = new List<IdentityWithResult>();

            foreach (var item in layer.Items)
            {
                Identity id = item.Id;
                ITask<object> executable = item.Task;
                TaskContext context = contextFactory(id);

                if (interceptor != null)
                {
                     interceptor.BeforeTaskExecute(id, executable, context);
                }

                ITaskResult<object> result = executable.Execute(context);
                if (interceptor != null)
                {
                    interceptor.AfterTaskExecute(id, executable, context, result);
                }

                if (!result.IsSuccess)
                {
                    return new FailureResult<IdentityWithResult[]>(result.Error);
                }

                resultStorage.Add(new IdentityWithResult(id, result.Data));
            }

            return new SuccessResult<IdentityWithResult[]>(resultStorage.ToArray());
        }
Ejemplo n.º 2
0
        public ITaskResult<IdentityWithResult[]> Execute(Layer layer, Func<Identity, TaskContext> contextFactory, ITaskInterceptor interceptor)
        {
            ITaskResult<IdentityWithResult[]> failure = null;

            var results = new ConcurrentBag<IdentityWithResult>();
            var layerTasks = layer.Items.Select(item => SystemTask.Factory.StartNew(() =>
            {
                Identity id = item.Id;
                TaskContext taskContext = contextFactory(id);

                if (interceptor != null)
                {
                    interceptor.BeforeTaskExecute(id, item.Task, taskContext);
                }

                ITaskResult<object> result = item.Task.Execute(taskContext);
                if (interceptor != null)
                {
                    interceptor.AfterTaskExecute(id, item.Task, taskContext, result);
                }

                if (result.IsSuccess)
                {
                    results.Add(new IdentityWithResult(id, result.Data));
                }
                else
                {
                    failure = new FailureResult<IdentityWithResult[]>(result.Error);
                }
            })).ToArray();

            SystemTask.WaitAll(layerTasks);

            if (failure != null)
            {
                return failure;
            }

            return new SuccessResult<IdentityWithResult[]>(results.ToArray());
        }