/// <summary>Try and get the API object type by CLR type.</summary>
        /// <typeparam name="TObject">CLR type to lookup the API object type by.</typeparam>
        /// <param name="apiObjectType">The API object type if it exists in the API schema, null otherwise.</param>
        /// <returns>True if API object type exists in the API schema, false otherwise.</returns>
        public static bool TryGetApiObjectType <TObject>(this IApiSchema apiSchema, out IApiObjectType apiObjectType)
        {
            Contract.Requires(apiSchema != null);

            var clrType = typeof(TObject);

            return(apiSchema.TryGetApiObjectType(clrType, out apiObjectType));
        }
Exemple #2
0
        // PUBLIC METHODS ///////////////////////////////////////////////////
        #region Extension Methods
        /// <summary>
        /// Gets the basic type guidance enumeration for the API object type.
        /// </summary>
        /// <param name="apiObjectType">API object type to call extension method on.</param>
        /// <returns>An <see cref="ApiObjectTypeKind"/> giving basic type guidance about the API object type.</returns>
        public static ApiObjectTypeKind GetApiObjectTypeKind(this IApiObjectType apiObjectType)
        {
            Contract.Requires(apiObjectType != null);

            var apiObjectTypeKind = apiObjectType.ApiIdentity != null ? ApiObjectTypeKind.ResourceType : ApiObjectTypeKind.ComplexType;

            return(apiObjectTypeKind);
        }
        public bool TryGetApiObjectType(Type clrType, out IApiObjectType apiObjectType)
        {
            if (clrType != null)
            {
                return(this.ClrTypeToApiObjectTypeDictionary.TryGetValue(clrType, out apiObjectType));
            }

            apiObjectType = null;
            return(false);
        }
        public bool TryGetApiObjectType(string apiName, out IApiObjectType apiObjectType)
        {
            if (!String.IsNullOrWhiteSpace(apiName))
            {
                return(this.ApiNameToApiObjectTypeDictionary.TryGetValue(apiName, out apiObjectType));
            }

            apiObjectType = null;
            return(false);
        }
Exemple #5
0
        /// <summary>Get the API relationship by CLR relationship name, thrown an exception if the API relationship does not exist.</summary>
        /// <param name="apiObjectType">API object type to call extension method on.</param>
        /// <param name="clrName">CLR relationship name to lookup the API relationship by.</param>
        /// <returns>The named API relationship if it exists, throws an exception if the API relationship does not exist.</returns>
        public static IApiRelationship GetApiRelationshipByClrName(this IApiObjectType apiObjectType, string clrName)
        {
            Contract.Requires(apiObjectType != null);
            Contract.Requires(clrName != null);

            if (apiObjectType.TryGetApiRelationshipByClrName(clrName, out var apiRelationship))
            {
                return(apiRelationship);
            }

            // Unable to get API relationship by the given CLR relationship name.
            var message = $"Unable to get API relationship [clrName={clrName}] in the API object type [apiName={apiObjectType.ApiName} clrName={apiObjectType.ClrType.Name}].";

            throw new ApiSchemaException(message);
        }
Exemple #6
0
        /// <summary>Get the API property by API property name, thrown an exception if the API property does not exist.</summary>
        /// <param name="apiObjectType">API object type to call extension method on.</param>
        /// <param name="apiName">API property name to lookup the API property by.</param>
        /// <returns>The named API property if it exists, throws an exception if the API property does not exist.</returns>
        public static IApiProperty GetApiPropertyByApiName(this IApiObjectType apiObjectType, string apiName)
        {
            Contract.Requires(apiObjectType != null);
            Contract.Requires(apiName != null);

            if (apiObjectType.TryGetApiPropertyByApiName(apiName, out var apiProperty))
            {
                return(apiProperty);
            }

            // Unable to get API property by the given API property name.
            var message = $"Unable to get API property [apiName={apiName}] in the API object type [apiName={apiObjectType.ApiName} clrName={apiObjectType.ClrType.Name}].";

            throw new ApiSchemaException(message);
        }
        /// <summary>Try and get the API resource type by CLR type object.</summary>
        /// <param name="clrResourceType">CLR resource type object to lookup the API resource type by.</param>
        /// <param name="apiResourceType">The API resource type if it exists in the API schema, null otherwise.</param>
        /// <returns>True if API resource type exists in the API schema, false otherwise.</returns>
        /// <remarks>If the CLR type exists but is an API complex object, then this method returns false.</remarks>
        public static bool TryGetApiResourceType(this IApiSchema apiSchema, Type clrResourceType, out IApiObjectType apiResourceType)
        {
            Contract.Requires(apiSchema != null);
            Contract.Requires(clrResourceType != null);

            apiResourceType = null;

            if (!apiSchema.TryGetApiObjectType(clrResourceType, out var apiObjectType))
            {
                return(false);
            }

            var apiObjectTypeKind = apiObjectType.GetApiObjectTypeKind();

            if (apiObjectTypeKind != ApiObjectTypeKind.ResourceType)
            {
                return(false);
            }

            apiResourceType = apiObjectType;
            return(true);
        }
        /// <summary>Try and get the API resource type by API type name.</summary>
        /// <param name="apiName">API type name to lookup the API resource type by.</param>
        /// <param name="apiResourceType">The API resource type if it exists in the API schema, null otherwise.</param>
        /// <returns>True if API resource type exists in the API schema, false otherwise.</returns>
        public static bool TryGetApiResourceType(this IApiSchema apiSchema, string apiName, out IApiObjectType apiResourceType)
        {
            Contract.Requires(apiSchema != null);
            Contract.Requires(apiName.SafeHasContent());

            apiResourceType = null;

            if (!apiSchema.TryGetApiObjectType(apiName, out var apiObjectType))
            {
                return(false);
            }

            var apiObjectTypeKind = apiObjectType.GetApiObjectTypeKind();

            if (apiObjectTypeKind != ApiObjectTypeKind.ResourceType)
            {
                return(false);
            }

            apiResourceType = apiObjectType;
            return(true);
        }
 public TryGetApiObjectTypeWithClrTypeUnitTest(string name, IApiSchema apiSchema, IApiObjectType expectedApiObjectType)
     : base(name)
 {
     this.ApiSchema             = apiSchema;
     this.ExpectedApiObjectType = expectedApiObjectType;
 }