Ejemplo n.º 1
0
        public IActivity Build(IBuildContext context)
        {
            if (!OptionSet)
            {
                throw new ActivityBuildException("No valid option was set for the build of an activity");
            }

            try {
                IActivity activity;

                if (ActivityType != null)
                {
                    if (context == null)
                    {
                        throw new ActivityResolveException(ActivityType, "The builder references a type that cannot be resolved outside a context");
                    }

                    activity = (IActivity)context.Resolve(ActivityType);

                    if (activity == null)
                    {
                        throw new ActivityResolveException(ActivityType, $"The type '{ActivityType}' is not a valid instance of {typeof(IActivity)}");
                    }
                }
                else if (ProxyActivity != null)
                {
                    activity = ProxyActivity;
                }
                else if (ActivityBranchBuilder != null)
                {
                    activity = ActivityBranchBuilder.Build(context);
                }
                else
                {
                    activity = new Activity(Name, Decision, Execution);

                    if (Metadata != null)
                    {
                        ((Activity)activity).SetMetadata(Metadata);
                    }
                }

                if (Factory)
                {
                    if (StateFactory == null &&
                        StateFactoryType == null)
                    {
                        throw new InvalidOperationException("Invalid setup for a state factory");
                    }

                    var factory = StateFactory;
                    if (factory == null && StateFactoryType != null)
                    {
                        factory = context.Resolve(StateFactoryType) as IStateFactory;
                    }

                    activity = new ActivityFactoryActivity(Name, Decision, activity, factory);
                }

                return(activity);
            } catch (ActivityBuildException) {
                throw;
            } catch (Exception ex) {
                throw new ActivityBuildException("Could not build the activity because of an error: see inner exception for details.", ex);
            }
        }