public void TestDomReadWriteResourceCreateWithPersonResource() { // Arrange var serviceModel = ClrSampleData.ServiceModelWithBlogResourceTypes; var personResourceType = serviceModel.GetResourceType <Person>(); var personResourceIdentity = personResourceType.ResourceIdentityInfo; var personFirstNameAttribute = personResourceType.GetClrAttributeInfo(StaticReflection.GetMemberName <Person>(x => x.FirstName)); var personLastNameAttribute = personResourceType.GetClrAttributeInfo(StaticReflection.GetMemberName <Person>(x => x.LastName)); var personTwitterAttribute = personResourceType.GetClrAttributeInfo(StaticReflection.GetMemberName <Person>(x => x.Twitter)); var expected = ApiSampleData.PersonResource; // Act var actual = DomReadWriteResource.Create( DomType.CreateFromResourceType(personResourceType), DomId.CreateFromApiResourceIdentity(personResourceType, expected), DomAttributes.Create( DomAttribute.CreateFromApiResource(personFirstNameAttribute, expected), DomAttribute.CreateFromApiResource(personLastNameAttribute, expected), DomAttribute.CreateFromApiResource(personTwitterAttribute, expected)), DomReadWriteRelationships.Create( DomReadWriteRelationship.Create(ApiSampleData.PersonToCommentsRel, DomReadWriteLinks.Create( DomReadWriteLink.Create(Keywords.Self, DomHRef.Create(ApiSampleData.PersonToRelationshipsToCommentsHRef)), DomReadWriteLink.Create(Keywords.Related, DomHRef.Create(ApiSampleData.PersonToCommentsHRef))))), DomReadWriteLinks.Create( DomReadWriteLink.Create(Keywords.Self, DomHRef.Create(ApiSampleData.PersonHRef))), DomReadOnlyMeta.Create(ApiSampleData.ResourceMeta)); this.OutputDomTree(actual); // Assert DomReadWriteResourceAssert.Equal(expected, actual); }
public void TestDomReadWriteResourceCreateWithCommentResource() { // Arrange var serviceModel = ClrSampleData.ServiceModelWithBlogResourceTypes; var commentResourceType = serviceModel.GetResourceType <Comment>(); var commentResourceIdentity = commentResourceType.ResourceIdentityInfo; var commentBodyAttribute = commentResourceType.GetClrAttributeInfo(StaticReflection.GetMemberName <Comment>(x => x.Body)); var expected = ApiSampleData.CommentResource; // Act var actual = DomReadWriteResource.Create( DomType.CreateFromResourceType(commentResourceType), DomId.CreateFromApiResourceIdentity(commentResourceType, expected), DomAttributes.Create( DomAttribute.CreateFromApiResource(commentBodyAttribute, expected)), DomReadWriteRelationships.Create( DomReadWriteRelationship.Create(ApiSampleData.CommentToAuthorRel, DomReadWriteLinks.Create( DomReadWriteLink.Create(Keywords.Self, DomHRef.Create(ApiSampleData.CommentToRelationshipsToAuthorHRef)), DomReadWriteLink.Create(Keywords.Related, DomHRef.Create(ApiSampleData.CommentToAuthorHRef))))), DomReadWriteLinks.Create( DomReadWriteLink.Create(Keywords.Self, DomHRef.Create(ApiSampleData.CommentHRef))), DomReadOnlyMeta.Create(ApiSampleData.ResourceMeta)); this.OutputDomTree(actual); // Assert DomReadWriteResourceAssert.Equal(expected, actual); }
public void TestDomReadWriteResourceCreateWithBlogResource() { // Arrange var serviceModel = ClrSampleData.ServiceModelWithBlogResourceTypes; var blogResourceType = serviceModel.GetResourceType <Blog>(); var blogResourceIdentity = blogResourceType.ResourceIdentityInfo; var blogNameAttribute = blogResourceType.GetClrAttributeInfo(StaticReflection.GetMemberName <Blog>(x => x.Name)); var expectedBlog = ApiSampleData.BlogResource; // Act var actual = DomReadWriteResource.Create( DomType.CreateFromResourceType(blogResourceType), DomId.CreateFromApiResourceIdentity(blogResourceType, expectedBlog), DomAttributes.Create( DomAttribute.CreateFromApiResource(blogNameAttribute, expectedBlog)), DomReadWriteRelationships.Create( DomReadWriteRelationship.Create(ApiSampleData.BlogToArticlesRel, DomReadWriteLinks.Create( DomReadWriteLink.Create(Keywords.Self, DomHRef.Create(ApiSampleData.BlogToRelationshipsToArticlesHRef)), DomReadWriteLink.Create(Keywords.Related, DomHRef.Create(ApiSampleData.BlogToArticlesHRef))))), DomReadWriteLinks.Create( DomReadWriteLink.Create(Keywords.Self, DomHRef.Create(ApiSampleData.BlogHRef))), DomReadOnlyMeta.Create(ApiSampleData.ResourceMeta)); this.OutputDomTree(actual); // Assert DomReadWriteResourceAssert.Equal(expectedBlog, actual); }
public void TestDomReadWriteResourceCreateWithArticleResourceWithResourceLinkage() { // Arrange var serviceModel = ClrSampleData.ServiceModelWithBlogResourceTypes; var articleResourceType = serviceModel.GetResourceType <Article>(); var articleTitleAttribute = articleResourceType.GetClrAttributeInfo(StaticReflection.GetMemberName <Article>(x => x.Title)); var commentResourceType = serviceModel.GetResourceType <Comment>(); var personResourceType = serviceModel.GetResourceType <Person>(); var expectedArticle = ApiSampleData.ArticleResourceWithResourceLinkage; var expectedAuthor = ApiSampleData.PersonResource; var expectedComment1 = ApiSampleData.CommentResource1; var expectedComment2 = ApiSampleData.CommentResource2; // Act var actual = DomReadWriteResource.Create( DomType.CreateFromResourceType(articleResourceType), DomId.CreateFromApiResourceIdentity(articleResourceType, expectedArticle), DomAttributes.Create( DomAttribute.CreateFromApiResource(articleTitleAttribute, expectedArticle)), DomReadWriteRelationships.Create( DomReadWriteRelationship.Create(ApiSampleData.ArticleToAuthorRel, DomReadWriteLinks.Create( DomReadWriteLink.Create(Keywords.Self, DomHRef.Create(ApiSampleData.ArticleToRelationshipsToAuthorHRef)), DomReadWriteLink.Create(Keywords.Related, DomHRef.Create(ApiSampleData.ArticleToAuthorHRef))), DomData.CreateFromResourceIdentifier( DomReadWriteResourceIdentifier.Create( DomType.CreateFromResourceType(personResourceType), DomId.CreateFromApiResourceIdentity(personResourceType, expectedAuthor)))), DomReadWriteRelationship.Create(ApiSampleData.ArticleToCommentsRel, DomReadWriteLinks.Create( DomReadWriteLink.Create(Keywords.Self, DomHRef.Create(ApiSampleData.ArticleToRelationshipsToCommentsHRef)), DomReadWriteLink.Create(Keywords.Related, DomHRef.Create(ApiSampleData.ArticleToCommentsHRef))), DomDataCollection.CreateFromResourceIdentifiers( DomReadWriteResourceIdentifier.Create( DomType.CreateFromResourceType(commentResourceType), DomId.CreateFromApiResourceIdentity(commentResourceType, expectedComment1)), DomReadWriteResourceIdentifier.Create( DomType.CreateFromResourceType(commentResourceType), DomId.CreateFromApiResourceIdentity(commentResourceType, expectedComment2))))), DomReadWriteLinks.Create( DomReadWriteLink.Create(Keywords.Canonical, DomHRef.Create(ApiSampleData.ArticleHRef)), DomReadWriteLink.Create(Keywords.Self, DomHRef.Create(ApiSampleData.ArticleHRef))), DomReadOnlyMeta.Create(ApiSampleData.ResourceMeta)); this.OutputDomTree(actual); // Assert DomReadWriteResourceAssert.Equal(expectedArticle, actual); }
private void InitializeResource(IContainerNode <DomNodeType> domContainerNode, object clrResource) { Contract.Requires(clrResource != null); if (clrResource == null) { return; } // Map the incoming CLR resource to the DOM resource node. var resourceType = this.ResourceType; var domReadWriteResource = DomReadWriteResource.Create(); resourceType.MapClrTypeToDomResource(domReadWriteResource); resourceType.MapClrIdToDomResource(domReadWriteResource, clrResource); var domResource = (IDomResource)domReadWriteResource; var domResourceKey = domResource.CreateDomResourceKey(); // Do not add the DOM read-write resource node if it already has been added to the DOM document. if (this.DocumentBuilderContext.ContainsDomReadWriteResource(domResourceKey)) { return; } // Add the DOM read/write resource nodes to the DOM document. this.DocumentBuilderContext.AddDomReadWriteResource(domResourceKey, domReadWriteResource); domContainerNode.Add(domReadWriteResource); // Finish mapping the DOM read/write resource attributes nodes to the DOM document. var queryParameters = this.DocumentBuilderContext.QueryParameters; var apiType = resourceType.ResourceIdentityInfo.ApiType; var useSparseFieldsets = this.DocumentBuilderContext.SparseFieldsetsEnabled && queryParameters.ContainsField(apiType); if (!useSparseFieldsets) { resourceType.MapClrAttributesToDomResource(domReadWriteResource, clrResource); } else { resourceType.MapClrAttributesToDomResource(domReadWriteResource, clrResource, (x, y) => queryParameters.ContainsField(x, y)); } this.BuildingResource = true; this.DomReadWriteResource = domReadWriteResource; this.ClrResource = clrResource; }
// PRIVATE METHODS ////////////////////////////////////////////////// #region Methods private void InitializeResource(IContainerNode <DomNodeType> domContainerNode, TResource clrResource) { // Add the DOM read/write resource node to the DOM document. var domReadWriteResource = DomReadWriteResource.Create(); domContainerNode.Add(domReadWriteResource); // Map the incoming CLR resource to the DOM resource node. var serviceModel = this.ServiceModel; var clrResourceType = typeof(TResource); var resourceType = serviceModel.GetResourceType(clrResourceType); resourceType.MapClrTypeToDomResource(domReadWriteResource); resourceType.MapClrIdToDomResource(domReadWriteResource, clrResource); resourceType.MapClrAttributesToDomResource(domReadWriteResource, clrResource); this.ResourceType = resourceType; this.DomReadWriteResource = domReadWriteResource; this.ClrResource = clrResource; }
private void InitializeResource(IContainerNode <DomNodeType> domContainerNode, TResource clrResource) { this.BuildingResource = false; if (clrResource == null) { return; } // Map the incoming CLR resource to the DOM resource node. var serviceModel = this.ServiceModel; var clrResourceType = typeof(TResource); var resourceType = serviceModel.GetResourceType(clrResourceType); var domReadWriteResource = DomReadWriteResource.Create(); resourceType.MapClrTypeToDomResource(domReadWriteResource); resourceType.MapClrIdToDomResource(domReadWriteResource, clrResource); var domResource = (IDomResource)domReadWriteResource; var domResourceKey = domResource.CreateDomResourceKey(); // Do not add the DOM read-write resource node if it already has been added to the DOM document. if (this.DocumentBuilderContext.ContainsDomReadWriteResource(domResourceKey)) { return; } // Add the DOM read/write resource nodes to the DOM document. this.DocumentBuilderContext.AddDomReadWriteResource(domResourceKey, domReadWriteResource); domContainerNode.Add(domReadWriteResource); // Finish mapping the DOM read/write resource attributes nodes to the DOM document. resourceType.MapClrAttributesToDomResource(domReadWriteResource, clrResource); this.BuildingResource = true; this.DomReadWriteResource = domReadWriteResource; this.ClrResource = clrResource; }
private void InitializeResourceCollection(IContainerNode <DomNodeType> domContainerNode, IEnumerable <TResource> clrResourceCollection) { // Map the incoming CLR resources to the DOM equilvalent nodes. var serviceModel = this.ServiceModel; var clrResourceType = typeof(TResource); var resourceType = serviceModel.GetResourceType(clrResourceType); var domResourceTupleCollection1 = clrResourceCollection .EmptyIfNull() .Where(clrResource => clrResource != null) .Select(clrResource => { var domReadWriteResource = DomReadWriteResource.Create(); resourceType.MapClrTypeToDomResource(domReadWriteResource); resourceType.MapClrIdToDomResource(domReadWriteResource, clrResource); var domResource = (IDomResource)domReadWriteResource; var domResourceKey = domResource.CreateDomResourceKey(); var domResourceTuple = new Tuple <ResourceIdentifier, DomReadWriteResource, TResource>(domResourceKey, domReadWriteResource, clrResource); return(domResourceTuple); }) .ToList(); // Add the DOM read/write resource nodes to the DOM document. var domResourceTupleCollection2 = new List <Tuple <ResourceIdentifier, DomReadWriteResource, TResource> >(); foreach (var domResourceTuple in domResourceTupleCollection1) { // Add the DOM read/write resource nodes to the DOM document. var domResourceKey = domResourceTuple.Item1; var domReadWriteResource = domResourceTuple.Item2; // Do not add the DOM read-write resource node if it already has been added to the DOM document. if (this.DocumentBuilderContext.ContainsDomReadWriteResource(domResourceKey)) { continue; } this.DocumentBuilderContext.AddDomReadWriteResource(domResourceKey, domReadWriteResource); domContainerNode.Add(domReadWriteResource); // Finish mapping the DOM read/write resource attributes nodes to the DOM document. var clrResource = domResourceTuple.Item3; resourceType.MapClrAttributesToDomResource(domReadWriteResource, clrResource); // Keep track of the actual DOM read-write resource nodes added to the DOM document. domResourceTupleCollection2.Add(domResourceTuple); } var domReadWriteResourceCollection = domResourceTupleCollection2 .Select(domResourceTuple => domResourceTuple.Item2) .ToList(); this.DomReadWriteResourceCollection = domReadWriteResourceCollection; var clrResourceCollectionInternal = domResourceTupleCollection2 .Select(domResourceTuple => domResourceTuple.Item3) .ToList(); this.ClrResourceCollection = clrResourceCollectionInternal; }
private void InitializeResourceCollection(IContainerNode <DomNodeType> domContainerNode, Type clrResourceType, IEnumerable <object> clrResourceCollection) { Contract.Requires(clrResourceType != null); // ReSharper disable PossibleMultipleEnumeration if (clrResourceCollection == null || clrResourceCollection.Any() == false) { this.ClrResourceCollection = new List <object>(); return; } // Map the incoming CLR resources to the DOM equivalent nodes. var serviceModel = this.ServiceModel; var resourceType = serviceModel.GetResourceType(clrResourceType); var domResourceTupleCollection1 = clrResourceCollection .Where(clrResource => clrResource != null) .Select(clrResource => { var domReadWriteResource = DomReadWriteResource.Create(); resourceType.MapClrTypeToDomResource(domReadWriteResource); resourceType.MapClrIdToDomResource(domReadWriteResource, clrResource); var domResource = (IDomResource)domReadWriteResource; var domResourceKey = domResource.CreateDomResourceKey(); var domResourceTuple = new Tuple <ResourceIdentifier, DomReadWriteResource, object>(domResourceKey, domReadWriteResource, clrResource); return(domResourceTuple); }) .ToList(); // ReSharper restore PossibleMultipleEnumeration // Add the DOM read/write resource nodes to the DOM document. var queryParameters = this.DocumentBuilderContext.QueryParameters; var apiType = resourceType.ResourceIdentityInfo.ApiType; var useSparseFieldsets = this.DocumentBuilderContext.SparseFieldsetsEnabled && queryParameters.ContainsField(apiType); var domResourceTupleCollection2 = new List <Tuple <ResourceIdentifier, DomReadWriteResource, object> >(); foreach (var domResourceTuple in domResourceTupleCollection1) { // Add the DOM read/write resource nodes to the DOM document. var domResourceKey = domResourceTuple.Item1; var domReadWriteResource = domResourceTuple.Item2; // Do not add the DOM read-write resource node if it already has been added to the DOM document. if (this.DocumentBuilderContext.ContainsDomReadWriteResource(domResourceKey)) { continue; } this.DocumentBuilderContext.AddDomReadWriteResource(domResourceKey, domReadWriteResource); domContainerNode.Add(domReadWriteResource); // Finish mapping the DOM read/write resource attributes nodes to the DOM document. var clrResource = domResourceTuple.Item3; if (!useSparseFieldsets) { resourceType.MapClrAttributesToDomResource(domReadWriteResource, clrResource); } else { resourceType.MapClrAttributesToDomResource(domReadWriteResource, clrResource, (x, y) => queryParameters.ContainsField(x, y)); } // Keep track of the actual DOM read-write resource nodes added to the DOM document. domResourceTupleCollection2.Add(domResourceTuple); } var domReadWriteResourceCollection = domResourceTupleCollection2 .Select(domResourceTuple => domResourceTuple.Item2) .ToList(); this.DomReadWriteResourceCollection = domReadWriteResourceCollection; var clrResourceCollectionInternal = domResourceTupleCollection2 .Select(domResourceTuple => domResourceTuple.Item3) .ToList(); this.ClrResourceCollection = clrResourceCollectionInternal; this.BuildingResourceCollection = true; }