Esempio n. 1
0
        private static IEnumerable <MemberMapInfo> GetMembersMapInfo(Type type, TypeMapInfo typeMapInfo)
        {
#if PCL
            var members = type.GetAllMembers().Where(x => !x.IsStatic());
#else
            var properties = type.GetProperties(BindingFlags.Instance | BindingFlags.Public);
            var fields     = type.GetFields(BindingFlags.Instance | BindingFlags.Public);
            var members    = properties.Cast <MemberInfo>().Union(fields);
#endif
            foreach (var memberInfo in members)
            {
#if PCL
                if (memberInfo.IsDefined(typeof(IgnoreAttribute)))
                {
                    continue;
                }
#else
                if (Attribute.IsDefined(memberInfo, typeof(IgnoreAttribute)))
                {
                    continue;
                }
#endif

                if (!(memberInfo is FieldInfo) &&
                    !(memberInfo is PropertyInfo))
                {
                    continue;
                }

                yield return(GetMemberMapInfo(typeMapInfo, memberInfo));
            }
        }
Esempio n. 2
0
        public static TypeMapInfo GetMapInfo(Type type)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }

            var tableName   = GetTableName(type);
            var typeMapInfo = new TypeMapInfo(type, tableName);

            var memberMapInfo = GetMembersMapInfo(type, typeMapInfo);

            foreach (var mapInfo in memberMapInfo)
            {
                typeMapInfo.AddMember(mapInfo);
            }

            return(typeMapInfo);
        }
Esempio n. 3
0
        private static MemberMapInfo GetMemberMapInfo(TypeMapInfo typeMapInfo, MemberInfo memberInfo)
        {
            string  columnName = memberInfo.Name;
            SqlType columnType = null;

            bool canBeNull = CanBeNull(memberInfo);
            bool nullable  = canBeNull;

            object defaultValue        = null;
            bool   defaultIsExpression = false;

#if PCL
            if (memberInfo.IsDefined(typeof(ColumnAttribute)))
            {
                var attribute = memberInfo.GetCustomAttribute <ColumnAttribute>();
#else
            if (Attribute.IsDefined(memberInfo, typeof(ColumnAttribute)))
            {
                var attribute = (ColumnAttribute)Attribute.GetCustomAttribute(memberInfo, typeof(ColumnAttribute));
#endif
                if (!String.IsNullOrEmpty(attribute.Name))
                {
                    columnName = attribute.Name;
                }

                if (!String.IsNullOrEmpty(attribute.TypeName))
                {
                    if (!PrimitiveTypes.IsPrimitive(attribute.TypeName))
                    {
                        throw new NotSupportedException(String.Format("The type '{0}' is not supported.", attribute.TypeName));
                    }

                    columnType = SqlType.Parse(attribute.TypeName);
                }

                defaultValue        = attribute.Default;
                defaultIsExpression = attribute.DefaultIsExpression;

                nullable = attribute.Null;
            }

            if (nullable && !canBeNull)
            {
                throw new NotSupportedException(String.Format("The member '{0}' is marked as NULL but is not nullable.", memberInfo.Name));
            }

            if (columnType == null)
            {
                columnType = GetSqlType(memberInfo);
            }

            ConstraintMapInfo[] constraints = null;
#if PCL
            if (memberInfo.IsDefined(typeof(ConstraintAttribute)))
            {
                var attributes = memberInfo.GetCustomAttributes <ConstraintAttribute>();
#else
            if (Attribute.IsDefined(memberInfo, typeof(ConstraintAttribute)))
            {
                var attributes = (ConstraintAttribute[])Attribute.GetCustomAttributes(memberInfo, typeof(ConstraintAttribute));
#endif
                constraints = attributes.Select(x => new ConstraintMapInfo(memberInfo, columnName, x.Type, x.Expression)).ToArray();
            }

#if PCL
            if (memberInfo.IsDefined(typeof(IdentityAttribute)))
            {
#else
            if (Attribute.IsDefined(memberInfo, typeof(IdentityAttribute)))
            {
#endif
                if (constraints != null &&
                    constraints.Length > 0)
                {
                    throw new InvalidOperationException("Cannot specify an identity for a column that already defines a constraint.");
                }

                if (defaultValue != null)
                {
                    throw new InvalidOperationException("Cannot specify an identity for a column that defines a default expression.");
                }

                constraints         = new[] { new ConstraintMapInfo(memberInfo, columnName, ConstraintType.PrimaryKey, null) };
                defaultValue        = String.Format("UNIQUEKEY('{0}')", typeMapInfo.TableName);
                defaultIsExpression = true;
            }

            var mapInfo = new MemberMapInfo(memberInfo, columnName, columnType, nullable, constraints);
            if (defaultValue != null)
            {
                mapInfo.SetDefault(defaultValue, defaultIsExpression);
            }
            return(mapInfo);
        }