Esempio n. 1
0
        public virtual TResponse Run <TResponse>
        (
            ITasq <TResponse> tasq
        )
        {
            TResponse retVal;

            OnLog?.Invoke(this, TasqProcess.Start, new LogEventHandlerEventArgs(tasq));

            var resolvedHandler = GetHandlerDetail(tasq);

            OnLog?.Invoke(this, TasqProcess.Start, new LogEventHandlerEventArgs(resolvedHandler.Handler));

            if (resolvedHandler.Handler is TasqHandlerAsync)
            {
                retVal = RunAsync(tasq).Result;
            }
            else
            {
                TasqHandler tasqHandlerInstance = (TasqHandler)resolvedHandler.Handler;


                tasqHandlerInstance.Initialize(tasq);
                tasqHandlerInstance.BeforeRun(tasq);
                retVal = (TResponse)tasqHandlerInstance.XRun(null, tasq);
                tasqHandlerInstance.AfterRun(tasq);
            }

            return(retVal);
        }
Esempio n. 2
0
        private TasqHandlerDetail GetHandlerDetail(ITasq tasq)
        {
            var tasqType = tasq.GetType();

            var handlerDetail = ForcedHandlerDetail != null
                ? ForcedHandlerDetail
                : p_TasqHandlerResolver.ResolveHandler(tasqType);

            if (autoClearReference)
            {
                ForcedHandlerDetail = null;
            }

            return(handlerDetail);
        }
Esempio n. 3
0
        public virtual void Run
        (
            ITasq tasq
        )
        {
            OnLog?.Invoke(this, TasqProcess.Start, new LogEventHandlerEventArgs(tasq));

            var resolvedHandler = GetHandlerDetail(tasq);

            OnLog?.Invoke(this, TasqProcess.Start, new LogEventHandlerEventArgs(resolvedHandler.Handler));

            if (resolvedHandler.Handler is TasqHandlerAsync)
            {
                RunAsync(tasq).Wait();
            }
            else
            {
                var tasqHandlerInstance = (TasqHandler)resolvedHandler.Handler;

                if (resolvedHandler.Reference.HandlerInterface.IsGenericType &&
                    resolvedHandler.Reference.HandlerInterface.GetGenericArguments().Length == 3)
                {
                    var argT1 = resolvedHandler.Reference.HandlerInterface.GetGenericArguments()[1];
                    var argT2 = resolvedHandler.Reference.HandlerInterface.GetGenericArguments()[2];

                    var method = this.GetType().GetMethods().Single(a => a.Name == nameof(Run) && a.GetGenericArguments().Length == 2);

                    method.MakeGenericMethod(argT1, argT2).Invoke(this, parameters: new object[] { tasq });
                }
                else if (resolvedHandler.Reference.HandlerInterface.IsGenericType &&
                         resolvedHandler.Reference.HandlerInterface.GetGenericArguments().Length == 2)
                {
                    var argT1 = resolvedHandler.Reference.HandlerInterface.GetGenericArguments()[1];

                    var method = this.GetType().GetMethods().Single(a => a.Name == nameof(Run) && a.GetGenericArguments().Length == 1);

                    method.MakeGenericMethod(argT1).Invoke(this, parameters: new object[] { tasq });
                }
                else
                {
                    tasqHandlerInstance.Initialize(tasq);
                    tasqHandlerInstance.BeforeRun(tasq);
                    tasqHandlerInstance.XRun(null, tasq);
                    tasqHandlerInstance.AfterRun(tasq);
                }
            }
        }
Esempio n. 4
0
        public virtual IEnumerable <TResponse> Run <TKey, TResponse>
        (
            ITasq <TKey, TResponse> tasq
        )
        {
            OnLog?.Invoke(this, TasqProcess.Start, new LogEventHandlerEventArgs(tasq));

            var resolvedHandler = GetHandlerDetail(tasq);

            OnLog?.Invoke(this, TasqProcess.Start, new LogEventHandlerEventArgs(resolvedHandler.Handler));

            if (resolvedHandler.Handler is TasqHandlerAsync)
            {
                return(RunAsync(tasq).Result);
            }

            TasqHandler tasqHandlerInstance = (TasqHandler)resolvedHandler.Handler;

            List <TResponse> retVal = new List <TResponse>();

            tasqHandlerInstance.Initialize(tasq);
            tasqHandlerInstance.BeforeRun(tasq);

            var selectionCriteria = tasqHandlerInstance.SelectionCriteria(tasq);

            if (selectionCriteria != null)
            {
                foreach (var eachSelection in selectionCriteria)
                {
                    var result = (TResponse)tasqHandlerInstance.XRun(eachSelection, tasq);

                    retVal.Add(result);
                }
            }

            tasqHandlerInstance.AfterRun(tasq);


            return(retVal);
        }
