/// <summary> /// Gets or sets the value identified by the specified /// <paramref name="key"/>, which corresponds to the /// mapped database column name. /// </summary> /// <param name="key"></param> /// <returns></returns> public object this[string key] { get { foreach (var field_kvp in Mapping.FieldMappings) { DataModelColumnAttribute field = field_kvp.Value; if (field.ColumnName == null) { field.ColumnName = field_kvp.Key; } if (field.ColumnName.ToLower() == key.ToLower()) { return(DataModel[field_kvp.Key]); } } if (DataModel["field:" + key] != null) { return(DataModel["field:" + key]); } throw new ArgumentException("Field mapping not found"); } set { foreach (var field_kvp in Mapping.FieldMappings) { DataModelColumnAttribute field = field_kvp.Value; if (field.ColumnName.ToLower() == key.ToLower()) { DataModel[field_kvp.Key] = value; return; } } var attr = new DataModelColumnAttribute(key) { DbType = DbTypeConverter.ToDbType(value.GetType()) }; Mapping.FieldMappings.Add("field:" + key, attr); DataModel["field:" + key] = value; } }
private void LoadFieldMappingAttributes() { var hierarchy = new List <Type>(); var t = this.EntityType; while (t != typeof(DataModel) && t != typeof(object)) { hierarchy.Insert(0, t); t = t.BaseType; } // walk up hierarchy foreach (var type in hierarchy) { var pis = type.GetProperties(); var fis = type.GetFields(); var mis = new Dictionary <MemberInfo, Type>(); foreach (var fi in fis) { mis.Add(fi, fi.FieldType); // fee fi fo fum } foreach (var pi in pis) { mis.Add(pi, pi.PropertyType); } foreach (var mi_kvp in mis) { var mi = mi_kvp.Key; var miType = mi_kvp.Value; var attrs = mi.GetCustomAttributes(typeof(DataModelColumnAttribute), false); if (!TypeIsFieldMappable(miType)) { if (attrs.Length > 0) { throw new InvalidAttributeException("Cannot apply a " + typeof(DataModelColumnAttribute).Name + " to a member having a custom or complex type. " + "Use " + typeof(ForeignDataModelAttribute).Name + ".", mi.Name, (Attribute)attrs[0]); } continue; } foreach (DataModelColumnAttribute attr in attrs) { attr.TargetMember = mi; attr.TargetMemberType = miType; if (string.IsNullOrEmpty(attr.ColumnName)) { attr.ColumnName = mi.Name; attr.IsColumnNameInferred = true; } if (FieldMappings.ContainsKey(mi.Name) && !attr.ClearBaseObjectMapping) { attr.CopyDeltaTo(FieldMappings[mi.Name]); } else { FieldMappings[mi.Name] = attr; } var fm = FieldMappings[mi.Name]; if (fm.DataType == null) { var def = miType; Type[] genargs; if (def.IsGenericType && (genargs = def.GetGenericArguments()).Length == 1) { def = genargs[0]; } try { fm.DbType = DbTypeConverter.ToDbType(def); } catch { } } } } } if ((FieldMappings.Count == 0 && TableMapping.PropertyLoadBehavior == InferProperties.OnlyAttributedOrAllIfNoneHaveAttributes) || TableMapping.PropertyLoadBehavior == InferProperties.NotIgnored) { foreach (var type in hierarchy) { var pis = type.GetProperties(BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Instance); var fis = type.GetFields(BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Instance); var mis = new Dictionary <MemberInfo, Type>(); foreach (var fi in fis) { mis.Add(fi, fi.FieldType); // fee fi fo fum } foreach (var pi in pis) { mis.Add(pi, pi.PropertyType); } foreach (var mi_kvp in mis) { var mi = mi_kvp.Key; if (!FieldMappings.ContainsKey(mi.Name)) { var miType = mi_kvp.Value; var ignoreAttrib = mi.GetCustomAttributes(typeof(DataModelIgnoreAttribute), false); if (ignoreAttrib != null && ignoreAttrib.Length > 0) { continue; } var gt = miType; if (!TypeIsFieldMappable(gt)) { continue; } var attr = new DataModelColumnAttribute(mi.Name); attr.TargetMember = mi; attr.TargetMemberType = miType; FieldMappings[mi.Name] = attr; var fm = attr; if (fm.DataType == null) { var def = miType; Type[] genargs; if (def.IsGenericType && (genargs = def.GetGenericArguments()).Length == 1) { def = genargs[0]; } try { fm.DbType = DbTypeConverter.ToDbType(def); } catch { } } } } } } }