string AddProperty(Type entityType, ExposedField item) { var extraArgs = ""; var maxLength = 0; var type = item.GetPropertyType(); var name = item.GetName(); if (type.IsArray) { type = type.GetElementType(); } bool isNullable; if (isNullable = type.IsNullable()) { type = type.GetGenericArguments().Single(); } var method = type.Name.ToPascalCaseId(); if (item.IsAssociation) { method = "Associate" + "<" + type.Name + ">"; if (type.IsEnum) { method = "String"; } } if (item is ExposedPropertyInfo p && p.IsInverseAssociation) { type = type.GetGenericArguments().Single(); method = "InverseAssociate<" + type.Name + ">"; extraArgs += ", inverseOf: \"" + p.Property.GetCustomAttribute <InverseOfAttribute>()?.Association + "\""; } switch (method) { case "Boolean": method = "Bool"; break; case "Int32": method = "Int"; break; case "Int64": method = "Decimal"; break; case "String": maxLength = GetStringLength(item); if (maxLength == 0) { method = "BigString"; } break; default: break; } var result = method + $"(\"{name}\"{extraArgs})"; if (type.Assembly == Context.AssemblyObject && type.IsArray) { result += ".MaxCardinality(null)"; } if (!isNullable && type.IsValueType) { result += ".Mandatory()"; } var scale = ProjectCreator.TryGetMetadataValueFor <int>(entityType, name, "Scale"); if (scale != null) { result += $".Scale({scale})"; } if (maxLength > 0) { result += $".Max({maxLength})"; } return(result + ";"); }
int GetStringLength(ExposedField item) { var lengthAttribute = (item as ExposedPropertyInfo)?.Property?.GetCustomAttributes(typeof(StringLengthAttribute), true).FirstOrDefault(); return(lengthAttribute != null ? ((StringLengthAttribute)lengthAttribute).MaximumLength : 0); }