private static IApiIdentity CreateApiIdentity(ApiMutableObjectType apiMutableObjectType, IEnumerable <IApiProperty> apiProperties)
        {
            Contract.Requires(apiMutableObjectType != null);
            Contract.Requires(apiProperties != null);

            var clrIdentityProperty = apiMutableObjectType.ClrIdentityProperty;

            if (clrIdentityProperty == null)
            {
                return(null);
            }

            var clrIdentityPropertyName = clrIdentityProperty.ClrPropertyName;

            if (String.IsNullOrWhiteSpace(clrIdentityPropertyName))
            {
                return(null);
            }

            var apiIdentityProperty = (ApiProperty)apiProperties.SingleOrDefault(x => x.ClrName == clrIdentityPropertyName);

            if (apiIdentityProperty == null)
            {
                var clrTypeName = apiMutableObjectType.ClrObjectType.Name;
                var message     = $"Unable to create API identity for an API object type [clrType={clrTypeName}] because the configured API identity property [clrName={clrIdentityPropertyName}] does not exist.";
                throw new ApiSchemaException(message);
            }

            var apiIdentity = ApiTypeFactory.CreateApiIdentity(apiIdentityProperty);

            ApiFrameworkLog.Debug($"Created {apiIdentity}".Indent(IndentConstants.ApiMutableObjectTypeIdentity));

            return(apiIdentity);
        }
        public IApiEnumerationValue Create(ApiMutableSchema apiMutableSchema)
        {
            Contract.Requires(apiMutableSchema != null);

            var apiMutableEnumerationValue = this.ApiMutableEnumerationValueFactory.Create(apiMutableSchema);

            var apiEnumerationValue = CreateApiEnumerationValue(apiMutableEnumerationValue);

            ApiFrameworkLog.Debug($"Created {apiEnumerationValue}".Indent(IndentConstants.ApiEnumerationValue));

            return(apiEnumerationValue);
        }
Exemplo n.º 3
0
        public IApiCollectionType Create(ApiMutableSchema apiMutableSchema, ApiSchemaProxy apiSchemaProxy)
        {
            Contract.Requires(apiMutableSchema != null);
            Contract.Requires(apiSchemaProxy != null);

            var apiMutableCollectionType = this.CreateApiMutableType(apiMutableSchema);

            var apiCollectionType = (IApiCollectionType)this.CreateApiType(apiMutableCollectionType, apiSchemaProxy);

            ApiFrameworkLog.Debug($"Created {apiCollectionType}".Indent(IndentConstants.ApiCollectionType));

            return(apiCollectionType);
        }
Exemplo n.º 4
0
        internal override IApiType CreateApiType(ApiMutableType apiMutableType, ApiSchemaProxy apiSchemaProxy)
        {
            Contract.Requires(apiMutableType != null);
            Contract.Requires(apiSchemaProxy != null);

            var apiMutableScalarType = (ApiMutableScalarType)apiMutableType;

            var apiScalarType = CreateApiScalarType(apiMutableScalarType);

            ApiFrameworkLog.Debug($"Created {apiScalarType}".Indent(IndentConstants.ApiScalarType));

            return(apiScalarType);
        }
        public IApiSchema Create(ApiSchemaFactorySettings apiSchemaFactorySettings)
        {
            ApiFrameworkLog.Debug($"Creating {nameof(ApiSchema)}".Indent(IndentConstants.ApiSchema));

            var apiMutableSchema = this.ApiMutableSchemaFactory.Create(apiSchemaFactorySettings);

            var apiSchema = CreateApiSchema(apiMutableSchema, this.ApiPrecedenceStack);

            var createdApiSchema = apiSchema.ToTreeString();

            ApiFrameworkLog.Information($"Created {nameof(ApiSchema)}" + "\n" + "{CreatedApiSchema}".Indent(IndentConstants.ApiSchema), createdApiSchema);

            return(apiSchema);
        }