Esempio n. 5
0
 internal LogEventHandlerEventArgs(ITasq tasq, Exception exception)
     : this(tasq)
 {
     Exception = exception;
 }
Esempio n. 6
0
 internal LogEventHandlerEventArgs(ITasq tasq)
     : this($"Tasq: {tasq.GetType().FullName}")
 {
 }
Esempio n. 7
0
        public virtual Type GetHandlerType(ITasq tasq)
        {
            var resolvedHandler = GetHandlerDetail(tasq);

            return(resolvedHandler.Reference.HandlerImplementation);
        }
Esempio n. 8
0
        public virtual Task <TResponse> RunAsync <TResponse>
        (
            ITasq <TResponse> tasq,
            CancellationToken cancellationToken = default
        )
        {
            Task <TResponse> retVal = null;

            OnLog?.Invoke(this, TasqProcess.Start, new LogEventHandlerEventArgs(tasq));

            var resolvedHandler = GetHandlerDetail(tasq);

            OnLog?.Invoke(this, TasqProcess.Start, new LogEventHandlerEventArgs(resolvedHandler.Handler));

            if (resolvedHandler.Handler is TasqHandler)
            {
                retVal = Task.FromResult(Run(tasq));
            }
            else
            {
                TasqHandlerAsync tasqHandlerInstance = (TasqHandlerAsync)resolvedHandler.Handler;

                tasqHandlerInstance.p_CancellationToken = cancellationToken;

                retVal = tasqHandlerInstance.InitializeAsync(tasq)
                         .ContinueWith(res1 =>
                {
                    if (res1.IsFaulted)
                    {
                        throw new TasqException(res1.Exception);
                    }

                    return(tasqHandlerInstance.BeforeRunAsync(tasq)
                           .ContinueWith(res2 =>
                    {
                        if (res2.IsFaulted)
                        {
                            throw new TasqException(res2.Exception);
                        }

                        return tasqHandlerInstance.XRunAsync(null, tasq)
                        .ContinueWith(res3 =>
                        {
                            if (res3.IsFaulted)
                            {
                                throw new TasqException(res3.Exception);
                            }

                            return tasqHandlerInstance.AfterRunAsync(tasq)
                            .ContinueWith(res4 =>
                            {
                                if (res4.IsFaulted)
                                {
                                    throw new TasqException(res4.Exception);
                                }

                                return (Task <TResponse>)res3;
                            }).Unwrap();
                        }).Unwrap();
                    }).Unwrap());
                }).Unwrap();
            }

            return(retVal);
        }
