// PUBLIC METHODS /////////////////////////////////////////////////// #region IApiObjectTypeConvention Implementation public void Apply(IApiObjectTypeBuilder apiObjectTypeBuilder, ApiConventionSettings apiConventionSettings) { Contract.Requires(apiObjectTypeBuilder != null); // Call ApiProperty method on all the discoverable CLR properties for the given CLR object type. var clrObjectType = apiObjectTypeBuilder.ClrType; var clrProperties = ClrPropertyDiscoveryRules.GetClrProperties(clrObjectType); foreach (var clrProperty in clrProperties) { var clrPropertyName = clrProperty.Name; var clrPropertyType = clrProperty.PropertyType; apiObjectTypeBuilder.ApiProperty(clrPropertyName, clrPropertyType); } }
/// <summary>Adds a property on the API object type by selecting the CLR property that represents the API property.</summary> /// <typeparam name="TObject">The CLR object type associated to the API object type for CLR serializing/deserializing purposes.</typeparam> /// <typeparam name="TProperty">The CLR type of property selected on the CLR object type.</typeparam> /// <param name="clrPropertySelector">Expression that selects the CLR property on the CLR object type.</param> /// <returns>A fluent-style builder for the API object type.</returns> public static IApiObjectTypeBuilder <TObject> ApiProperty <TObject, TProperty>(this IApiObjectTypeBuilder <TObject> apiObjectTypeBuilder, Expression <Func <TObject, TProperty> > clrPropertySelector) { Contract.Requires(clrPropertySelector != null); return(apiObjectTypeBuilder.ApiProperty(clrPropertySelector, null)); }
// PRIVATE METHODS ////////////////////////////////////////////////// #region Methods private static void HandleApiPropertyAttribute(IApiObjectTypeBuilder apiObjectTypeBuilder, ApiPrecedenceStack apiPrecedenceStack, PropertyInfo clrPropertyInfo) { Contract.Requires(apiObjectTypeBuilder != null); Contract.Requires(apiPrecedenceStack != null); Contract.Requires(clrPropertyInfo != null); var apiPropertyAttribute = (ApiPropertyAttribute)Attribute.GetCustomAttribute(clrPropertyInfo, typeof(ApiPropertyAttribute)); if (apiPropertyAttribute == null) { return; } var clrPropertyName = clrPropertyInfo.Name; var clrPropertyType = clrPropertyInfo.PropertyType; if (apiPropertyAttribute.Excluded) { apiPrecedenceStack.Push(ApiPrecedenceLevel.Annotation); apiObjectTypeBuilder.Exclude(clrPropertyName); apiPrecedenceStack.Pop(); return; } IApiPropertyBuilder ApiPropertyConfiguration(IApiPropertyBuilder apiPropertyBuilder) { apiPrecedenceStack.Push(ApiPrecedenceLevel.Annotation); if (String.IsNullOrWhiteSpace(apiPropertyAttribute.Name) == false) { apiPropertyBuilder.HasName(apiPropertyAttribute.Name); } if (String.IsNullOrWhiteSpace(apiPropertyAttribute.Description) == false) { apiPropertyBuilder.HasDescription(apiPropertyAttribute.Description); } if (apiPropertyAttribute.Required) { apiPropertyBuilder.IsRequired(); } // ReSharper disable once InvertIf if (apiPropertyAttribute.ItemRequired) { IApiCollectionTypeBuilder ApiCollectionTypeConfiguration(IApiCollectionTypeBuilder apiCollectionTypeBuilder) { apiPrecedenceStack.Push(ApiPrecedenceLevel.Annotation); apiCollectionTypeBuilder.ItemIsRequired(); apiPrecedenceStack.Pop(); return(apiCollectionTypeBuilder); } apiPropertyBuilder.ApiCollectionType(ApiCollectionTypeConfiguration); } apiPrecedenceStack.Pop(); return(apiPropertyBuilder); } apiPrecedenceStack.Push(ApiPrecedenceLevel.Annotation); apiObjectTypeBuilder.ApiProperty(clrPropertyName, clrPropertyType, ApiPropertyConfiguration); apiPrecedenceStack.Pop(); }