当前位置: 首页>>代码示例>>C#>>正文


C# ConfigurationBuilder.WithConvention方法代码示例

本文整理汇总了C#中ConfigurationBuilder.WithConvention方法的典型用法代码示例。如果您正苦于以下问题:C# ConfigurationBuilder.WithConvention方法的具体用法?C# ConfigurationBuilder.WithConvention怎么用?C# ConfigurationBuilder.WithConvention使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在ConfigurationBuilder的用法示例。


在下文中一共展示了ConfigurationBuilder.WithConvention方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: WithAllProperties_maps_properly

        public void WithAllProperties_maps_properly()
        {
            //Arrange
            var builder = new ConfigurationBuilder();
            builder
                .WithConvention(new DefaultPropertyScanningConvention())
                .WithConvention(new CamelCaseLinkNameConvention())
                .WithConvention(new PluralizedCamelCaseTypeConvention())
                .WithConvention(new SimpleLinkedIdConvention());

            //Act
            builder
                .Resource<Post>()
                .WithAllProperties();

            builder.Resource<Author>();
            builder.Resource<Comment>();

            var configuration = builder.Build();
            var postMapping = configuration.GetMapping(typeof(Post));

            //Assert
            postMapping.Relationships.Count.ShouldEqual(2);
            postMapping.Relationships.SingleOrDefault(l => l.RelatedBaseResourceType == "authors").ShouldNotBeNull();
            postMapping.Relationships.SingleOrDefault(l => l.RelatedBaseResourceType == "comments").ShouldNotBeNull();
            postMapping.PropertyGetters.Count.ShouldEqual(2);
            postMapping.PropertySetters.Count.ShouldEqual(2);
            postMapping.IdGetter.ShouldNotBeNull();
        }
开发者ID:FrontlineEducation,项目名称:Util-JsonApiSerializer,代码行数:29,代码来源:ConfigurationBuilderTest.cs

示例2: WithLinkedResource_maps_properly

        public void WithLinkedResource_maps_properly()
        {
            //Arrange
            var builder = new ConfigurationBuilder();
            builder
                .WithConvention(new CamelCaseLinkNameConvention())
                .WithConvention(new PluralizedCamelCaseTypeConvention())
                .WithConvention(new SimpleLinkedIdConvention());

            var post = new Post();
            var author = new Author
            {
                Posts = new List<Post> { post }
            };

            post.Author = author;
            post.AuthorId = 4;

            //Act
            builder
                .Resource<Post, PostsController>()
                .WithLinkedResource(p => p.Author);

            builder
                .Resource<Author, AuthorsController>()
                .WithLinkedResource(a => a.Posts);

            var configuration = builder.Build();
            var postMapping = configuration.GetMapping(typeof(Post));
            var authorMapping = configuration.GetMapping(typeof(Author));

            //Assert
            Assert.Equal(postMapping.Relationships.Count, 1);

            var linkToAuthor = postMapping.Relationships.Single();

            Assert.False(linkToAuthor.IsCollection);
            Assert.Equal(linkToAuthor.RelationshipName, "author");
            Assert.Equal(linkToAuthor.ParentType, typeof(Post));
            Assert.Equal(linkToAuthor.RelatedBaseType, typeof(Author));
            Assert.Same(linkToAuthor.RelatedResource(post), author);
            Assert.Equal(linkToAuthor.RelatedResourceId(post), 4);
            Assert.Same(linkToAuthor.ResourceMapping, authorMapping);
            Assert.Equal(authorMapping.Relationships.Count, 1);
            
            var linkToPosts = authorMapping.Relationships.Single();

            Assert.True(linkToPosts.IsCollection);
            Assert.Equal(linkToPosts.RelationshipName, "posts");
            Assert.Equal(linkToPosts.ParentType, typeof(Author));
            Assert.Equal(linkToPosts.RelatedBaseType, typeof(Post));
            Assert.Same(linkToPosts.RelatedResource(author), author.Posts);
            Assert.Null(linkToPosts.RelatedResourceId);
            Assert.Same(linkToPosts.ResourceMapping, postMapping);
            
        }
开发者ID:brainwipe,项目名称:NJsonApi,代码行数:56,代码来源:ConfigurationBuilderTest.cs

