コード例 #1
0
        private IDataEntity ResolveEntity(XmlReader reader, MetadataFile provider, string @namespace, Action unrecognize)
        {
            //创建实体元素对象
            var entity = new MetadataEntity(provider,
                                            this.GetFullName(reader.GetAttribute(XML_NAME_ATTRIBUTE), @namespace),
                                            this.GetFullName(reader.GetAttribute(XML_INHERITS_ATTRIBUTE), @namespace),
                                            this.GetAttributeValue(reader, XML_IMMUTABLE_ATTRIBUTE, false));

            //设置其他属性
            entity.Alias = reader.GetAttribute(XML_ALIAS_ATTRIBUTE) ?? reader.GetAttribute(XML_TABLE_ATTRIBUTE);

            var keys  = new HashSet <string>();
            int depth = reader.Depth;

            while (reader.Read() && reader.Depth > depth)
            {
                if (reader.NodeType != XmlNodeType.Element)
                {
                    continue;
                }

                switch (reader.LocalName)
                {
                case XML_KEY_ELEMENT:
                    while (reader.Read() && reader.Depth > depth + 1)
                    {
                        if (reader.NodeType != XmlNodeType.Element)
                        {
                            continue;
                        }

                        if (reader.Name == XML_MEMBER_ELEMENT)
                        {
                            keys.Add(reader.GetAttribute(XML_NAME_ATTRIBUTE));
                        }
                        else
                        {
                            unrecognize();
                        }
                    }

                    break;

                case XML_PROPERTY_ELEMENT:
                    var property = new MetadataEntitySimplexProperty(entity,
                                                                     reader.GetAttribute(XML_NAME_ATTRIBUTE),
                                                                     this.GetFieldType(this.GetAttributeValue <string>(reader, XML_TYPE_ATTRIBUTE)),
                                                                     this.GetAttributeValue(reader, XML_IMMUTABLE_ATTRIBUTE, false))
                    {
                        Alias     = this.GetAttributeValue <string>(reader, XML_ALIAS_ATTRIBUTE) ?? this.GetAttributeValue <string>(reader, XML_FIELD_ATTRIBUTE),
                        Length    = this.GetAttributeValue <int>(reader, XML_LENGTH_ATTRIBUTE),
                        Precision = this.GetAttributeValue <byte>(reader, XML_PRECISION_ATTRIBUTE),
                        Scale     = this.GetAttributeValue <byte>(reader, XML_SCALE_ATTRIBUTE),
                        Nullable  = this.GetAttributeValue <bool>(reader, XML_NULLABLE_ATTRIBUTE, true),
                        Sortable  = this.GetAttributeValue <bool>(reader, XML_SORTABLE_ATTRIBUTE, false),
                    };

                    //设置默认值的字面量
                    property.SetDefaultValue(this.GetAttributeValue <string>(reader, XML_DEFAULT_ATTRIBUTE));

                    //设置序号器元数据信息
                    property.SetSequence(this.GetAttributeValue <string>(reader, XML_SEQUENCE_ATTRIBUTE));

                    //将解析成功的属性元素加入到实体的属性集合
                    entity.Properties.Add(property);

                    break;

                case XML_COMPLEXPROPERTY_ELEMENT:
                    var complexProperty = new MetadataEntityComplexProperty(entity,
                                                                            reader.GetAttribute(XML_NAME_ATTRIBUTE),
                                                                            this.GetRoleName(reader.GetAttribute(XML_ROLE_ATTRIBUTE), @namespace),
                                                                            this.GetAttributeValue(reader, XML_IMMUTABLE_ATTRIBUTE, false));

                    var multiplicity = reader.GetAttribute(XML_MULTIPLICITY_ATTRIBUTE);

                    if (multiplicity != null && multiplicity.Length > 0)
                    {
                        switch (multiplicity)
                        {
                        case "*":
                            complexProperty.Multiplicity = DataAssociationMultiplicity.Many;
                            break;

                        case "1":
                        case "!":
                            complexProperty.Multiplicity = DataAssociationMultiplicity.One;
                            break;

                        case "?":
                        case "0..1":
                            complexProperty.Multiplicity = DataAssociationMultiplicity.ZeroOrOne;
                            break;

                        default:
                            throw new DataException($"Invalid '{multiplicity}' value of the multiplicity attribute.");
                        }
                    }

                    var links = new List <DataAssociationLink>();

                    while (reader.Read() && reader.Depth > depth + 1)
                    {
                        if (reader.NodeType != XmlNodeType.Element)
                        {
                            continue;
                        }

                        if (reader.LocalName == XML_LINK_ELEMENT)
                        {
                            links.Add(
                                new DataAssociationLink(complexProperty,
                                                        this.GetAttributeValue <string>(reader, XML_NAME_ATTRIBUTE),
                                                        this.GetAttributeValue <string>(reader, XML_ROLE_ATTRIBUTE)));
                        }
                        else if (reader.LocalName == XML_CONSTRAINTS_ELEMENT)
                        {
                            if (reader.IsEmptyElement)
                            {
                                continue;
                            }

                            var constraints = new List <DataAssociationConstraint>();

                            while (reader.Read() && reader.Depth > depth + 2)
                            {
                                if (reader.NodeType != XmlNodeType.Element)
                                {
                                    continue;
                                }

                                if (reader.LocalName == XML_CONSTRAINT_ELEMENT)
                                {
                                    constraints.Add(
                                        new DataAssociationConstraint(
                                            this.GetAttributeValue <string>(reader, XML_NAME_ATTRIBUTE),
                                            this.GetAttributeValue(reader, XML_ACTOR_ATTRIBUTE, DataAssociationConstraintActor.Principal),
                                            this.GetAttributeValue <object>(reader, XML_VALUE_ATTRIBUTE)));
                                }
                                else
                                {
                                    unrecognize();
                                }
                            }

                            if (constraints.Count > 0)
                            {
                                complexProperty.Constraints = constraints.ToArray();
                            }
                        }
                        else
                        {
                            unrecognize();
                        }
                    }

                    if (links == null || links.Count == 0)
                    {
                        throw new DataException($"Missing links of the '{complexProperty.Name}' complex property in the '{provider.FilePath}' mapping file.");
                    }

                    //设置复合属性的链接集属性
                    complexProperty.Links = links.ToArray();

                    //将解析成功的属性元素加入到实体的属性集合
                    entity.Properties.Add(complexProperty);

                    break;

                default:
                    unrecognize();
                    break;
                }
            }

            //确认实体的主键信息
            if (keys.Count > 0)
            {
                var array = new IDataEntitySimplexProperty[keys.Count];
                var index = 0;

                foreach (var key in keys)
                {
                    if (!entity.Properties.TryGet(key, out var property))
                    {
                        throw new MetadataFileException($"The '{key}' primary key in the '{entity.Name}' entity is undefined.");
                    }
                    if (property.IsComplex)
                    {
                        throw new MetadataFileException($"The '{key}' primary key in the '{entity.Name}' entity cannot be a complex(navigation) property.");
                    }

                    //将主键属性的是否主键开关打开
                    ((MetadataEntitySimplexProperty)property).SetPrimaryKey();

                    array[index++] = (IDataEntitySimplexProperty)property;
                }

                entity.Key = array;
            }

            return(entity);
        }
コード例 #2
0
 public MetadataEntitySimplexProperty(MetadataEntity entity, string name, System.Data.DbType type, bool immutable = false) : base(entity, name, immutable)
 {
     this.Type = type;
 }
コード例 #3
0
 public MetadataEntitySimplexProperty(MetadataEntity entity, string name, System.Data.DbType type) : base(entity, name, type)
 {
 }