// PUBLIC METHODS /////////////////////////////////////////////////// #region IApiObjectTypeConvention Implementation public void Apply(IApiObjectTypeBuilder apiObjectTypeBuilder, ApiConventionSettings apiConventionSettings) { Contract.Requires(apiObjectTypeBuilder != null); // Try and discover the identity CLR property of the CLR object type: var clrObjectType = apiObjectTypeBuilder.ClrType; var clrProperties = ClrPropertyDiscoveryRules.GetClrProperties(clrObjectType); // 1. By convention, any CLR property named "Id" is the identity CLR property for this CLR object type. var clrIdProperty = ClrPropertyDiscoveryRules.GetClrPropertyByName(clrProperties, "Id"); if (clrIdProperty != null) { // Call ApiIdentity method on the discovered CLR property. var clrIdPropertyName = clrIdProperty.Name; var clrIdPropertyType = clrIdProperty.PropertyType; apiObjectTypeBuilder.ApiIdentity(clrIdPropertyName, clrIdPropertyType); return; } // 2. By convention, any CLR property named "XXXId" where XXX is the CLR class name is the identity CLR property for this CLR object type. var clrClassName = clrObjectType.Name; var clrClassNameAndId = $"{clrClassName}Id"; var clrClassNameAndIdProperty = ClrPropertyDiscoveryRules.GetClrPropertyByName(clrProperties, clrClassNameAndId); // ReSharper disable once InvertIf if (clrClassNameAndIdProperty != null) { // Call ApiIdentity method on the discovered CLR property. var clrClassNameAndIdPropertyName = clrClassNameAndIdProperty.Name; var clrClassNameAndIdPropertyType = clrClassNameAndIdProperty.PropertyType; apiObjectTypeBuilder.ApiIdentity(clrClassNameAndIdPropertyName, clrClassNameAndIdPropertyType); } }
// 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); } }
// PUBLIC METHODS /////////////////////////////////////////////////// #region IApiObjectTypeConvention Implementation public void Apply(IApiObjectTypeBuilder apiObjectTypeBuilder, ApiConventionSettings apiConventionSettings) { Contract.Requires(apiObjectTypeBuilder != null); var clrObjectType = apiObjectTypeBuilder.ClrType; var clrPropertyInfoCollection = ClrPropertyDiscoveryRules.GetClrProperties(clrObjectType); var apiObjectTypeConfiguration = (ApiObjectTypeConfiguration)apiObjectTypeBuilder; var apiPrecedenceStack = apiObjectTypeConfiguration.ApiPrecedenceStack; foreach (var clrPropertyInfo in clrPropertyInfoCollection) { HandleApiPropertyAttribute(apiObjectTypeBuilder, apiPrecedenceStack, clrPropertyInfo); HandleApiIdentityAttribute(apiObjectTypeBuilder, apiPrecedenceStack, clrPropertyInfo); HandleApiRelationshipAttribute(apiObjectTypeBuilder, apiPrecedenceStack, clrPropertyInfo); } }
// PUBLIC METHODS /////////////////////////////////////////////////// #region IApiObjectTypeConvention Implementation public void Apply(IApiObjectTypeBuilder apiObjectTypeBuilder, ApiConventionSettings apiConventionSettings) { Contract.Requires(apiObjectTypeBuilder != null); // Try and discover relationship CLR properties of the CLR object type: var clrObjectType = apiObjectTypeBuilder.ClrType; var clrProperties = ClrPropertyDiscoveryRules.GetClrProperties(clrObjectType); foreach (var clrProperty in clrProperties) { var clrPropertyType = clrProperty.PropertyType; var apiTypeKind = clrPropertyType.GetApiTypeKind(out var clrItemType); switch (apiTypeKind) { case ApiTypeKind.Collection: { var apiItemTypeKind = clrItemType.GetApiTypeKind(); switch (apiItemTypeKind) { case ApiTypeKind.Object: { var clrPropertyName = clrProperty.Name; apiObjectTypeBuilder.ApiRelationship(clrPropertyName, clrPropertyType); break; } } break; } case ApiTypeKind.Object: { var clrPropertyName = clrProperty.Name; apiObjectTypeBuilder.ApiRelationship(clrPropertyName, clrPropertyType); break; } } } }