// 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);
            }
        }
示例#2
0
        private static void HandleApiIdentityAttribute(IApiObjectTypeBuilder apiObjectTypeBuilder,
                                                       ApiPrecedenceStack apiPrecedenceStack,
                                                       PropertyInfo clrPropertyInfo)
        {
            Contract.Requires(apiObjectTypeBuilder != null);
            Contract.Requires(clrPropertyInfo != null);

            var apiIdentityAttribute = Attribute.GetCustomAttribute(clrPropertyInfo, typeof(ApiIdentityAttribute));

            if (apiIdentityAttribute == null)
            {
                return;
            }

            var clrPropertyName = clrPropertyInfo.Name;
            var clrPropertyType = clrPropertyInfo.PropertyType;

            apiPrecedenceStack.Push(ApiPrecedenceLevel.Annotation);
            apiObjectTypeBuilder.ApiIdentity(clrPropertyName, clrPropertyType);
            apiPrecedenceStack.Pop();
        }