Example #1
0
        // PRIVATE METHODS //////////////////////////////////////////////////
        #region Methods
        private static void AddOrUpdateApiTypeConfiguration <TConfiguration, TMutableFactory, TMutable>(IDictionary <Type, ApiTypeConfiguration> dictionary,
                                                                                                        ApiTypeConfiguration apiTypeConfiguration,
                                                                                                        Type apiTypeConfigurationOpenGenericType,
                                                                                                        ApiPrecedenceLevel apiPrecedenceLevel,
                                                                                                        ApiPrecedenceStack apiPrecedenceStack)
            where TConfiguration : ApiTypeConfiguration
            where TMutableFactory : ApiMutableFactory <ApiMutableSchema, TMutable>
        {
            Contract.Requires(dictionary != null);
            Contract.Requires(apiTypeConfiguration != null);
            Contract.Requires(apiTypeConfigurationOpenGenericType != null);
            Contract.Requires(apiPrecedenceStack != null);

            var apiConfigurationIncoming = (TConfiguration)apiTypeConfiguration;
            var apiObjectFactoryIncoming = (TMutableFactory)apiConfigurationIncoming.ApiMutableFactory;

            var clrType = apiTypeConfiguration.ClrType;

            if (dictionary.TryGetValue(clrType, out var apiConfiguration))
            {
                // Update the existing API configuration by merging the incoming derived API configuration.
                var apiConfigurationExisting = (TConfiguration)apiConfiguration;
                var apiObjectFactoryExisting = (TMutableFactory)apiConfigurationExisting.ApiMutableFactory;
                apiObjectFactoryExisting.Merge(apiObjectFactoryIncoming);
                return;
            }

            // Create a new API configuration from the incoming derived API configuration.
            var apiConfigurationNew = ApiTypeConfigurationFactory.CreateApiTypeConfiguration <TConfiguration, TMutableFactory, TMutable>(clrType, apiTypeConfigurationOpenGenericType, apiPrecedenceStack, apiObjectFactoryIncoming);

            dictionary.Add(clrType, apiConfigurationNew);

            ApiFrameworkLog.Trace($"Added {typeof(TConfiguration).Name} [ClrType={apiConfigurationNew.ClrType.Name}] at {apiPrecedenceLevel} Level".Indent(IndentConstants.ApiMutableSchemaModifier));
        }
Example #2
0
        private static TApiConfiguration GetOrAddApiConfiguration <TApiConfiguration>(IDictionary <Type, ApiTypeConfiguration> dictionary,
                                                                                      Type clrType,
                                                                                      Type apiConfigurationOpenGenericType,
                                                                                      ApiPrecedenceLevel apiPrecedenceLevel,
                                                                                      ApiPrecedenceStack apiPrecedenceStack)
            where TApiConfiguration : ApiTypeConfiguration
        {
            Contract.Requires(dictionary != null);
            Contract.Requires(clrType != null);
            Contract.Requires(apiConfigurationOpenGenericType != null);
            Contract.Requires(apiPrecedenceStack != null);

            if (dictionary.TryGetValue(clrType, out var apiConfiguration))
            {
                var apiConfigurationExisting = (TApiConfiguration)apiConfiguration;
                return(apiConfigurationExisting);
            }

            var apiConfigurationNew = ApiTypeConfigurationFactory.CreateApiTypeConfiguration <TApiConfiguration>(clrType, apiConfigurationOpenGenericType, apiPrecedenceStack);

            dictionary.Add(clrType, apiConfigurationNew);

            var apiTypeKind = apiConfigurationNew.ApiTypeKind;

            switch (apiTypeKind)
            {
            case ApiTypeKind.Enumeration:
            {
                ApiFrameworkLog.Trace($"Added {nameof(ApiEnumerationType)} [{nameof(ApiEnumerationType.ClrType)}={clrType.Name}] at {apiPrecedenceLevel} Level".Indent(IndentConstants.ApiMutableSchemaModifier));
                break;
            }

            case ApiTypeKind.Object:
            {
                ApiFrameworkLog.Trace($"Added {nameof(ApiObjectType)} [{nameof(ApiObjectType.ClrType)}={clrType.Name}] at {apiPrecedenceLevel} Level".Indent(IndentConstants.ApiMutableSchemaModifier));
                break;
            }

            case ApiTypeKind.Scalar:
            {
                ApiFrameworkLog.Trace($"Added {nameof(ApiScalarType)} [{nameof(ApiScalarType.ClrType)}={clrType.Name}] at {apiPrecedenceLevel} Level".Indent(IndentConstants.ApiMutableSchemaModifier));
                break;
            }

            default:
                throw new ArgumentOutOfRangeException();
            }

            return(apiConfigurationNew);
        }