public static MetadataPropertyType ToProperty(this ParameterInfo pi) { var propertyAttrs = pi.AllAttributes(); var property = new MetadataPropertyType { Name = pi.Name, Attributes = propertyAttrs.ToAttributes(), Type = pi.ParameterType.GetOperationName(), Description = pi.GetDescription(), }; return property; }
public static MetadataType ToType(this Type type) { if (type == null) return null; var metaType = new MetadataType { Name = type.GetOperationName(), Namespace = type.Namespace, GenericArgs = type.IsGenericType ? type.GetGenericArguments().Select(x => x.GetOperationName()).ToArray() : null, Attributes = type.ToAttributes(), Properties = type.ToProperties(), }; if (type.BaseType != null && type.BaseType != typeof(object)) { metaType.Inherits = type.BaseType.GetOperationName(); metaType.InheritsGenericArgs = type.BaseType.IsGenericType ? type.BaseType.GetGenericArguments().Select(x => x.GetOperationName()).ToArray() : null; } if (type.GetTypeWithInterfaceOf(typeof(IReturnVoid)) != null) { metaType.ReturnVoidMarker = true; } else { var genericMarker = type.GetTypeWithGenericTypeDefinitionOf(typeof(IReturn<>)); if (genericMarker != null) { metaType.ReturnMarkerGenericArgs = genericMarker.GetGenericArguments().Select(x => x.GetOperationName()).ToArray(); } } var routeAttrs = type.AllAttributes<RouteAttribute>().ToList(); if (routeAttrs.Count > 0) { metaType.Routes = routeAttrs.ConvertAll(x => new MetadataRoute { Path = x.Path, Notes = x.Notes, Summary = x.Summary, Verbs = x.Verbs, }); } metaType.Description = type.GetDescription(); var dcAttr = type.GetDataContract(); if (dcAttr != null) { metaType.DataContract = new MetadataDataContract { Name = dcAttr.Name, Namespace = dcAttr.Namespace, }; } return metaType; }
internal static ModelDefinition GetModelDefinition(this Type modelType) { ModelDefinition modelDef; if (typeModelDefinitionMap.TryGetValue(modelType, out modelDef)) return modelDef; if (modelType.IsValueType() || modelType == typeof(string)) return null; var modelAliasAttr = modelType.FirstAttribute<AliasAttribute>(); var schemaAttr = modelType.FirstAttribute<SchemaAttribute>(); var preCreate = modelType.FirstAttribute<PreCreateTableAttribute>(); var postCreate = modelType.FirstAttribute<PostCreateTableAttribute>(); var preDrop = modelType.FirstAttribute<PreDropTableAttribute>(); var postDrop = modelType.FirstAttribute<PostDropTableAttribute>(); modelDef = new ModelDefinition { ModelType = modelType, Name = modelType.Name, Alias = modelAliasAttr?.Name, Schema = schemaAttr?.Name, PreCreateTableSql = preCreate?.Sql, PostCreateTableSql = postCreate?.Sql, PreDropTableSql = preDrop?.Sql, PostDropTableSql = postDrop?.Sql, }; modelDef.CompositeIndexes.AddRange( modelType.AllAttributes<CompositeIndexAttribute>().ToList() .ConvertAll(x => (CompositeIndexAttribute)x)); var objProperties = modelType.GetProperties( BindingFlags.Public | BindingFlags.Instance).ToList(); var hasPkAttr = objProperties.Any(p => p.HasAttribute<PrimaryKeyAttribute>()); var hasIdField = CheckForIdField(objProperties); var i = 0; foreach (var propertyInfo in objProperties) { if (propertyInfo.GetIndexParameters().Length > 0) continue; //Is Indexer var sequenceAttr = propertyInfo.FirstAttribute<SequenceAttribute>(); var computeAttr = propertyInfo.FirstAttribute<ComputeAttribute>(); var customSelectAttr = propertyInfo.FirstAttribute<CustomSelectAttribute>(); var decimalAttribute = propertyInfo.FirstAttribute<DecimalLengthAttribute>(); var belongToAttribute = propertyInfo.FirstAttribute<BelongToAttribute>(); var isFirst = i++ == 0; var isPrimaryKey = (!hasPkAttr && (propertyInfo.Name == OrmLiteConfig.IdField || (!hasIdField && isFirst))) || propertyInfo.HasAttributeNamed(typeof(PrimaryKeyAttribute).Name); var isRowVersion = propertyInfo.Name == ModelDefinition.RowVersionName && propertyInfo.PropertyType == typeof(ulong); var isNullableType = propertyInfo.PropertyType.IsNullableType(); var isNullable = (!propertyInfo.PropertyType.IsValueType() && !propertyInfo.HasAttributeNamed(typeof(RequiredAttribute).Name)) || isNullableType; var propertyType = isNullableType ? Nullable.GetUnderlyingType(propertyInfo.PropertyType) : propertyInfo.PropertyType; Type treatAsType = null; if (propertyType.IsEnumFlags() || propertyType.HasAttribute<EnumAsIntAttribute>()) treatAsType = Enum.GetUnderlyingType(propertyType); var aliasAttr = propertyInfo.FirstAttribute<AliasAttribute>(); var indexAttr = propertyInfo.FirstAttribute<IndexAttribute>(); var isIndex = indexAttr != null; var isUnique = isIndex && indexAttr.Unique; var stringLengthAttr = propertyInfo.CalculateStringLength(decimalAttribute); var defaultValueAttr = propertyInfo.FirstAttribute<DefaultAttribute>(); var referencesAttr = propertyInfo.FirstAttribute<ReferencesAttribute>(); var referenceAttr = propertyInfo.FirstAttribute<ReferenceAttribute>(); var fkAttr = propertyInfo.FirstAttribute<ForeignKeyAttribute>(); var customFieldAttr = propertyInfo.FirstAttribute<CustomFieldAttribute>(); var fieldDefinition = new FieldDefinition { Name = propertyInfo.Name, Alias = aliasAttr?.Name, FieldType = propertyType, FieldTypeDefaultValue = propertyType.GetDefaultValue(), TreatAsType = treatAsType, PropertyInfo = propertyInfo, IsNullable = isNullable, IsPrimaryKey = isPrimaryKey, AutoIncrement = isPrimaryKey && propertyInfo.HasAttributeNamed(typeof(AutoIncrementAttribute).Name), IsIndexed = !isPrimaryKey && isIndex, IsUnique = isUnique, IsClustered = indexAttr != null && indexAttr.Clustered, IsNonClustered = indexAttr != null && indexAttr.NonClustered, IsRowVersion = isRowVersion, FieldLength = stringLengthAttr?.MaximumLength, DefaultValue = defaultValueAttr?.DefaultValue, ForeignKey = fkAttr == null ? referencesAttr != null ? new ForeignKeyConstraint(referencesAttr.Type) : null : new ForeignKeyConstraint(fkAttr.Type, fkAttr.OnDelete, fkAttr.OnUpdate, fkAttr.ForeignKeyName), IsReference = referenceAttr != null && propertyType.IsClass(), GetValueFn = propertyInfo.GetPropertyGetterFn(), SetValueFn = propertyInfo.GetPropertySetterFn(), Sequence = sequenceAttr != null ? sequenceAttr.Name : string.Empty, IsComputed = computeAttr != null || customSelectAttr != null, ComputeExpression = computeAttr != null ? computeAttr.Expression : string.Empty, CustomSelect = customSelectAttr?.Sql, Scale = decimalAttribute?.Scale, BelongToModelName = belongToAttribute?.BelongToTableType.GetModelDefinition().ModelName, CustomFieldDefinition = customFieldAttr?.Sql, IsRefType = propertyType.IsRefType(), }; var isIgnored = propertyInfo.HasAttributeNamed(typeof(IgnoreAttribute).Name) || fieldDefinition.IsReference; if (isIgnored) modelDef.IgnoredFieldDefinitions.Add(fieldDefinition); else modelDef.FieldDefinitions.Add(fieldDefinition); if (isRowVersion) modelDef.RowVersion = fieldDefinition; } modelDef.AfterInit(); Dictionary<Type, ModelDefinition> snapshot, newCache; do { snapshot = typeModelDefinitionMap; newCache = new Dictionary<Type, ModelDefinition>(typeModelDefinitionMap) { [modelType] = modelDef }; } while (!ReferenceEquals( Interlocked.CompareExchange(ref typeModelDefinitionMap, newCache, snapshot), snapshot)); LicenseUtils.AssertValidUsage(LicenseFeature.OrmLite, QuotaType.Tables, typeModelDefinitionMap.Count); return modelDef; }