コード例 #1
0
ファイル: ObjectMapper.cs プロジェクト: epdumitru/mysqlib
        public virtual void Init(MappingSchema mappingSchema, Type type)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }

            _typeAccessor  = TypeAccessor.GetAccessor(type);
            _mappingSchema = mappingSchema;
            _extension     = TypeExtension.GetTypeExtension(
                _typeAccessor.OriginalType, mappingSchema.Extensions);

            foreach (MemberAccessor ma in _typeAccessor)
            {
                if (GetIgnore(ma))
                {
                    continue;
                }

                MapFieldAttribute mapFieldAttr = ma.GetAttribute <MapFieldAttribute>();

                if (mapFieldAttr == null || (mapFieldAttr.OrigName == null && mapFieldAttr.Format == null))
                {
                    MapMemberInfo mi = new MapMemberInfo();

                    mi.MemberAccessor  = ma;
                    mi.Type            = ma.Type;
                    mi.MappingSchema   = mappingSchema;
                    mi.MemberExtension = _extension[ma.Name];
                    mi.Name            = GetFieldName(ma);
                    mi.MemberName      = ma.Name;
                    mi.Trimmable       = GetTrimmable(ma);
                    mi.MapValues       = GetMapValues(ma);
                    mi.DefaultValue    = GetDefaultValue(ma);
                    mi.Nullable        = GetNullable(ma);
                    mi.NullValue       = GetNullValue(ma, mi.Nullable);

                    Add(CreateMemberMapper(mi));
                }
                else if (mapFieldAttr.OrigName != null)
                {
                    EnsureMapper(mapFieldAttr.MapName, ma.Name + "." + mapFieldAttr.OrigName);
                }
                else if (mapFieldAttr.Format != null)
                {
                    foreach (MemberMapper inner in _mappingSchema.GetObjectMapper(ma.Type))
                    {
                        EnsureMapper(string.Format(mapFieldAttr.Format, inner.Name), ma.Name + "." + inner.MemberName);
                    }
                }
            }

            foreach (AttributeExtension ae in _extension.Attributes["MapField"])
            {
                string mapName  = (string)ae["MapName"];
                string origName = (string)ae["OrigName"];

                if (mapName == null || origName == null)
                {
                    throw new MappingException(string.Format(
                                                   "Type '{0}' has invalid  extension. MapField MapName='{1}' OrigName='{2}'.",
                                                   type.FullName, mapName, origName));
                }

                EnsureMapper(mapName, origName);
            }

            MetadataProvider.EnsureMapper(this, EnsureMapper);
        }
コード例 #2
0
        public virtual void Init(MappingSchema mappingSchema, Type type)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }

            _typeAccessor  = TypeAccessor.GetAccessor(type);
            _mappingSchema = mappingSchema;
            _extension     = TypeExtension.GetTypeExtension(_typeAccessor.OriginalType, mappingSchema.Extensions);

            _inheritanceMapping.AddRange(GetInheritanceMapping());

            foreach (MemberAccessor ma in _typeAccessor)
            {
                Association a = GetAssociation(ma);

                if (a != null)
                {
                    _associations.Add(a);
                    continue;
                }

                if (GetMapIgnore(ma))
                {
                    continue;
                }

                MapFieldAttribute mapFieldAttr = ma.GetAttribute <MapFieldAttribute>();

                if (mapFieldAttr == null || (mapFieldAttr.OrigName == null && mapFieldAttr.Format == null))
                {
                    MapMemberInfo mi = new MapMemberInfo();

                    DbTypeAttribute dbTypeAttribute = ma.GetAttribute <DbTypeAttribute>();

                    if (dbTypeAttribute != null)
                    {
                        mi.DbType      = dbTypeAttribute.DbType;
                        mi.IsDbTypeSet = true;
                        if (dbTypeAttribute.Size != null)
                        {
                            mi.DbSize      = dbTypeAttribute.Size.Value;
                            mi.IsDbSizeSet = true;
                        }
                    }

                    mi.MemberAccessor             = ma;
                    mi.Type                       = ma.Type;
                    mi.MappingSchema              = mappingSchema;
                    mi.MemberExtension            = _extension[ma.Name];
                    mi.Name                       = GetFieldName(ma);
                    mi.MemberName                 = ma.Name;
                    mi.Storage                    = GetFieldStorage(ma);
                    mi.IsInheritanceDiscriminator = GetInheritanceDiscriminator(ma);
                    mi.Trimmable                  = GetTrimmable(ma);
                    mi.SqlIgnore                  = GetSqlIgnore(ma);
                    mi.MapValues                  = GetMapValues(ma);
                    mi.DefaultValue               = GetDefaultValue(ma);
                    mi.Nullable                   = GetNullable(ma);
                    mi.NullValue                  = GetNullValue(ma, mi.Nullable);

                    Add(CreateMemberMapper(mi));
                }
                else if (mapFieldAttr.OrigName != null)
                {
                    EnsureMapper(mapFieldAttr.MapName, ma.Name + "." + mapFieldAttr.OrigName);
                }
                else if (mapFieldAttr.Format != null)
                {
                    foreach (MemberMapper inner in _mappingSchema.GetObjectMapper(ma.Type))
                    {
                        EnsureMapper(string.Format(mapFieldAttr.Format, inner.Name), ma.Name + "." + inner.MemberName);
                    }
                }
            }

            foreach (AttributeExtension ae in _extension.Attributes["MapField"])
            {
                string mapName  = (string)ae["MapName"];
                string origName = (string)ae["OrigName"];

                if (mapName == null || origName == null)
                {
                    throw new MappingException(string.Format(
                                                   "Type '{0}' has invalid  extension. MapField MapName='{1}' OrigName='{2}'.",
                                                   type.FullName, mapName, origName));
                }

                EnsureMapper(mapName, origName);
            }

            MetadataProvider.EnsureMapper(TypeAccessor, MappingSchema, EnsureMapper);
        }