public ObjectBuilderCallBackHandler(
            Tasks.ITaskRunner taskRunner,
            ObjectBuildingInfo <TAddress> objectBuilderInfo,
            IEnumerable <Tuple <ObjectBuilders.IDependency <TAddress>, IObjectBuildingInfo <TAddress> > > dependencies)
        {
            if (taskRunner == null)
            {
                throw new ArgumentNullException(nameof(taskRunner));
            }

            if (dependencies == null)
            {
                dependencies = new Tuple <ObjectBuilders.IDependency <TAddress>, IObjectBuildingInfo <TAddress> > [0];
            }

            this.TaskRunner        = taskRunner;
            this.Dependencies      = new List <Tuple <ObjectBuilders.IDependency <TAddress>, IObjectBuildingInfo <TAddress> > >(dependencies);
            this.ObjectBuilderInfo = objectBuilderInfo;

            this._outstandingDependencies = this.Dependencies.Count;
            if (_outstandingDependencies == 0)
            {
                this.RunBuilder();
            }
        }
        public ObjectBuilderRecursiveDependenciesCallBackHandler(ObjectBuildingInfo <TAddress> objectBuildingInfo,
                                                                 IEnumerable <Tuple <ObjectBuilders.IDependency <TAddress>, IObjectBuildingInfo <TAddress> > > dependencies)
        {
            if (dependencies == null)
            {
                dependencies = new Tuple <ObjectBuilders.IDependency <TAddress>, IObjectBuildingInfo <TAddress> > [0];
            }

            this.Dependencies       = new List <Tuple <ObjectBuilders.IDependency <TAddress>, IObjectBuildingInfo <TAddress> > >(dependencies);
            this.ObjectBuildingInfo = objectBuildingInfo;

            this._outstandingDependencies = this.Dependencies.Count;
            if (_outstandingDependencies == 0)
            {
                this.RunGetAdditionalDependencies();
            }
        }
Exemple #3
0
        private ObjectBuildingInfo <TAddress> GetDependenciesInt(TAddress address)
        {
            if (address == null)
            {
                throw new ArgumentNullException(nameof(address));
            }

            // Check to see if we've been asked to build this before, if so, simply return
            ObjectBuildingInfo <TAddress> objectBuildingInfo = null;

            lock (this._objectBuildingInfo)
            {
                if (_objectBuildingInfo.TryGetValue(address, out objectBuildingInfo))
                {
                    return(objectBuildingInfo);
                }

                objectBuildingInfo            = new ObjectBuildingInfo <TAddress>(this, address);
                _objectBuildingInfo [address] = objectBuildingInfo;
            }

            // At this stage, we're now responsible for the population of this chap...
            if (this.ObjectBuilderProvider.TryGetObjectBuilder(address, out var objectBuilder))
            {
                try
                {
                    objectBuildingInfo.SetObjectBuilder(objectBuilder);

                    if (objectBuilder is ObjectBuilders.IObjectBuilderAsynchronousDependencies <TAddress> &&
                        ((ObjectBuilders.IObjectBuilderAsynchronousDependencies <TAddress>)objectBuilder).GetDependenciesAsynchronously(address))
                    {
                        // Create a task to do discovery and then to perform the
                        var job = new Tasks.ObjectBuilderGetDependenciesTask <TAddress>(this,
                                                                                        objectBuildingInfo,
                                                                                        address);

                        this.TaskRunner.RegisterTask(job);
                    }
                    else
                    {
                        var context      = new ObjectBuilders.ObjectBuilderGetDependenciesContext <TAddress>(this);
                        var dependencies = objectBuilder.GetDependencies(context, address);

                        if (dependencies != null && dependencies.RecursiveMode)
                        {
                            var dependencyTuples = new List <Tuple <ObjectBuilders.IDependency <TAddress>, IObjectBuildingInfo <TAddress> > >();
                            // Recursive mode
                            foreach (var dependency in dependencies.Dependencies)
                            {
                                var dependencyObjectBuildingInfo = this.BuildObject(dependency.Address);
                                var tuple = Tuple.Create(dependency, dependencyObjectBuildingInfo);
                                dependencyTuples.Add(tuple);
                            }

                            var callBackHandler = new ObjectBuilderRecursiveDependenciesCallBackHandler <TAddress>(
                                objectBuildingInfo,
                                dependencyTuples);

                            foreach (var tuple in dependencyTuples)
                            {
                                tuple.Item2.RegisterPostBuildCallBack(callBackHandler.PostDependencyBuildCallBack);
                            }
                        }
                        else
                        {
                            var requestedDependencies = new RequestedDependencies <TAddress>(
                                false,
                                dependencies == null ? null : dependencies.Dependencies);

                            objectBuildingInfo.SetRequestedDependencies(requestedDependencies);
                        }
                    }
                }
                catch (Exception ex)
                {
                    objectBuildingInfo.SetObjectFailed(ex);
                }
                return(objectBuildingInfo);
            }
            else
            {
                // This needs to be extended to ask for items from the child context...
                var parentContext = (ObjectContext <TAddress>) this.ParentContext;
                if (parentContext == null)
                {
                    objectBuildingInfo.SetNoBuilderFound();
                    return(objectBuildingInfo);
                }

                var parentContextObjectBuildingInfo = parentContext.GetDependencies(address);

                parentContextObjectBuildingInfo.RegisterPostDependenciesKnownCallBack((x) =>
                {
                    if (parentContextObjectBuildingInfo.ObjectBuildingState == ObjectBuildingStates.Failure)
                    {
                        objectBuildingInfo.SetObjectFailed(new Exception("Parent dependencies failed"));
                        return;
                    }

                    objectBuildingInfo.SetObjectBuilder(parentContextObjectBuildingInfo.ObjectBuilder);
                    objectBuildingInfo.SetRequestedDependencies(parentContextObjectBuildingInfo.RequestedDependencies);
                });
                return(objectBuildingInfo);
            }
        }