コード例 #1
0
        // PUBLIC METHODS ///////////////////////////////////////////////////
        #region IApiSchemaConvention Implementation
        public void Apply(IApiSchemaBuilder apiSchemaBuilder, ApiConventionSettings apiConventionSettings)
        {
            Contract.Requires(apiSchemaBuilder != null);

            apiConventionSettings = apiConventionSettings ?? ApiConventionSettings.Empty;
            var apiDiscoverySettings = apiConventionSettings.ApiDiscoverySettings;
            var assemblies           = apiDiscoverySettings.Assemblies;
            var clrTypes             = assemblies.SelectMany(x => x.GetExportedTypes())
                                       .ToList();

            foreach (var clrType in clrTypes)
            {
                var apiTypeKind = clrType.GetApiTypeKind();
                switch (apiTypeKind)
                {
                case ApiTypeKind.Object:
                {
                    if (ClrTypeDiscoveryRules.CanAddApiObjectType(clrType) == false)
                    {
                        continue;
                    }

                    var apiTypeDiscoveryPredicateResult = apiDiscoverySettings.ApiTypeDiscoveryPredicate?.Invoke(clrType) ?? true;
                    if (apiTypeDiscoveryPredicateResult == false)
                    {
                        continue;
                    }

                    apiSchemaBuilder.ApiObjectType(clrType);
                    break;
                }
                }
            }
        }
コード例 #2
0
        private void HandleApiObjectTypeAttribute(IApiSchemaBuilder apiSchemaBuilder,
                                                  ApiConventionSettings apiConventionSettings,
                                                  ApiPrecedenceStack apiPrecedenceStack,
                                                  Type clrType)
        {
            Contract.Requires(apiSchemaBuilder != null);
            Contract.Requires(apiConventionSettings != null);
            Contract.Requires(clrType != null);

            var apiObjectTypeAttribute = (ApiObjectTypeAttribute)Attribute.GetCustomAttribute(clrType, typeof(ApiObjectTypeAttribute));

            if (apiObjectTypeAttribute == null)
            {
                return;
            }

            if (ClrTypeDiscoveryRules.CanAddApiObjectType(clrType) == false)
            {
                return;
            }

            var apiDiscoverySettings = apiConventionSettings.ApiDiscoverySettings;
            var apiAnnotationDiscoveryPredicateResult = apiDiscoverySettings.ApiAnnotationDiscoveryPredicate?.Invoke(clrType) ?? true;

            if (apiAnnotationDiscoveryPredicateResult == false)
            {
                return;
            }

            if (apiObjectTypeAttribute.Excluded)
            {
                apiPrecedenceStack.Push(ApiPrecedenceLevel.Annotation);
                apiSchemaBuilder.Exclude(clrType);
                apiPrecedenceStack.Pop();
                return;
            }

            IApiObjectTypeBuilder ApiObjectTypeConfiguration(IApiObjectTypeBuilder apiObjectTypeBuilder)
            {
                apiPrecedenceStack.Push(ApiPrecedenceLevel.Annotation);
                if (String.IsNullOrWhiteSpace(apiObjectTypeAttribute.Name) == false)
                {
                    apiObjectTypeBuilder.HasName(apiObjectTypeAttribute.Name);
                }

                if (String.IsNullOrWhiteSpace(apiObjectTypeAttribute.Description) == false)
                {
                    apiObjectTypeBuilder.HasDescription(apiObjectTypeAttribute.Description);
                }

                apiPrecedenceStack.Pop();

                return(apiObjectTypeBuilder);
            }

            apiPrecedenceStack.Push(ApiPrecedenceLevel.Annotation);
            apiSchemaBuilder.ApiObjectType(clrType, ApiObjectTypeConfiguration);
            apiPrecedenceStack.Pop();
        }