Example #1
0
        /// <summary>
        /// for the given dependency, gets its tree, sorted from least dependent to most
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public List <NamedDependencyWithObjectInstance> GetDependencyTree(NamedDependencyWithObjectInstance item)
        {
            var castedList = this.Items.Cast <NamedDependency>().ToList();
            var list       = NamedDependency.GetSortedDependencies(castedList, item);

            return(list.Cast <NamedDependencyWithObjectInstance>().ToList());
        }
Example #2
0
        /// <summary>
        /// for all the contained dependencies, sorts and returns them
        /// </summary>
        /// <returns></returns>
        public List <NamedDependencyWithObjectInstance> GetTotalDependencyOrder()
        {
            var castedList = this.Items.Cast <NamedDependency>().ToList();
            List <NamedDependency> list = NamedDependency.Sort(castedList);

            return(list.Cast <NamedDependencyWithObjectInstance>().ToList());
        }
Example #3
0
        /// <summary>
        /// adds a dependency item. will not add if item with the same "Self" already exists.
        /// </summary>
        /// <param name="instance"></param>
        /// <param name="dependency"></param>
        public bool AddDependency(object instance, NamedDependency dependency)
        {
            NamedDependencyWithObjectInstance item = new NamedDependencyWithObjectInstance(instance, dependency);

            lock (this._stateLock)
            {
                if (!this.Items.Exists(x => x.Self == item.Self))
                {
                    this.Items.Add(item);
                    return(true);
                }
            }
            return(false);
        }
Example #4
0
            public NamedDependencyWithObjectInstance(object instance, NamedDependency dependency)
                : base()
            {
                this.Instance = instance;

                if (dependency == null)
                {
                    this.Self = Guid.NewGuid().ToString();
                    this.IsKeyAutogenerated = true;
                }
                else
                {
                    this.IsKeyAutogenerated = false;

                    this.Classification = dependency.Classification;
                    this.Self           = dependency.Self;
                    this.Prerequisites  = dependency.Prerequisites;
                }
            }
        /// <summary>
        /// helper method.  constructs a NamedDependencyNode instance by examining the type's
        /// NamedDependencyAttributes.
        /// </summary>
        /// <param name="type"></param>
        /// <param name="classification"></param>
        /// <returns></returns>
        private static NamedDependency BuildNamedDependencyFromTypeAttributes(Type type, string classification)
        {
            Condition.Requires(type).IsNotNull();
            Condition.Requires(classification).IsNotNullOrEmpty();

            //a dependency is indicated by decoration with NamedDependencyAttribute
            NamedDependency returnValue = null;

            try
            {
                //find the attribute
                var attr = type.GetCustomAttributes <NamedDependencyAttribute>().SingleOrDefault(x => x.NamedDependency.Classification.Equals(classification));
                if (attr != null)
                {
                    returnValue = attr.NamedDependency;
                }
            }
            catch { }
            return(returnValue);
        }
Example #6
0
        /// <summary>
        /// with a list of dependencies and a dependency,return all it depends on, sorted from least dependent to most.
        /// Note: it just returns the exact dependency tree for the rootDep, and does not include any dependencies that
        /// are less dependant within the provided set.
        /// </summary>
        /// <param name="alldeps"></param>
        /// <param name="rootDep"></param>
        /// <returns></returns>
        public static List <NamedDependency> GetSortedDependencies(List <NamedDependency> alldeps, NamedDependency rootDep)
        {
            //if we have no dependencies, carry on
            if (rootDep.Prerequisites == null || rootDep.Prerequisites.Count == 0)
            {
                return(null);
            }

            //define strategy to find dependencies with the provided names
            Func <List <string>, List <NamedDependency> > lookupDeps = (depNames) =>
            {
                return(alldeps.Filter((nd) =>
                {
                    return depNames.Contains(nd.Self);
                }));
            };

            //walk the dependency starting at the root dependency
            List <NamedDependency> depTree  = new List <NamedDependency>();      //tree to build
            List <NamedDependency> iterDeps = lookupDeps(rootDep.Prerequisites); //temp iterator

            while (iterDeps != null && iterDeps.Count > 0)
            {
                List <string> nextDeps = new List <string>();

                iterDeps.WithEach(dep =>
                {
                    //add to tree if it hasn't happened
                    if (depTree.Exists(x => x.Self == dep.Self))
                    {
                        depTree.Add(dep);

                        //aggregate the next deps to iterate on
                        nextDeps.AddRange(dep.Prerequisites);
                    }
                });

                //iterate
                iterDeps = lookupDeps(nextDeps);
            }

            //now sort this limited set using the regular sort routine
            var sortedDeps = Sort(depTree);

            //build the return list
            List <NamedDependency> returnValue = new List <NamedDependency>();

            sortedDeps.WithEach(key =>
            {
                var depInstance = depTree.SingleOrDefault(x => x.Self == key.Self);
                if (depInstance != null)
                {
                    returnValue.Add(depInstance);
                }
            });
            return(returnValue);
        }