Ejemplo n.º 1
0
            /// <summary>
            /// Gets the field mapping used to resolve this field reference.
            /// </summary>
            public DataModelColumnAttribute GetFieldMapping(Type context)
            {
                var fmaps = DataModelMap.GetEntityMapping(context).FieldMappings;

                if (FindFieldMappingKeyType == Data.FieldMappingKeyType.ClrMember)
                {
                    return(fmaps[FieldName]);
                }
                foreach (var fld in fmaps)
                {
                    if (fld.Value.ColumnName == FieldName)
                    {
                        return(fld.Value);
                    }
                }
                return(null);
            }
Ejemplo n.º 2
0
        /// <summary>
        /// Prepares the
        /// data to propagate to or from the <see cref="Entity"/> before
        /// the Entity is retrieved or before data changes are made and/or saved.
        /// </summary>
        /// <param name="syncTo"></param>
        public override void SynchronizeFields(SyncTo syncTo)
        {
            if (Type.IsDataModel() &&
                Type == GetUnwrappedType(Type))
            {
                return;
            }
            switch (syncTo)
            {
            case SyncTo.FieldMappedData:
                foreach (var field_kvp in EntityMappings.FieldMappings)
                {
                    DataModelColumnAttribute field = field_kvp.Value;

                    // todo: optimize with cached Action<T>
                    // see: http://msmvps.com/blogs/jon_skeet/archive/2008/08/09/making-reflection-fly-and-exploring-delegates.aspx

                    if (field.TargetMember is FieldInfo)
                    {
                        object val  = ((FieldInfo)field.TargetMember).GetValue(Entity);
                        object dval = this[field.TargetMember.Name];
                        if (((dval == null) != (val == null)) ||
                            (dval != null && !dval.Equals(val)))
                        {
                            this[field.TargetMember.Name] = val;
                        }
                    }
                    else if (field.TargetMember is PropertyInfo)
                    {
                        object val = ((PropertyInfo)field.TargetMember).GetValue(Entity, new object[] {});
                        if (val != null && !DataModelMap.TypeIsFieldMappable(val.GetType()))
                        {
                            if (DataModelMap.MapItems.ContainsKey(val.GetType()) &&
                                DataModelMap.GetEntityMapping(val.GetType()).PrimaryKeyColumns.Length == 1)
                            {
                                var map = DataModelMap.GetEntityMapping(val.GetType());
                                if (val is DataModel)
                                {
                                    val =
                                        ((DataModel)val).ColumnMappedValue[
                                            ((DataModel)val).EntityMappings.PrimaryKeyColumns[0]];
                                }
                                else
                                {
                                    val = val.GetType().GetProperty(
                                        map.PrimaryKeyColumns[0]).GetValue(val, new object[] { });
                                }
                            }
                        }
                        object dval = InnerData.ContainsKey(field.TargetMember.Name)
                                ? this[field.TargetMember.Name]
                                : null;
                        if (((dval == null) != (val == null)) ||
                            (dval != null && !dval.Equals(val)))
                        {
                            if (this.ModelData.ContainsKey(field.TargetMember.Name))
                            {
                                this[field.TargetMember.Name] = val;
                            }
                            else
                            {
                                var mapping = EntityMappings[field.TargetMember.Name];
                                if (mapping != null)
                                {
                                    var name = mapping.TargetMember.Name;
                                    if (this.ModelData.ContainsKey(name))
                                    {
                                        this[name] = val;
                                    }
                                    else if (this.ModelData.ContainsKey("field:" + mapping.ColumnName))
                                    {
                                        this["field:" + mapping.ColumnName] = val;
                                    }
                                }
                            }
                        }
                    }
                }
                break;

            case SyncTo.ClrMembers:
                foreach (var field_kvp in EntityMappings.FieldMappings)
                {
                    DataModelColumnAttribute field = field_kvp.Value;

                    // todo: optimize with cached Action<T>
                    // see: http://msmvps.com/blogs/jon_skeet/archive/2008/08/09/making-reflection-fly-and-exploring-delegates.aspx

                    if (field.TargetMember is FieldInfo)
                    {
                        object clrval = ((FieldInfo)field.TargetMember).GetValue(Entity);
                        object dicval = this[field.TargetMember.Name];
                        if (((clrval == null) != (dicval == null)) ||
                            (dicval != null && !dicval.Equals(clrval)))
                        {
                            ((FieldInfo)field.TargetMember).SetValue(
                                Entity, this[field.TargetMember.Name]);
                        }
                    }
                    else if (field.TargetMember is PropertyInfo)
                    {
                        if (DataModelMap.TypeIsFieldMappable(field.TargetMemberType))
                        {
                            object propval = ((PropertyInfo)field.TargetMember).GetValue(Entity, new object[] {});
                            object dicval  = this[field.TargetMember.Name];
                            if (dicval == DBNull.Value)
                            {
                                dicval = null;
                            }
                            if (((dicval == null) != (propval == null)) ||
                                (dicval != null && !dicval.Equals(propval)))
                            {
                                ((PropertyInfo)field.TargetMember).SetValue(
                                    Entity, dicval, new object[] {});
                            }
                        }
                        else
                        {
                            // todo maybe: assign loaded entity?
                        }
                    }
                }
                break;
            }
        }
Ejemplo n.º 3
0
 private static DataModelMap GetMapping(Type t)
 {
     return(DataModelMap.GetEntityMapping(t));
 }
Ejemplo n.º 4
0
 internal ColumnMappedValueProperty(DataModel dataModel, DataModelMap mapping)
 {
     DataModel = dataModel;
     Mapping   = mapping;
 }