Exemplo n.º 6
0
        public IApiProperty CreateApiProperty(ApiMutableSchema apiMutableSchema, ApiMutableObjectType apiMutableObjectType, ApiSchemaProxy apiSchemaProxy)
        {
            Contract.Requires(apiMutableSchema != null);
            Contract.Requires(apiMutableObjectType != null);
            Contract.Requires(apiSchemaProxy != null);

            var apiMutableProperty = this.ApiMutablePropertyFactory.Create(apiMutableSchema);

            var apiProperty = CreateApiProperty(apiMutableObjectType, apiMutableProperty, apiSchemaProxy);

            ApiFrameworkLog.Debug($"Created {apiProperty}".Indent(IndentConstants.ApiProperty));

            return(apiProperty);
        }
        private static IEnumerable <IApiObjectType> CreateApiObjectTypes(ApiMutableSchema apiMutableSchema,
                                                                         ApiSchemaProxy apiSchemaProxy)
        {
            Contract.Requires(apiMutableSchema != null);
            Contract.Requires(apiSchemaProxy != null);

            ApiFrameworkLog.Debug($"Creating {nameof(ApiObjectType)}s".Indent(IndentConstants.ApiObjectTypes));

            var apiObjectTypeConfigurations = apiMutableSchema.ApiObjectTypeConfigurationDictionary
                                              .Values
                                              .ToList();
            var clrExcludedObjectTypes = apiMutableSchema.ClrExcludedObjectTypes;
            var apiObjectTypes         = CreateApiTypes <IApiObjectType>(apiMutableSchema, apiSchemaProxy, apiObjectTypeConfigurations, clrExcludedObjectTypes);

            ApiFrameworkLog.Debug($"Created {nameof(ApiObjectType)}s".Indent(IndentConstants.ApiObjectTypes));

            return(apiObjectTypes);
        }
        private static IEnumerable <IApiEnumerationType> CreateApiEnumerationTypes(ApiMutableSchema apiMutableSchema,
                                                                                   ApiPrecedenceStack apiPrecedenceStack,
                                                                                   ApiSchemaProxy apiSchemaProxy)
        {
            Contract.Requires(apiMutableSchema != null);
            Contract.Requires(apiPrecedenceStack != null);

            ApiFrameworkLog.Debug($"Creating {nameof(ApiEnumerationType)}s".Indent(IndentConstants.ApiEnumerationTypes));

            RemoveUnusedApiEnumerationTypes(apiMutableSchema);
            AddMissingApiEnumerationTypes(apiMutableSchema, apiPrecedenceStack);

            var apiEnumerationTypeConfigurations = apiMutableSchema.ApiEnumerationTypeConfigurationDictionary
                                                   .Values
                                                   .ToList();
            var clrExcludedEnumerationTypes = apiMutableSchema.ClrExcludedEnumerationTypes;
            var apiEnumerationTypes         = CreateApiTypes <IApiEnumerationType>(apiMutableSchema, apiSchemaProxy, apiEnumerationTypeConfigurations, clrExcludedEnumerationTypes);

            ApiFrameworkLog.Debug($"Created {nameof(ApiEnumerationType)}s".Indent(IndentConstants.ApiEnumerationTypes));

            return(apiEnumerationTypes);
        }
        // ReSharper disable once ReturnTypeCanBeEnumerable.Local
        private static IReadOnlyCollection <IApiRelationship> CreateApiRelationships(ApiMutableObjectType apiMutableObjectType,
                                                                                     ApiSchemaProxy apiSchemaProxy,
                                                                                     IEnumerable <IApiProperty> apiProperties,
                                                                                     IApiIdentity apiIdentity)
        {
            Contract.Requires(apiMutableObjectType != null);
            Contract.Requires(apiSchemaProxy != null);
            Contract.Requires(apiProperties != null);

            // If this API object type has no identity, it may not contain any relationships.
            if (apiIdentity == null)
            {
                return(null);
            }

            var apiMutableSchema = apiMutableObjectType.ApiMutableSchema;
            var clrRelationshipPropertyCollection = apiMutableObjectType.ClrRelationshipPropertyCollection;
            var apiRelationships = clrRelationshipPropertyCollection
                                   .Where(x =>
            {
                // Can not create an API relationship if the related API object type does not have identity (must be an API resource type).
                var clrPropertyType     = x.ClrPropertyType;
                var apiPropertyTypeKind = clrPropertyType.GetApiTypeKind(out var clrPropertyItemType);
                switch (apiPropertyTypeKind)
                {
                case ApiTypeKind.Object:
                    {
                        var isApiResourceType = apiMutableSchema.ClrResourceTypes.Contains(clrPropertyType);
                        if (!isApiResourceType)
                        {
                            return(false);
                        }

                        break;
                    }

                case ApiTypeKind.Collection:
                    {
                        var apiItemTypeKind = clrPropertyItemType.GetApiTypeKind();
                        switch (apiItemTypeKind)
                        {
                        case ApiTypeKind.Collection:
                            {
                                // Unable to handle collections within collections.
                                var message = $"Unable to create API relationship for an API property [{nameof(x.ClrPropertyName)}={x.ClrPropertyName}] that contains collections within collections";
                                throw new ApiSchemaException(message);
                            }
                        }

                        var isApiResourceType = apiMutableSchema.ClrResourceTypes.Contains(clrPropertyItemType);
                        if (!isApiResourceType)
                        {
                            return(false);
                        }

                        break;
                    }

                default:
                    {
                        return(false);
                    }
                }

                return(true);
            })
                                   .Select(x =>
            {
                var clrPropertyName     = x.ClrPropertyName;
                var clrPropertyType     = x.ClrPropertyType;
                var apiProperty         = apiProperties.Single(y => y.ClrName == clrPropertyName);
                var apiPropertyTypeKind = clrPropertyType.GetApiTypeKind(out var clrPropertyItemType);

                ApiRelationshipCardinality apiCardinality;
                Type clrType;
                switch (apiPropertyTypeKind)
                {
                case ApiTypeKind.Object:
                    {
                        apiCardinality = ApiRelationshipCardinality.ToOne;
                        clrType        = clrPropertyType;
                        break;
                    }

                case ApiTypeKind.Collection:
                    {
                        apiCardinality = ApiRelationshipCardinality.ToMany;
                        clrType        = clrPropertyItemType;
                        break;
                    }

                default:
                    {
                        throw new ArgumentOutOfRangeException();
                    }
                }

                var apiRelatedTypeResolver = new ApiSchemaProxyTypeResolver(apiSchemaProxy, ApiTypeKind.Object, clrType);
                var apiRelationship        = ApiTypeFactory.CreateApiRelationship(apiProperty, apiCardinality, apiRelatedTypeResolver);

                return(apiRelationship);
            })
                                   .ToList();

            foreach (var apiRelationship in apiRelationships)
            {
                ApiFrameworkLog.Debug($"Created {apiRelationship}".Indent(IndentConstants.ApiMutableObjectTypeRelationship));
            }

            return(apiRelationships);
        }