AddMapping() public method

public AddMapping ( IResourceMapping resourceMapping ) : void
resourceMapping IResourceMapping
return void
Beispiel #1
0
        private IConfiguration CreateConfiguration()
        {
            var mapping = new ResourceMapping <SampleClass, DummyController>(c => c.Id);

            mapping.ResourceType = "sampleClasses";
            mapping.AddPropertyGetter("someValue", c => c.SomeValue);
            mapping.AddPropertyGetter("date", c => c.DateTime);

            var mappingWithLinks = new ResourceMapping <SampleClassWithObjectLinks, DummyController>(c => c.Id);

            mappingWithLinks.ResourceType = "sampleClassesWithLinks";
            mappingWithLinks.AddPropertyGetter("someValue", c => c.SomeValue);

            var config = new NJsonApi.Configuration();

            config.AddMapping(mapping);
            config.AddMapping(mappingWithLinks);
            return(config);
        }
        public IConfiguration Build()
        {
            Configuration configuration = new Configuration()
            {
                JsonSerializerFactory = this.jsonSerialzierFactory,
                CreateResponseInJsonApiForUnhandedExceptions = this.createResponseInJsonApiForUnhandedExceptions
            };

            configuration.AddInputMapper(this.inputMappers);
            configuration.AddPreSerializationAction(this.preSerializationActions);
            configuration.AddOverrideResponseHeadersAction(this.overrideResponseHeadersActios);
            configuration.AddSupportedOutputContentTypes(this.supportedOutputTypes);
            configuration.AddSupportedInputContentTypes(this.supportedInputTypes);
            var propertyScanningConvention = GetConvention <IPropertyScanningConvention>();

            // Each link needs to be wired to full metadata once all resources are registered
            foreach (var resourceConfiguration in ResourceConfigurationsByType)
            {
                var links = resourceConfiguration.Value.ConstructedMetadata.Relationships;
                for (int i = links.Count - 1; i >= 0; i--)
                {
                    var link = links[i];
                    IResourceConfigurationBuilder resourceConfigurationOutput;
                    if (!ResourceConfigurationsByType.TryGetValue(link.RelatedBaseType, out resourceConfigurationOutput))
                    {
                        if (propertyScanningConvention.ThrowOnUnmappedLinkedType)
                        {
                            throw new InvalidOperationException(
                                      string.Format(
                                          "Type {0} was registered to have a linked resource {1} of type {2} which was not registered. Register resource type {2} or disable serialization of that property.",
                                          link.ParentType.Name,
                                          link.RelationshipName,
                                          link.RelatedBaseType.Name));
                        }
                        else
                        {
                            links.RemoveAt(i);
                        }
                    }
                    else
                    {
                        link.ResourceMapping = resourceConfigurationOutput.ConstructedMetadata;
                    }
                }

                configuration.AddMapping(resourceConfiguration.Value.ConstructedMetadata);
            }

            return(configuration);
        }
        public Configuration Build()
        {
            var configuration = new Configuration();
            var propertyScanningConvention = GetConvention <IPropertyScanningConvention>();

            // Each link needs to be wired to full metadata once all resources are registered
            foreach (var resourceConfiguration in ResourceConfigurationsByType)
            {
                var links = resourceConfiguration.Value.ConstructedMetadata.Relationships;
                for (int i = links.Count - 1; i >= 0; i--)
                {
                    var link = links[i];
                    IResourceConfigurationBuilder resourceConfigurationOutput;
                    if (!ResourceConfigurationsByType.TryGetValue(link.RelatedBaseType, out resourceConfigurationOutput))
                    {
                        if (propertyScanningConvention.ThrowOnUnmappedLinkedType)
                        {
                            throw new InvalidOperationException(
                                      string.Format(
                                          "Type {0} was registered to have a linked resource {1} of type {2} which was not registered. Register resource type {2} or disable serialization of that property.",
                                          link.ParentType.Name,
                                          link.RelationshipName,
                                          link.RelatedBaseType.Name));
                        }
                        else
                        {
                            links.RemoveAt(i);
                        }
                    }
                    else
                    {
                        link.ResourceMapping = resourceConfigurationOutput.ConstructedMetadata;
                    }
                }

                configuration.AddMapping(resourceConfiguration.Value.ConstructedMetadata);
            }

            return(configuration);
        }