Esempio n. 9
0
        public virtual Task <IEnumerable <TResponse> > RunAsync <TKey, TResponse>
        (
            ITasq <TKey, TResponse> tasq,
            CancellationToken cancellationToken = default
        )
        {
            Task <IEnumerable <TResponse> > retVal = null;

            OnLog?.Invoke(this, TasqProcess.Start, new LogEventHandlerEventArgs(tasq));

            var resolvedHandler = GetHandlerDetail(tasq);

            OnLog?.Invoke(this, TasqProcess.Start, new LogEventHandlerEventArgs(resolvedHandler.Handler));

            if (resolvedHandler.Handler is TasqHandler)
            {
                retVal = Task.FromResult(Run(tasq));
            }
            else
            {
                TasqHandlerAsync tasqHandlerInstance = (TasqHandlerAsync)resolvedHandler.Handler;

                retVal = tasqHandlerInstance.InitializeAsync(tasq)
                         .ContinueWith(async res1 =>
                {
                    if (res1.IsFaulted)
                    {
                        throw new TasqException(res1.Exception);
                    }

                    List <TResponse> abRet = new List <TResponse>();

                    var selectionCriteria = await tasqHandlerInstance.SelectionCriteriaAsync(tasq);

                    List <Task <TResponse> > listOfTask = new List <Task <TResponse> >();

                    foreach (var item in selectionCriteria)
                    {
                        var task = tasqHandlerInstance.BeforeRunAsync(tasq)
                                   .ContinueWith(res2 =>
                        {
                            if (res2.IsFaulted)
                            {
                                throw new TasqException(res2.Exception);
                            }

                            return(tasqHandlerInstance.XRunAsync(item, tasq)
                                   .ContinueWith(res3 =>
                            {
                                if (res3.IsFaulted)
                                {
                                    throw new TasqException(res3.Exception);
                                }

                                return tasqHandlerInstance.AfterRunAsync(tasq)
                                .ContinueWith(res5 =>
                                {
                                    if (res5.IsFaulted)
                                    {
                                        throw new TasqException(res5.Exception);
                                    }

                                    var tasR3 = (Task <TResponse>)res3;

                                    abRet.Add(tasR3.Result);

                                    return tasR3;
                                }).Unwrap();
                            }, TaskContinuationOptions.AttachedToParent).Unwrap());
                        }, TaskContinuationOptions.AttachedToParent).Unwrap();

                        listOfTask.Add(task);
                    }

                    var allResult = await Task.WhenAll(listOfTask);

                    return(allResult.AsEnumerable());
                }).Unwrap();
            }

            return(retVal);
        }
Esempio n. 10
0
        public virtual Task RunAsync
        (
            ITasq tasq,
            CancellationToken cancellationToken = default
        )
        {
            OnLog?.Invoke(this, TasqProcess.Start, new LogEventHandlerEventArgs(tasq));

            var resolvedHandler = GetHandlerDetail(tasq);

            OnLog?.Invoke(this, TasqProcess.Start, new LogEventHandlerEventArgs(resolvedHandler.Handler));

            if (resolvedHandler.Reference.HandlerInterface.IsGenericType &&
                resolvedHandler.Reference.HandlerInterface.GetGenericArguments().Length == 3)
            {
                var argT1 = resolvedHandler.Reference.HandlerInterface.GetGenericArguments()[1];
                var argT2 = resolvedHandler.Reference.HandlerInterface.GetGenericArguments()[2];

                var method = this.GetType().GetMethods().Single(a => a.Name == nameof(RunAsync) && a.GetGenericArguments().Length == 2);

                return
                    (((Task)method.MakeGenericMethod(argT1, argT2)
                      .Invoke(this, parameters: new object[] { tasq, cancellationToken })
                      )
                     .ContinueWith(a =>
                {
                    if (a.IsFaulted)
                    {
                        throw a.Exception;
                    }
                }));
            }
            else if (resolvedHandler.Reference.HandlerInterface.IsGenericType &&
                     resolvedHandler.Reference.HandlerInterface.GetGenericArguments().Length == 2)
            {
                var argT1 = resolvedHandler.Reference.HandlerInterface.GetGenericArguments()[1];

                var method = this.GetType().GetMethods().Single(a => a.Name == nameof(RunAsync) && a.GetGenericArguments().Length == 1);

                return((Task)method.MakeGenericMethod(argT1).Invoke(this, parameters: new object[] { tasq, cancellationToken }));
            }

            var tasqHandlerInstance = (TasqHandlerAsync)resolvedHandler.Handler;


            return(tasqHandlerInstance.InitializeAsync(tasq)
                   .ContinueWith(res1 =>
            {
                if (res1.IsFaulted)
                {
                    throw new TasqException(res1.Exception);
                }

                return tasqHandlerInstance.BeforeRunAsync(tasq)
                .ContinueWith(res2 =>
                {
                    if (res2.IsFaulted)
                    {
                        throw new TasqException(res2.Exception);
                    }

                    return tasqHandlerInstance.XRunAsync(null, tasq)
                    .ContinueWith(res3 =>
                    {
                        if (res3.IsFaulted)
                        {
                            throw new TasqException(res3.Exception);
                        }

                        return tasqHandlerInstance.AfterRunAsync(tasq)
                        .ContinueWith(res4 =>
                        {
                            if (res4.IsFaulted)
                            {
                                throw new TasqException(res4.Exception);
                            }

                            return res3;
                        }).Unwrap();
                    }).Unwrap();
                }).Unwrap();
            }).Unwrap());
        }