public void GetRegistrationForResourceTypeName_returns_correct_value_for_registered_names() { // Arrange var mockPostRegistration = new Mock <IResourceTypeRegistration>(MockBehavior.Strict); mockPostRegistration.Setup(m => m.Type).Returns(typeof(Post)); mockPostRegistration.Setup(m => m.ResourceTypeName).Returns("posts"); var mockAuthorRegistration = new Mock <IResourceTypeRegistration>(MockBehavior.Strict); mockAuthorRegistration.Setup(m => m.Type).Returns(typeof(Author)); mockAuthorRegistration.Setup(m => m.ResourceTypeName).Returns("authors"); var registry = new ResourceTypeRegistry(); registry.AddRegistration(mockPostRegistration.Object); registry.AddRegistration(mockAuthorRegistration.Object); // Act var postReg = registry.GetRegistrationForResourceTypeName("posts"); var authorReg = registry.GetRegistrationForResourceTypeName("authors"); // Assert postReg.Should().BeSameAs(mockPostRegistration.Object); authorReg.Should().BeSameAs(mockAuthorRegistration.Object); }
private DefaultSortingTransformer GetTransformer() { var registrar = new ResourceTypeRegistrar(new DefaultNamingConventions(new PluralizationService())); var registry = new ResourceTypeRegistry(); registry.AddRegistration(registrar.BuildRegistration(typeof(Dummy), "dummies")); registry.AddRegistration(registrar.BuildRegistration(typeof(Dummy2), "dummy2s")); return(new DefaultSortingTransformer(registry)); }
public void TypeIsRegistered_returns_false_if_no_type_in_hierarchy_is_registered() { // Arrange var registry = new ResourceTypeRegistry(); // Act var isRegistered = registry.TypeIsRegistered(typeof(Comment)); // Assert isRegistered.Should().BeFalse(); }
public DocumentationOperationFilter(ResourceTypeRegistry registry, IStringLocalizer <DocumentationOperationFilter> localizer, IResourceDataService service, IOptionsMonitor <ApiOptions> options, ILogger <DocumentationOperationFilter> logger) { _registry = registry; _localizer = localizer; _service = service; _options = options; _logger = logger; }
private DefaultFilteringTransformer GetTransformer() { var pluralizationService = new PluralizationService(new Dictionary <string, string> { { "Dummy", "Dummies" } }); var registrar = new ResourceTypeRegistrar(new DefaultNamingConventions(pluralizationService)); var registry = new ResourceTypeRegistry(); registry.AddRegistration(registrar.BuildRegistration(typeof(Dummy))); registry.AddRegistration(registrar.BuildRegistration(typeof(RelatedItemWithId))); return(new DefaultFilteringTransformer(registry)); }
public void GetRegistrationForResourceTypeName_fails_when_getting_unregistered_type_name() { // Arrange var registry = new ResourceTypeRegistry(); // Act Action action = () => { registry.GetRegistrationForResourceTypeName("posts"); }; // Assert action.ShouldThrow <TypeRegistrationNotFoundException>().WithMessage("No type registration was found for the type name \"posts\"."); }
public void GetRegistrationForType_gets_registration_for_closest_registered_base_type_for_unregistered_type() { // Arrange var mockPostRegistration = new Mock <IResourceTypeRegistration>(MockBehavior.Strict); mockPostRegistration.Setup(m => m.Type).Returns(typeof(Post)); mockPostRegistration.Setup(m => m.ResourceTypeName).Returns("posts"); var registry = new ResourceTypeRegistry(); registry.AddRegistration(mockPostRegistration.Object); // Act var registration = registry.GetRegistrationForType(typeof(DerivedPost)); // Assert registration.Type.Should().Be(typeof(Post)); }
public void TypeIsRegistered_returns_true_if_parent_type_is_registered() { // Arrange var mockPostRegistration = new Mock <IResourceTypeRegistration>(MockBehavior.Strict); mockPostRegistration.Setup(m => m.Type).Returns(typeof(Post)); mockPostRegistration.Setup(m => m.ResourceTypeName).Returns("posts"); var registry = new ResourceTypeRegistry(); registry.AddRegistration(mockPostRegistration.Object); // Act var isRegistered = registry.TypeIsRegistered(typeof(DerivedPost)); // Assert isRegistered.Should().BeTrue(); }
protected override void Load(ContainerBuilder builder) { // Register resource types var registry = new ResourceTypeRegistry(); foreach (var resourceTypeConfiguration in _jsonApiConfiguration.ResourceTypeConfigurations) { var resourceTypeRegistration = resourceTypeConfiguration.BuildResourceTypeRegistration(); registry.AddRegistration(resourceTypeRegistration); var configuration = resourceTypeConfiguration; builder.Register(c => configuration) .Keyed <IResourceTypeConfiguration>(resourceTypeRegistration.Type) .Keyed <IResourceTypeConfiguration>(resourceTypeRegistration.ResourceTypeName) .SingleInstance(); if (resourceTypeConfiguration.DocumentMaterializerType != null) { builder.RegisterType(resourceTypeConfiguration.DocumentMaterializerType); } if (resourceTypeConfiguration.ResourceCollectionResolverType != null) { builder.RegisterType(resourceTypeConfiguration.ResourceCollectionResolverType); } foreach (var relationship in resourceTypeRegistration.Relationships) { IResourceTypeRelationshipConfiguration relationshipConfiguration; if (resourceTypeConfiguration.RelationshipConfigurations .TryGetValue(relationship.Property.Name, out relationshipConfiguration)) { if (relationshipConfiguration.MaterializerType != null) { builder.RegisterType(relationshipConfiguration.MaterializerType); continue; } } // They didn't set an explicit materializer. See if they specified a factory for this resource type. if (configuration.RelatedResourceMaterializerTypeFactory == null) { continue; } var materializerType = configuration.RelatedResourceMaterializerTypeFactory(relationship); builder.RegisterType(materializerType); } } builder.Register(c => registry).As <IResourceTypeRegistry>().SingleInstance(); builder.Register(c => { var context = c.Resolve <IComponentContext>(); Func <string, IDocumentMaterializer> factory = resourceTypeName => { var configuration = context.ResolveKeyed <IResourceTypeConfiguration>(resourceTypeName); var registration = registry.GetRegistrationForResourceTypeName(resourceTypeName); var parameters = new Parameter[] { new TypedParameter(typeof(IResourceTypeRegistration), registration) }; if (configuration.ResourceCollectionResolverType != null) { var collectionResolver = context.Resolve(configuration.ResourceCollectionResolverType, parameters); parameters = new Parameter[] { new TypedParameter(typeof(IResourceTypeRegistration), registration), new NamedParameter("collectionResolver", collectionResolver), }; } if (configuration.DocumentMaterializerType != null) { return((IDocumentMaterializer)context.Resolve(configuration.DocumentMaterializerType, parameters)); } return(context.Resolve <IDocumentMaterializer>(parameters)); }; return(factory); }); builder.Register(c => { var context = c.Resolve <IComponentContext>(); Func <Type, IDocumentMaterializer> factory = clrType => { var configuration = context.ResolveKeyed <IResourceTypeConfiguration>(clrType); var registration = registry.GetRegistrationForType(clrType); var parameters = new List <Parameter> { new TypedParameter(typeof(IResourceTypeRegistration), registration) }; // add parameter for collectionResolver if (configuration.ResourceCollectionResolverType != null) { var collectionResolver = context.Resolve(configuration.ResourceCollectionResolverType, parameters); parameters.Add(new NamedParameter("collectionResolver", collectionResolver)); } if (configuration.DocumentMaterializerType != null) { return((IDocumentMaterializer)context.Resolve(configuration.DocumentMaterializerType, parameters)); } return(context.Resolve <IDocumentMaterializer>(parameters)); }; return(factory); }); builder.Register(c => { var context = c.Resolve <IComponentContext>(); Func <string, string, IRelatedResourceDocumentMaterializer> factory = (resourceTypeName, relationshipName) => { var configuration = context.ResolveKeyed <IResourceTypeConfiguration>(resourceTypeName); var registration = registry.GetRegistrationForResourceTypeName(resourceTypeName); var relationship = registration.GetFieldByName(relationshipName) as ResourceTypeRelationship; if (relationship == null) { throw JsonApiException.CreateForNotFound( string.Format("No relationship `{0}` exists for the resource type `{1}`.", relationshipName, resourceTypeName)); } var parameters = new List <Parameter> { new TypedParameter(typeof(IResourceTypeRegistration), registration), new TypedParameter(typeof(ResourceTypeRelationship), relationship) }; // add parameter for collectionResolver if (context.IsRegisteredWithKey <IResourceTypeConfiguration>(relationship.RelatedType)) { var relConfiguration = context.ResolveKeyed <IResourceTypeConfiguration>(relationship.RelatedType); if (relConfiguration.ResourceCollectionResolverType != null) { var collectionResolver = context.Resolve(relConfiguration.ResourceCollectionResolverType, parameters); parameters.Add(new NamedParameter("collectionResolver", collectionResolver)); } } // First, see if they have set an explicit materializer for this relationship IResourceTypeRelationshipConfiguration relationshipConfiguration; if (configuration.RelationshipConfigurations.TryGetValue(relationship.Property.Name, out relationshipConfiguration) && relationshipConfiguration.MaterializerType != null) { return((IRelatedResourceDocumentMaterializer)context.Resolve(relationshipConfiguration.MaterializerType, parameters)); } // They didn't set an explicit materializer. See if they specified a factory for this resource type. if (configuration.RelatedResourceMaterializerTypeFactory != null) { var materializerType = configuration.RelatedResourceMaterializerTypeFactory(relationship); return((IRelatedResourceDocumentMaterializer)context.Resolve(materializerType, parameters)); } return(context.Resolve <IRelatedResourceDocumentMaterializer>(parameters)); }; return(factory); }); builder.RegisterType <JsonApiHttpConfiguration>().SingleInstance(); if (_jsonApiConfiguration.CustomBaseUrlService != null) { builder.Register(c => _jsonApiConfiguration.CustomBaseUrlService).As <IBaseUrlService>().SingleInstance(); } else { builder.RegisterType <BaseUrlService>().As <IBaseUrlService>().SingleInstance(); } builder.RegisterType <DocumentMaterializerLocator>().As <IDocumentMaterializerLocator>().InstancePerRequest(); // Serialization builder.RegisterType <MetadataFormatter>().As <IMetadataFormatter>().SingleInstance(); builder.RegisterType <LinkFormatter>().As <ILinkFormatter>().SingleInstance(); builder.RegisterType <ResourceLinkageFormatter>().As <IResourceLinkageFormatter>().SingleInstance(); builder.RegisterType <RelationshipObjectFormatter>().As <IRelationshipObjectFormatter>().SingleInstance(); builder.RegisterType <ResourceObjectFormatter>().As <IResourceObjectFormatter>().SingleInstance(); builder.RegisterType <SingleResourceDocumentFormatter>().As <ISingleResourceDocumentFormatter>().SingleInstance(); builder.RegisterType <ResourceCollectionDocumentFormatter>().As <IResourceCollectionDocumentFormatter>().SingleInstance(); builder.RegisterType <ErrorFormatter>().As <IErrorFormatter>().SingleInstance(); builder.RegisterType <ErrorDocumentFormatter>().As <IErrorDocumentFormatter>().SingleInstance(); // Queryable transforms builder.RegisterType <SynchronousEnumerationTransformer>().As <IQueryableEnumerationTransformer>().SingleInstance(); builder.RegisterType <DefaultFilteringTransformer>().As <IQueryableFilteringTransformer>().SingleInstance(); builder.RegisterType <DefaultSortingTransformer>().As <IQueryableSortingTransformer>().SingleInstance(); builder.RegisterType <DefaultPaginationTransformer>().As <IQueryablePaginationTransformer>().SingleInstance(); // Document building builder.Register(c => _jsonApiConfiguration.LinkConventions).As <ILinkConventions>().SingleInstance(); builder.RegisterType <JsonApiFormatter>().SingleInstance(); builder.RegisterType <RegistryDrivenResourceCollectionDocumentBuilder>().As <IResourceCollectionDocumentBuilder>().SingleInstance(); builder.RegisterType <RegistryDrivenSingleResourceDocumentBuilder>().As <ISingleResourceDocumentBuilder>().SingleInstance(); builder.RegisterType <FallbackDocumentBuilder>().As <IFallbackDocumentBuilder>().SingleInstance(); builder.RegisterType <ErrorDocumentBuilder>().As <IErrorDocumentBuilder>().SingleInstance(); builder.RegisterType <FallbackDocumentBuilderAttribute>().SingleInstance(); builder.RegisterType <JsonApiExceptionFilterAttribute>().SingleInstance(); builder.RegisterType <DefaultQueryableResourceCollectionDocumentBuilder>().As <IQueryableResourceCollectionDocumentBuilder>(); // Misc builder.RegisterType <DefaultSortExpressionExtractor>().As <ISortExpressionExtractor>().SingleInstance(); builder.RegisterType <DefaultIncludeExpressionExtractor>().As <IIncludeExpressionExtractor>().SingleInstance(); }
/// <summary> /// Registers validators, serializers and instance factories of all known extended resource data types. /// /// Invoke this method as part of your application's startup process before using any other part of the Maestro API /// </summary> /// <remarks> /// This only needs to be called once, and should generally be done as part of your application's startup routine /// </remarks> public static void Initialize() { //By default the ObjectFactory, ResourceTypeRegistry and ResourceValidatorSet only //support v1.0.0 of all resource types. To support additional types we need to inject //this information as part of the consuming application's init/startup process // //This is our application's entry point, so we do this here. //Layer Definition 1.1.0 ResourceValidatorSet.RegisterValidator(new Ldf110.LayerDefinitionValidator()); ResourceTypeRegistry.RegisterResource( new ResourceTypeDescriptor(ResourceTypes.LayerDefinition.ToString(), "1.1.0"), new ResourceSerializationCallback(Ldf110.LdfEntryPoint.Serialize), new ResourceDeserializationCallback(Ldf110.LdfEntryPoint.Deserialize)); ObjectFactory.RegisterLayerFactoryMethod(new Version(1, 1, 0), new LayerCreatorFunc(Ldf110.LdfEntryPoint.CreateDefault)); //Layer Definition 1.2.0 ResourceValidatorSet.RegisterValidator(new Ldf120.LayerDefinitionValidator()); ResourceTypeRegistry.RegisterResource( new ResourceTypeDescriptor(ResourceTypes.LayerDefinition.ToString(), "1.2.0"), new ResourceSerializationCallback(Ldf120.LdfEntryPoint.Serialize), new ResourceDeserializationCallback(Ldf120.LdfEntryPoint.Deserialize)); ObjectFactory.RegisterLayerFactoryMethod(new Version(1, 2, 0), new LayerCreatorFunc(Ldf120.LdfEntryPoint.CreateDefault)); //Layer Definition 1.3.0 ResourceValidatorSet.RegisterValidator(new Ldf130.LayerDefinitionValidator()); ResourceTypeRegistry.RegisterResource( new ResourceTypeDescriptor(ResourceTypes.LayerDefinition.ToString(), "1.3.0"), new ResourceSerializationCallback(Ldf130.LdfEntryPoint.Serialize), new ResourceDeserializationCallback(Ldf130.LdfEntryPoint.Deserialize)); ObjectFactory.RegisterLayerFactoryMethod(new Version(1, 3, 0), new LayerCreatorFunc(Ldf130.LdfEntryPoint.CreateDefault)); //Layer Definition 2.3.0 ResourceValidatorSet.RegisterValidator(new Ldf230.LayerDefinitionValidator()); ResourceTypeRegistry.RegisterResource( new ResourceTypeDescriptor(ResourceTypes.LayerDefinition.ToString(), "2.3.0"), new ResourceSerializationCallback(Ldf230.LdfEntryPoint.Serialize), new ResourceDeserializationCallback(Ldf230.LdfEntryPoint.Deserialize)); ObjectFactory.RegisterLayerFactoryMethod(new Version(2, 3, 0), new LayerCreatorFunc(Ldf230.LdfEntryPoint.CreateDefault)); //Layer Definition 2.4.0 ResourceValidatorSet.RegisterValidator(new Ldf240.LayerDefinitionValidator()); ResourceTypeRegistry.RegisterResource( new ResourceTypeDescriptor(ResourceTypes.LayerDefinition.ToString(), "2.4.0"), new ResourceSerializationCallback(Ldf240.LdfEntryPoint.Serialize), new ResourceDeserializationCallback(Ldf240.LdfEntryPoint.Deserialize)); ObjectFactory.RegisterLayerFactoryMethod(new Version(2, 4, 0), new LayerCreatorFunc(Ldf240.LdfEntryPoint.CreateDefault)); //Load Procedure 1.1.0 ResourceValidatorSet.RegisterValidator(new Lp110.LoadProcedureValidator()); ResourceTypeRegistry.RegisterResource( new ResourceTypeDescriptor(ResourceTypes.LoadProcedure.ToString(), "1.1.0"), new ResourceSerializationCallback(Lp110.LoadProcEntryPoint.Serialize), new ResourceDeserializationCallback(Lp110.LoadProcEntryPoint.Deserialize)); //Load Procedure 1.1.0 schema offers nothing new for the ones we want to support, so nothing to register //with the ObjectFactory //Load Procedure 2.2.0 ResourceValidatorSet.RegisterValidator(new Lp220.LoadProcedureValidator()); ResourceTypeRegistry.RegisterResource( new ResourceTypeDescriptor(ResourceTypes.LoadProcedure.ToString(), "2.2.0"), new ResourceSerializationCallback(Lp220.LoadProcEntryPoint.Serialize), new ResourceDeserializationCallback(Lp220.LoadProcEntryPoint.Deserialize)); ObjectFactory.RegisterLoadProcedureFactoryMethod(LoadType.Sqlite, new LoadProcCreatorFunc(Lp220.LoadProcEntryPoint.CreateDefaultSqlite)); //Web Layout 1.1.0 ResourceValidatorSet.RegisterValidator(new WL110.WebLayoutValidator()); ResourceTypeRegistry.RegisterResource( new ResourceTypeDescriptor(ResourceTypes.WebLayout.ToString(), "1.1.0"), new ResourceSerializationCallback(WL110.WebLayoutEntryPoint.Serialize), new ResourceDeserializationCallback(WL110.WebLayoutEntryPoint.Deserialize)); ObjectFactory.RegisterWebLayoutFactoryMethod(new Version(1, 1, 0), new WebLayoutCreatorFunc(WL110.WebLayoutEntryPoint.CreateDefault)); //Web Layout 2.4.0 ResourceValidatorSet.RegisterValidator(new WL240.WebLayoutValidator()); ResourceTypeRegistry.RegisterResource( new ResourceTypeDescriptor(ResourceTypes.WebLayout.ToString(), "2.4.0"), new ResourceSerializationCallback(WL240.WebLayoutEntryPoint.Serialize), new ResourceDeserializationCallback(WL240.WebLayoutEntryPoint.Deserialize)); ObjectFactory.RegisterWebLayoutFactoryMethod(new Version(2, 4, 0), new WebLayoutCreatorFunc(WL240.WebLayoutEntryPoint.CreateDefault)); //Web Layout 2.6.0 ResourceValidatorSet.RegisterValidator(new WL260.WebLayoutValidator()); ResourceTypeRegistry.RegisterResource( new ResourceTypeDescriptor(ResourceTypes.WebLayout.ToString(), "2.6.0"), new ResourceSerializationCallback(WL260.WebLayoutEntryPoint.Serialize), new ResourceDeserializationCallback(WL260.WebLayoutEntryPoint.Deserialize)); ObjectFactory.RegisterWebLayoutFactoryMethod(new Version(2, 6, 0), new WebLayoutCreatorFunc(WL260.WebLayoutEntryPoint.CreateDefault)); //Symbol Definition 1.1.0 ResourceValidatorSet.RegisterValidator(new Sym110.SymbolDefinitionValidator()); ResourceTypeRegistry.RegisterResource( new ResourceTypeDescriptor(ResourceTypes.SymbolDefinition.ToString(), "1.1.0"), new ResourceSerializationCallback(Sym110.SymbolDefEntryPoint.Serialize), new ResourceDeserializationCallback(Sym110.SymbolDefEntryPoint.Deserialize)); ObjectFactory.RegisterCompoundSymbolFactoryMethod(new Version(1, 1, 0), new CompoundSymbolDefCreatorFunc(Sym110.SymbolDefEntryPoint.CreateDefaultCompound)); ObjectFactory.RegisterSimpleSymbolFactoryMethod(new Version(1, 1, 0), new SimpleSymbolDefCreatorFunc(Sym110.SymbolDefEntryPoint.CreateDefaultSimple)); //Symbol Definition 2.4.0 ResourceValidatorSet.RegisterValidator(new Sym240.SymbolDefinitionValidator()); ResourceTypeRegistry.RegisterResource( new ResourceTypeDescriptor(ResourceTypes.SymbolDefinition.ToString(), "2.4.0"), new ResourceSerializationCallback(Sym240.SymbolDefEntryPoint.Serialize), new ResourceDeserializationCallback(Sym240.SymbolDefEntryPoint.Deserialize)); ObjectFactory.RegisterCompoundSymbolFactoryMethod(new Version(2, 4, 0), new CompoundSymbolDefCreatorFunc(Sym240.SymbolDefEntryPoint.CreateDefaultCompound)); ObjectFactory.RegisterSimpleSymbolFactoryMethod(new Version(2, 4, 0), new SimpleSymbolDefCreatorFunc(Sym240.SymbolDefEntryPoint.CreateDefaultSimple)); //Map Definition 2.3.0 ResourceValidatorSet.RegisterValidator(new Mdf230.MapDefinitionValidator()); ResourceTypeRegistry.RegisterResource( new ResourceTypeDescriptor(ResourceTypes.MapDefinition.ToString(), "2.3.0"), new ResourceSerializationCallback(Mdf230.MdfEntryPoint.Serialize), new ResourceDeserializationCallback(Mdf230.MdfEntryPoint.Deserialize)); ObjectFactory.RegisterMapDefinitionFactoryMethod(new Version(2, 3, 0), new MapDefinitionCreatorFunc(Mdf230.MdfEntryPoint.CreateDefault)); //Map Definition 2.4.0 ResourceValidatorSet.RegisterValidator(new Mdf240.MapDefinitionValidator()); ResourceTypeRegistry.RegisterResource( new ResourceTypeDescriptor(ResourceTypes.MapDefinition.ToString(), "2.4.0"), new ResourceSerializationCallback(Mdf240.MdfEntryPoint.Serialize), new ResourceDeserializationCallback(Mdf240.MdfEntryPoint.Deserialize)); ObjectFactory.RegisterMapDefinitionFactoryMethod(new Version(2, 4, 0), new MapDefinitionCreatorFunc(Mdf240.MdfEntryPoint.CreateDefault)); //Watermark Definition 2.3.0 ResourceTypeRegistry.RegisterResource( new ResourceTypeDescriptor(ResourceTypes.WatermarkDefinition.ToString(), "2.3.0"), new ResourceSerializationCallback(Wdf230.WdfEntryPoint.Serialize), new ResourceDeserializationCallback(Wdf230.WdfEntryPoint.Deserialize)); ObjectFactory.RegisterWatermarkDefinitionFactoryMethod(new Version(2, 3, 0), new WatermarkCreatorFunc(Wdf230.WdfEntryPoint.CreateDefault)); //Watermark Definition 2.4.0 ResourceTypeRegistry.RegisterResource( new ResourceTypeDescriptor(ResourceTypes.WatermarkDefinition.ToString(), "2.4.0"), new ResourceSerializationCallback(Wdf240.WdfEntryPoint.Serialize), new ResourceDeserializationCallback(Wdf240.WdfEntryPoint.Deserialize)); ObjectFactory.RegisterWatermarkDefinitionFactoryMethod(new Version(2, 4, 0), new WatermarkCreatorFunc(Wdf240.WdfEntryPoint.CreateDefault)); }