示例#1
0
        public void InheritanceTree_Test()
        {
            ModelRoot modelRoot = new ModelRoot();

            Interface I00 = new Interface(modelRoot, "I00");
            Interface I01 = new Interface(modelRoot, "I01");
            Interface I02 = new Interface(modelRoot, "I02");
            Interface I03 = new Interface(modelRoot, "I03");
            Interface I04 = new Interface(modelRoot, "I04");
            Interface I05 = new Interface(modelRoot, "I05");
            Entity    C01 = new Entity(modelRoot, "C01");
            Entity    C02 = new Entity(modelRoot, "C02");
            Entity    C03 = new Entity(modelRoot, "C03");

            // assign inheritance
            I02.SetInheritance(I01);

            I04.SetInheritance(I01);

            I03.SetInheritance(I02, I04);

            I05.SetInheritance(I04, I02);

            C01.SetInheritance(I03, I05, I00);

            C02.SetInheritance(C01);

            //C03.SetInheritance(I00);

            // test case #1
            var inheritanceTrees = InheritanceTree.Create(C01, C02, C03);

            InheritanceTree.RebuildTree(true, inheritanceTrees.ToArray());
            InheritanceTree C01_inheritanceTree = inheritanceTrees[0];
            //C01_inheritanceTree.RebuildTree(true);

            ReadOnlyCollection <InheritanceNode> flatList       = C01_inheritanceTree.GetFlatList(InheritanceListMode.CurrentLevel);
            IEnumerable <IInterface>             flatInterfaces = flatList.Select(node => node.Interface);

            Assert.IsTrue(flatInterfaces.Contains(I03));
            Assert.IsTrue(flatInterfaces.Contains(I05));
            Assert.IsTrue(flatInterfaces.Contains(I00));

            // test case #2
            //inheritanceTrees[1].RebuildTree(true);
            var mergedPaths = InheritanceTree.MergePaths(inheritanceTrees).ToArray();
        }
示例#2
0
    public string[] InheritanceList(IPersistentType persistentType)
    {
        List <string> result = null;

        if (persistentType is ITypedEntitySet)
        {
            ITypedEntitySet typedEntitySet = (ITypedEntitySet)persistentType;
            result = new List <string> {
                BuildXtensiveType(OrmType.EntitySet, EscapeNameWithNamespace(typedEntitySet.ItemType, persistentType))
            };
            return(result.ToArray());
        }

        IInterface      @interface            = persistentType as IInterface;
        string          entityBaseBaseTypeStr = null;
        InheritanceTree inheritanceTree       = null;

        if (@interface != null)
        {
            inheritanceTree = InheritanceTreeCache.Get(@interface);
            if (!inheritanceTree.TreeRebuilded)
            {
                inheritanceTree.RebuildTree(false);
            }

            IInterface entityBaseBaseType = null;
            if (@interface is IEntityBase)
            {
                IEntityBase entityBase = (IEntityBase)@interface;
                entityBaseBaseType    = entityBase.BaseType;
                entityBaseBaseTypeStr = entityBase.BaseType == null
                                            ? null
                                            : EscapeNameWithNamespace(entityBase.BaseType, persistentType);
            }

            ReadOnlyCollection <InheritanceNode> flatList           = inheritanceTree.GetFlatList(InheritanceListMode.CurrentLevel);
            IEnumerable <IInterface>             allInheritanceList = flatList.Select(node => node.Interface).Where(item => item != entityBaseBaseType);

            result = allInheritanceList.Select(item => EscapeNameWithNamespace(item, persistentType)).ToList();
        }

        string commonBaseType = persistentType.TypeKind == PersistentTypeKind.Structure
                                    ? BuildXtensiveType(OrmType.Structure)
                                    : BuildXtensiveType(OrmType.Entity);

        bool noInheritance = (result == null || result.Count == 0 && persistentType.TypeKind != PersistentTypeKind.Interface) && string.IsNullOrEmpty(entityBaseBaseTypeStr);

        if (noInheritance)
        {
            result = new List <string>();
            if (!this.ActiveDTOStage)
            {
                result.Add(commonBaseType);
            }
        }
        else
        {
            if (persistentType.TypeKind.In(PersistentTypeKind.Entity, PersistentTypeKind.Structure))
            {
                if (@interface.InheritingByInterfaces.Count == 0 && string.IsNullOrEmpty(entityBaseBaseTypeStr) && !this.ActiveDTOStage)
                {
                    result.Insert(0, commonBaseType);
                }
            }
            else if (persistentType.TypeKind == PersistentTypeKind.Interface)
            {
                bool inheritesCommonIEntity = @interface.InheritsIEntity == InheritsIEntityMode.AlwaysInherit;
                if (!inheritesCommonIEntity)
                {
                    inheritesCommonIEntity = @interface.InheritedInterfaces.Count == 0;
                }

                if (inheritesCommonIEntity && !this.ActiveDTOStage)
                {
                    string commonIEntityType = BuildXtensiveType(OrmType.IEntity);
                    result.Insert(0, commonIEntityType);
                }
            }
        }

        if (!string.IsNullOrEmpty(entityBaseBaseTypeStr))
        {
            result.Insert(0, entityBaseBaseTypeStr);
        }

        return(result.ToArray());
    }