示例3: WithAllProperties_uses_conventions

        public void WithAllProperties_uses_conventions()
        {
            //Arrange
            const string testResourceType = "testResourceType";
            const string testLinkName = "testLinkName";
            string testname = "testName";

            Expression<Func<Post, object>> testIdExpression = p => 4;

            var builder = new ConfigurationBuilder();

            var linkNameConventionMock = A.Fake<ILinkNameConvention>();
            A.CallTo(() => linkNameConventionMock.GetLinkNameFromExpression(A<Expression<Func<Post, Author>>>._)).Returns(testLinkName);

            var resourceTypeMock = A.Fake<IResourceTypeConvention>();
            A.CallTo(() => resourceTypeMock.GetResourceTypeFromRepresentationType(A<Type>._)).Returns(testResourceType);

            var linkedIdConventionMock = A.Fake<ILinkIdConvention>();
            A.CallTo(() => linkedIdConventionMock.GetIdExpression(A<Expression<Func<Post, Author>>>._)).Returns(testIdExpression);

            var propertyScanningConventionMock = A.Fake<IPropertyScanningConvention>();
            A
                .CallTo(() => propertyScanningConventionMock.IsLinkedResource(A<PropertyInfo>
                    .That.Matches(pi => pi.Name == "Replies" || pi.Name == "Author")))
                .Returns(true);

            A.CallTo(() => propertyScanningConventionMock.IsPrimaryId(A<PropertyInfo>.That.Matches(pi => pi.Name == "Id"))).Returns(true);

            A.CallTo(() => propertyScanningConventionMock.GetPropertyName(A<PropertyInfo>.That.Matches(pi => pi.Name == "Title"))).Returns(testname);
            A.CallTo(() => propertyScanningConventionMock.GetPropertyName(A<PropertyInfo>.That.Matches(pi => pi.Name == "AuthorId"))).Returns("authorId");

            builder
                .WithConvention(propertyScanningConventionMock)
                .WithConvention(linkNameConventionMock)
                .WithConvention(resourceTypeMock)
                .WithConvention(linkedIdConventionMock);

            //Act
            builder
                .Resource<Post>()
                .WithAllProperties();

            builder
                .Resource<Author>()
                .WithResourceType(testResourceType);

            var configuration = builder.Build();
            var postMapping = configuration.GetMapping(typeof(Post));
            var link = postMapping.Relationships.Single();

            //Assert
            link.RelationshipName.ShouldEqual(testLinkName);
            link.RelatedBaseResourceType.ShouldEqual(testResourceType);
            link.RelatedResourceId(new Post()).ShouldEqual(4);
            postMapping.PropertyGetters.ContainsKey(testname).ShouldBeTrue();

            A.CallTo(() => propertyScanningConventionMock.IsLinkedResource(A<PropertyInfo>._)).MustHaveHappened();
            A.CallTo(() => propertyScanningConventionMock.IsPrimaryId(A<PropertyInfo>._)).MustHaveHappened();
            A.CallTo(() => propertyScanningConventionMock.ShouldIgnore(A<PropertyInfo>._)).MustHaveHappened();
            A.CallTo(() => propertyScanningConventionMock.ThrowOnUnmappedLinkedType).MustHaveHappened();
        }
开发者ID:FrontlineEducation,项目名称:Util-JsonApiSerializer,代码行数:61,代码来源:ConfigurationBuilderTest.cs

示例4: WithLinkedResource_uses_conventions

        public void WithLinkedResource_uses_conventions()
        {
            //Arrange
            const string testResourceType = "testResourceType";
            const string testLinkName = "testLinkName";
            Expression<Func<Post, object>> testIdExpression = p => 4;

            var builder = new ConfigurationBuilder();

            var linkNameConventionMock = A.Fake<ILinkNameConvention>();
            A.CallTo(() => linkNameConventionMock.GetLinkNameFromExpression(A<Expression<Func<Post, Author>>>._)).Returns(testLinkName);

            var resourceTypeMock = A.Fake<IResourceTypeConvention>();
            A.CallTo(() => resourceTypeMock.GetResourceTypeFromRepresentationType(A<Type>._)).Returns(testResourceType);

            var linkedIdConventionMock = A.Fake<ILinkIdConvention>();
            A.CallTo(() => linkedIdConventionMock.GetIdExpression(A<Expression<Func<Post, Author>>>._)).Returns(testIdExpression);

            builder
                .WithConvention(linkNameConventionMock)
                .WithConvention(resourceTypeMock)
                .WithConvention(linkedIdConventionMock);

            //Act
            builder
                .Resource<Post>()
                .WithLinkedResource(p => p.Author);

            builder
                .Resource<Author>()
                .WithResourceType(testResourceType);

            var configuration = builder.Build();
            var postMapping = configuration.GetMapping(typeof(Post));
            var link = postMapping.Relationships.Single();

            //Assert
            link.RelationshipName.ShouldEqual(testLinkName);
            link.RelatedBaseResourceType.ShouldEqual(testResourceType);
            link.RelatedResourceId(new Post()).ShouldEqual(4);
        }
开发者ID:FrontlineEducation,项目名称:Util-JsonApiSerializer,代码行数:41,代码来源:ConfigurationBuilderTest.cs

