/// <summary> /// Initializes a new instance of the <see cref="EntityMapping"/> class. /// </summary> /// <param name="type">Type.</param> /// <param name="parentMapping">Parent mapping.</param> public EntityMapping(string type, Mapping parentMapping) { _Type = type; _Ids = new PrimaryKeyMappingCollection(); _Attributes = new AttributeMappingCollection(); _Attributes.TypeName = type; _References = new ReferenceMappingCollection(); _ParentMapping = parentMapping; }
private void ImportAttributes(IDataReader reader, string[] attributes, EntityMapping entityMapping, Entity entity, Hashtable columnAliasMapping) { if (attributes != null && _Model.GetInheritedAttributes(entity.Type).Count > 0) { ArrayList attrs = new ArrayList(attributes); StringCollection names = new StringCollection(); AttributeMappingCollection attributeMappings = new AttributeMappingCollection(); attributeMappings.TypeName = entityMapping.Type; // Retrieve attributes of the current entity type and all its sub classes attributes foreach (AttributeMapping a in entityMapping.Attributes) attributeMappings.Add(a); IList children = _Model.GetTree(entityMapping.Type); if (children != null && children.Count > 0) { foreach (Evaluant.Uss.Models.Entity e in children) { EntityMapping currentEM = _Mapping.Entities[e.Type]; if (currentEM == null) throw new SqlMapperException(string.Concat("Cannot find the entity ", e.Type, " in your mapping file")); foreach (Evaluant.Uss.Models.Attribute a in e.Attributes) { if (attributeMappings[a.Name] == null) if (currentEM.Attributes[a.Name] != null) attributeMappings.Add(currentEM.Attributes[a.Name]); } } } foreach (AttributeMapping am in attributeMappings) { string fullAttributeName = _Model.GetFullAttributeName(am.Name, am.ParentEntity.Type); // load only specified attributes ? if (((attrs.Count > 0) && !attrs.Contains(fullAttributeName))) continue; //Process generic Attributes string name = string.Empty; if (am.Discriminator != null && am.Discriminator != string.Empty) if (reader[am.Discriminator] != DBNull.Value) name = (string)reader[am.Discriminator]; if (columnAliasMapping != null && columnAliasMapping[fullAttributeName] == null) continue; object val = columnAliasMapping == null ? reader[fullAttributeName] : reader[columnAliasMapping[fullAttributeName].ToString()]; string type = (am.Discriminator != null) ? reader[am.Discriminator].ToString() : am.Name; if (((type != string.Empty) && !names.Contains(type)) && (type == am.Name)) { Type t = am.Type; Evaluant.Uss.Models.Entity current = _Model.GetEntity(entity.Type); if (t == null && name != null && name != string.Empty) t = _Model.GetAttribute(entity.Type, name).Type; string attributeName = am.Name; if (t == null && entity != null && _Model.GetAttribute(entity.Type, attributeName) != null) t = _Model.GetAttribute(entity.Type, attributeName).Type; if (current != null && t == null) { while (_Model.GetParent(current) != null) { current = _Model.GetParent(current); if (_Model.GetAttribute(current.Type, attributeName) != null) t = _Model.GetAttribute(current.Type, attributeName).Type; } } if (current != null && t == null) { foreach (Evaluant.Uss.Models.Entity child in _Model.GetTree(current.Type)) if (_Model.GetAttribute(child.Type, attributeName) != null) t = _Model.GetAttribute(child.Type, attributeName).Type; } if (((t == null) || (!t.IsValueType && (t != typeof(string)))) && (am.Name == "*")) { object obj2 = Utils.UnSerialize((string)val); t = obj2.GetType(); } SerializableType serializable = am.GetSerializableType(am.DbType, t); if (val != DBNull.Value) { object sValue = null; switch (serializable) { case SerializableType.BinarySerialization: sValue = Utils.UnSerialize((byte[])val); break; case SerializableType.StringSerialization: sValue = Utils.UnSerialize((string)val); t = val.GetType(); break; case SerializableType.Standard: #if !EUSS11 if ((t.IsGenericType && t.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))) { sValue = new System.ComponentModel.NullableConverter(t).ConvertTo(val, t.GetGenericArguments()[0]); } else #endif { if (am.DbType == DbType.String) val = ((string)val).Trim(); sValue = Convert.ChangeType(val, t, CultureInfo.InvariantCulture); } break; case SerializableType.String: if (val.GetType() == t) sValue = val; else sValue = Utils.StringToObject((string)val, t); break; case SerializableType.Int: val = Convert.ToInt32(val); if (t.IsEnum) sValue = Enum.ToObject(t, val); else sValue = Convert.ChangeType(val, t, CultureInfo.InvariantCulture); break; } if (t == null) t = typeof(object); if (entity.FindEntry(type) == null) entity.AddValue(type, sValue, t, State.UpToDate); } names.Add(type); entity.State = State.UpToDate; } } } }
/// <summary> /// Initializes a new instance of the <see cref="EntityMapping"/> class. /// </summary> public EntityMapping() { _Ids = new PrimaryKeyMappingCollection(); _Attributes = new AttributeMappingCollection(); _References = new ReferenceMappingCollection(); }