示例5: WithLinkedResource_maps_properly

        public void WithLinkedResource_maps_properly()
        {
            //Arrange
            var builder = new ConfigurationBuilder();
            builder
                .WithConvention(new CamelCaseLinkNameConvention())
                .WithConvention(new PluralizedCamelCaseTypeConvention())
                .WithConvention(new SimpleLinkedIdConvention());

            var post = new Post();
            var author = new Author
            {
                Posts = new List<Post> { post }
            };

            post.Author = author;
            post.AuthorId = 4;

            //Act
            builder
                .Resource<Post>()
                .WithLinkedResource(p => p.Author);

            builder
                .Resource<Author>()
                .WithLinkedResource(a => a.Posts);

            var configuration = builder.Build();
            var postMapping = configuration.GetMapping(typeof(Post));
            var authorMapping = configuration.GetMapping(typeof(Author));

            //Assert
            postMapping.Relationships.Count.ShouldEqual(1);

            var linkToAuthor = postMapping.Relationships.Single();

            linkToAuthor.IsCollection.ShouldBeFalse();
            linkToAuthor.RelationshipName.ShouldEqual("author");
            linkToAuthor.ParentType.ShouldEqual(typeof(Post));
            linkToAuthor.RelatedBaseType.ShouldEqual(typeof(Author));
            linkToAuthor.RelatedResource(post).ShouldBeSameAs(author);
            linkToAuthor.RelatedResourceId(post).ShouldEqual(4);
            linkToAuthor.ResourceMapping.ShouldBeSameAs(authorMapping);

            authorMapping.Relationships.Count.ShouldEqual(1);
            var linkToPosts = authorMapping.Relationships.Single();

            linkToPosts.IsCollection.ShouldBeTrue();
            linkToPosts.RelationshipName.ShouldEqual("posts");
            linkToPosts.RelatedBaseType.ShouldEqual(typeof(Post));
            linkToPosts.ParentType.ShouldEqual(typeof(Author));
            linkToPosts.RelatedResource(author).ShouldBeSameAs(author.Posts);
            linkToPosts.RelatedResourceId.ShouldBeNull();
            linkToPosts.ResourceMapping.ShouldBeSameAs(postMapping);
        }
开发者ID:FrontlineEducation,项目名称:Util-JsonApiSerializer,代码行数:55,代码来源:ConfigurationBuilderTest.cs

示例6: WithAllSimpleProperties_maps_properly

        public void WithAllSimpleProperties_maps_properly()
        {
            //Arrange
            var builder = new ConfigurationBuilder();
            builder
                .WithConvention(new DefaultPropertyScanningConvention());

            const string testTitle = "test";
            var post = new Post
            {
                Id = 4,
                Title = testTitle
            };

            //Act
            builder
                .Resource<Post>()
                .WithAllSimpleProperties();

            var configuration = builder.Build();
            var postMapping = configuration.GetMapping(typeof(Post));

            //Assert
            postMapping.IdGetter.ShouldNotBeNull();
            postMapping.IdGetter(post).ShouldEqual(4);
            postMapping.PropertyGetters.Count.ShouldEqual(2);
            postMapping.PropertySetters.Count.ShouldEqual(2);
            postMapping.PropertyGetters["title"](post).ShouldEqual(testTitle);
            postMapping.PropertyGetters.ContainsKey("authorId").ShouldBeTrue();
        }
开发者ID:FrontlineEducation,项目名称:Util-JsonApiSerializer,代码行数:30,代码来源:ConfigurationBuilderTest.cs

示例7: WithAllLinkedResources_maps_properly

        public void WithAllLinkedResources_maps_properly()
        {
            //Arrange
            var builder = new ConfigurationBuilder();
            builder
                .WithConvention(new DefaultPropertyScanningConvention())
                .WithConvention(new CamelCaseLinkNameConvention())
                .WithConvention(new PluralizedCamelCaseTypeConvention())
                .WithConvention(new SimpleLinkedIdConvention());
            
            //Act
            builder
                .Resource<Post, PostsController>()
                .WithAllLinkedResources();

            builder.Resource<Author, AuthorsController>();
            builder.Resource<Comment, CommentsController>();

            var configuration = builder.Build();
            var postMapping = configuration.GetMapping(typeof(Post));

            //Assert
            Assert.Equal(postMapping.Relationships.Count, 2);
            Assert.NotNull(postMapping.Relationships.SingleOrDefault(l => l.RelatedBaseResourceType == "authors"));
            Assert.NotNull(postMapping.Relationships.SingleOrDefault(l => l.RelatedBaseResourceType == "comments"));
        }
开发者ID:brainwipe,项目名称:NJsonApi,代码行数:26,代码来源:ConfigurationBuilderTest.cs


注:本文中的ConfigurationBuilder.WithConvention方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。