private static Delegate GenerateSingleMapper() { var config = new CustomConfig(); var selectQuery = new SelectQuery <Post>(new Mock <ISelectQueryExecutor>().Object).Fetch(p => p.Comments) as SelectQuery <Post>; var writer = new SelectWriter(new SqlServer2012Dialect(), config); var result = writer.GenerateSql(selectQuery); var mapper = new SingleCollectionMapperGenerator(config); var func = mapper.GenerateCollectionMapper <Post>(result.FetchTree); return(func.Item1); }
public DelegateQueryCreator(IConfiguration configuration) { this.nonCollectionMapperGenerator = new NonCollectionMapperGenerator(configuration); this.singleCollectionMapperGenerator = new SingleCollectionMapperGenerator(configuration); this.multiCollectionMapperGenerator = new MultiCollectionMapperGenerator(configuration); this.configuration = configuration; this.mapperFactories = new ConcurrentDictionary <Tuple <Type, string>, Tuple <Delegate, Type[]> >(); this.multiCollectionMapperFactories = new ConcurrentDictionary <Tuple <Type, string>, Tuple <Delegate, Type[], Type[]> >(); this.collectionQueries = new ConcurrentDictionary <Tuple <Type, string>, Delegate>(); this.asyncCollectionQueries = new ConcurrentDictionary <Tuple <Type, string>, Delegate>(); this.noCollectionQueries = new ConcurrentDictionary <Tuple <Type, string>, Delegate>(); this.asyncNoCollectionQueries = new ConcurrentDictionary <Tuple <Type, string>, Delegate>(); }
private static Delegate GenerateThenFetchMapper() { var config = new CustomConfig(); var selectQuery = new SelectQuery <Post>(new Mock <IProjectedSelectQueryExecutor>().Object).FetchMany(p => p.Comments).ThenFetch(c => c.User) as SelectQuery <Post>; var writer = new SelectWriter(new SqlServer2012Dialect(), config); var result = writer.GenerateSql(selectQuery, new AutoNamingDynamicParameters()); var mapper = new SingleCollectionMapperGenerator(config); var func = mapper.GenerateCollectionMapper <Post>(result.FetchTree); return(func.Item1); }
private static Delegate GenerateSingleAwkwardMapper() { var config = new CustomConfig(); var selectQuery = new SelectQuery <PostWithoutCollectionInitializerInConstructor>(new Mock <IProjectedSelectQueryExecutor>().Object).Fetch(p => p.Comments) as SelectQuery <PostWithoutCollectionInitializerInConstructor>; var writer = new SelectWriter(new SqlServer2012Dialect(), config); var result = writer.GenerateSql(selectQuery, new AutoNamingDynamicParameters()); var mapper = new SingleCollectionMapperGenerator(config); var func = mapper.GenerateCollectionMapper <PostWithoutCollectionInitializerInConstructor>(result.FetchTree); return(func.Item1); }
public void FetchManyNonRootTrackingEnabledLast() { var config = new CustomConfig(); var selectQuery = new SelectQuery <PostTag>(new Mock <ISelectQueryExecutor>().Object).FetchMany(p => p.Post.Comments).ThenFetch(c => c.User) as SelectQuery <PostTag>; var writer = new SelectWriter(new SqlServer2012Dialect(), config); var result = writer.GenerateSql(selectQuery); var mapper = new SingleCollectionMapperGenerator(config); var funcFac = mapper.GenerateCollectionMapper <PostTag>(result.FetchTree).Item1; // setup the scenario var tag1 = new PostTag { PostTagId = 1 }; var tag2 = new PostTag { PostTagId = 2 }; var tag3 = new PostTag { PostTagId = 3 }; var post1 = new Post { PostId = 1, Title = "Foo" }; var anotherPost1 = new Post { PostId = 1, Title = "Foo" }; var post2 = new Post { PostId = 2, Title = "Foo" }; var post3 = new Post { PostId = 3, Title = "Foo" }; var post4 = new Post { PostId = 4, Title = "Foo" }; var comment1 = new Comment { CommentId = 1 }; var comment2 = new Comment { CommentId = 2 }; var comment3 = new Comment { CommentId = 3 }; var comment4 = new Comment { CommentId = 4 }; var comment5 = new Comment { CommentId = 5 }; var comment6 = new Comment { CommentId = 6 }; var user1 = new User { UserId = 1 }; var user2 = new User { UserId = 2 }; var user3 = new User { UserId = 3 }; var user4 = new User { UserId = 4 }; var user5 = new User { UserId = 5 }; PostTag currentRoot = null; IList <PostTag> results = new List <PostTag>(); var func = (Func <object[], PostTag>)funcFac.DynamicInvoke(currentRoot, results); func(new object[] { tag1, post1, comment1, user1 }); func(new object[] { tag1, post1, comment2, user1 }); func(new object[] { tag1, post1, comment3, user2 }); func(new object[] { tag2, anotherPost1, comment1, user1 }); func(new object[] { tag2, anotherPost1, comment2, user1 }); func(new object[] { tag2, anotherPost1, comment3, user2 }); func(new object[] { tag3, post2, comment4, user3 }); func(new object[] { tag3, post2, comment5, user4 }); func(new object[] { tag3, post2, comment6, user5 }); Assert.False(((ITrackedEntity)results[0]).GetDirtyProperties().Any()); Assert.False(((ITrackedEntity)results[0].Post).GetDirtyProperties().Any()); Assert.False(((ITrackedEntity)results[0].Post.Comments[0]).GetDirtyProperties().Any()); Assert.False(((ITrackedEntity)results[0].Post.Comments[0].User).GetDirtyProperties().Any()); }
public void FetchManyNonRootWorks() { var config = new CustomConfig(); var selectQuery = new SelectQuery <PostTag>(new Mock <ISelectQueryExecutor>().Object).FetchMany(p => p.Post.Comments).ThenFetch(c => c.User) as SelectQuery <PostTag>; var writer = new SelectWriter(new SqlServer2012Dialect(), config); var result = writer.GenerateSql(selectQuery); var mapper = new SingleCollectionMapperGenerator(config); var funcFac = mapper.GenerateCollectionMapper <PostTag>(result.FetchTree).Item1; // setup the scenario var tag1 = new PostTag { PostTagId = 1 }; var tag2 = new PostTag { PostTagId = 2 }; var tag3 = new PostTag { PostTagId = 3 }; var post1 = new Post { PostId = 1, Title = "Foo" }; var anotherPost1 = new Post { PostId = 1, Title = "Foo" }; var post2 = new Post { PostId = 2, Title = "Foo" }; var post3 = new Post { PostId = 3, Title = "Foo" }; var post4 = new Post { PostId = 4, Title = "Foo" }; var comment1 = new Comment { CommentId = 1 }; var comment2 = new Comment { CommentId = 2 }; var comment3 = new Comment { CommentId = 3 }; var comment4 = new Comment { CommentId = 4 }; var comment5 = new Comment { CommentId = 5 }; var comment6 = new Comment { CommentId = 6 }; var user1 = new User { UserId = 1 }; var user2 = new User { UserId = 2 }; var user3 = new User { UserId = 3 }; var user4 = new User { UserId = 4 }; var user5 = new User { UserId = 5 }; PostTag currentRoot = null; IList <PostTag> results = new List <PostTag>(); var func = (Func <object[], PostTag>)funcFac.DynamicInvoke(currentRoot, results); func(new object[] { tag1, post1, comment1, user1 }); func(new object[] { tag1, post1, comment2, user1 }); func(new object[] { tag1, post1, comment3, user2 }); func(new object[] { tag2, anotherPost1, comment1, user1 }); func(new object[] { tag2, anotherPost1, comment2, user1 }); func(new object[] { tag2, anotherPost1, comment3, user2 }); func(new object[] { tag3, post2, comment4, user3 }); func(new object[] { tag3, post2, comment5, user4 }); func(new object[] { tag3, post2, comment6, user5 }); Assert.Equal(3, results.Count); Assert.Equal(3, results.First().Post.Comments.Count); Assert.Equal(1, results.First().Post.PostId); Assert.Equal(1, results.First().Post.Comments.First().User.UserId); Assert.Equal(1, results.First().Post.Comments.ElementAt(1).User.UserId); Assert.Equal(2, results.First().Post.Comments.ElementAt(2).User.UserId); Assert.Equal(3, results.ElementAt(1).Post.Comments.Count); Assert.Equal(1, results.ElementAt(1).Post.PostId); Assert.Equal(1, results.ElementAt(1).Post.Comments.First().User.UserId); Assert.Equal(1, results.ElementAt(1).Post.Comments.ElementAt(1).User.UserId); Assert.Equal(2, results.ElementAt(1).Post.Comments.ElementAt(2).User.UserId); Assert.Equal(3, results.ElementAt(2).Post.Comments.Count); Assert.Equal(2, results.ElementAt(2).Post.PostId); Assert.Equal(4, results.ElementAt(2).Post.Comments.First().CommentId); Assert.Equal(5, results.ElementAt(2).Post.Comments.ElementAt(1).CommentId); Assert.Equal(6, results.ElementAt(2).Post.Comments.ElementAt(2).CommentId); }
public void FetchNonRootCollectionWorks() { var config = new CustomConfig(); var selectQuery = new SelectQuery <PostTag>(new Mock <IProjectedSelectQueryExecutor>().Object).Fetch(p => p.Post.Comments).Take(1) as SelectQuery <PostTag>; var writer = new SelectWriter(new SqlServer2012Dialect(), config); var result = writer.GenerateSql(selectQuery, new AutoNamingDynamicParameters()); var mapper = new SingleCollectionMapperGenerator(config); var funcFac = mapper.GenerateCollectionMapper <PostTag>(result.FetchTree).Item1; var tag1 = new PostTag { PostTagId = 1 }; var tag2 = new PostTag { PostTagId = 2 }; var tag3 = new PostTag { PostTagId = 3 }; var post1 = new Post { PostId = 1, Title = "Foo" }; var anotherPost1 = new Post { PostId = 1, Title = "Foo" }; var post2 = new Post { PostId = 2, Title = "Foo" }; var post3 = new Post { PostId = 3, Title = "Foo" }; var post4 = new Post { PostId = 4, Title = "Foo" }; var comment1 = new Comment { CommentId = 1 }; var comment2 = new Comment { CommentId = 2 }; var comment3 = new Comment { CommentId = 3 }; var comment4 = new Comment { CommentId = 4 }; var comment5 = new Comment { CommentId = 5 }; var comment6 = new Comment { CommentId = 6 }; PostTag currentRoot = null; IList <PostTag> results = new List <PostTag>(); var func = (Func <object[], PostTag>)funcFac.DynamicInvoke(currentRoot, results); func(new object[] { tag1, post1, comment1 }); func(new object[] { tag1, post1, comment2 }); func(new object[] { tag1, post1, comment3 }); func(new object[] { tag2, anotherPost1, comment1 }); func(new object[] { tag2, anotherPost1, comment2 }); func(new object[] { tag2, anotherPost1, comment3 }); func(new object[] { tag3, post2, comment4 }); func(new object[] { tag3, post2, comment5 }); func(new object[] { tag3, post2, comment6 }); Assert.Equal(3, results.Count); Assert.Equal(3, results.First().Post.Comments.Count); Assert.Equal(1, results.First().Post.PostId); Assert.Equal(3, results.ElementAt(1).Post.Comments.Count); Assert.Equal(1, results.ElementAt(1).Post.PostId); Assert.Equal(3, results.ElementAt(2).Post.Comments.Count); Assert.Equal(2, results.ElementAt(2).Post.PostId); Assert.Equal(4, results.ElementAt(2).Post.Comments.First().CommentId); Assert.Equal(5, results.ElementAt(2).Post.Comments.ElementAt(1).CommentId); Assert.Equal(6, results.ElementAt(2).Post.Comments.ElementAt(2).CommentId); }
private static Delegate GenerateSingleAwkwardMapper() { var config = new CustomConfig(); var selectQuery = new SelectQuery<PostWithoutCollectionInitializerInConstructor>(new Mock<ISelectQueryExecutor>().Object).Fetch(p => p.Comments) as SelectQuery<PostWithoutCollectionInitializerInConstructor>; var writer = new SelectWriter(new SqlServer2012Dialect(), config); var result = writer.GenerateSql(selectQuery); var mapper = new SingleCollectionMapperGenerator(config); var func = mapper.GenerateCollectionMapper<PostWithoutCollectionInitializerInConstructor>(result.FetchTree); return func.Item1; }
public void FetchManyNonRootTrackingEnabledLast() { var config = new CustomConfig(); var selectQuery = new SelectQuery<PostTag>(new Mock<ISelectQueryExecutor>().Object).FetchMany(p => p.Post.Comments).ThenFetch(c => c.User) as SelectQuery<PostTag>; var writer = new SelectWriter(new SqlServer2012Dialect(), config); var result = writer.GenerateSql(selectQuery); var mapper = new SingleCollectionMapperGenerator(config); var funcFac = mapper.GenerateCollectionMapper<PostTag>(result.FetchTree).Item1; // setup the scenario var tag1 = new PostTag { PostTagId = 1 }; var tag2 = new PostTag { PostTagId = 2 }; var tag3 = new PostTag { PostTagId = 3 }; var post1 = new Post { PostId = 1, Title = "Foo" }; var anotherPost1 = new Post { PostId = 1, Title = "Foo" }; var post2 = new Post { PostId = 2, Title = "Foo" }; var post3 = new Post { PostId = 3, Title = "Foo" }; var post4 = new Post { PostId = 4, Title = "Foo" }; var comment1 = new Comment { CommentId = 1 }; var comment2 = new Comment { CommentId = 2 }; var comment3 = new Comment { CommentId = 3 }; var comment4 = new Comment { CommentId = 4 }; var comment5 = new Comment { CommentId = 5 }; var comment6 = new Comment { CommentId = 6 }; var user1 = new User { UserId = 1 }; var user2 = new User { UserId = 2 }; var user3 = new User { UserId = 3 }; var user4 = new User { UserId = 4 }; var user5 = new User { UserId = 5 }; PostTag currentRoot = null; IList<PostTag> results = new List<PostTag>(); var func = (Func<object[], PostTag>)funcFac.DynamicInvoke(currentRoot, results); func(new object[] { tag1, post1, comment1, user1 }); func(new object[] { tag1, post1, comment2, user1 }); func(new object[] { tag1, post1, comment3, user2 }); func(new object[] { tag2, anotherPost1, comment1, user1 }); func(new object[] { tag2, anotherPost1, comment2, user1 }); func(new object[] { tag2, anotherPost1, comment3, user2 }); func(new object[] { tag3, post2, comment4, user3 }); func(new object[] { tag3, post2, comment5, user4 }); func(new object[] { tag3, post2, comment6, user5 }); Assert.False(((ITrackedEntity)results[0]).GetDirtyProperties().Any()); Assert.False(((ITrackedEntity)results[0].Post).GetDirtyProperties().Any()); Assert.False(((ITrackedEntity)results[0].Post.Comments[0]).GetDirtyProperties().Any()); Assert.False(((ITrackedEntity)results[0].Post.Comments[0].User).GetDirtyProperties().Any()); }
public void FetchManyNonRootWorks() { var config = new CustomConfig(); var selectQuery = new SelectQuery<PostTag>(new Mock<ISelectQueryExecutor>().Object).FetchMany(p => p.Post.Comments).ThenFetch(c => c.User) as SelectQuery<PostTag>; var writer = new SelectWriter(new SqlServer2012Dialect(), config); var result = writer.GenerateSql(selectQuery); var mapper = new SingleCollectionMapperGenerator(config); var funcFac = mapper.GenerateCollectionMapper<PostTag>(result.FetchTree).Item1; // setup the scenario var tag1 = new PostTag { PostTagId = 1 }; var tag2 = new PostTag { PostTagId = 2 }; var tag3 = new PostTag { PostTagId = 3 }; var post1 = new Post { PostId = 1, Title = "Foo" }; var anotherPost1 = new Post { PostId = 1, Title = "Foo" }; var post2 = new Post { PostId = 2, Title = "Foo" }; var post3 = new Post { PostId = 3, Title = "Foo" }; var post4 = new Post { PostId = 4, Title = "Foo" }; var comment1 = new Comment { CommentId = 1 }; var comment2 = new Comment { CommentId = 2 }; var comment3 = new Comment { CommentId = 3 }; var comment4 = new Comment { CommentId = 4 }; var comment5 = new Comment { CommentId = 5 }; var comment6 = new Comment { CommentId = 6 }; var user1 = new User { UserId = 1 }; var user2 = new User { UserId = 2 }; var user3 = new User { UserId = 3 }; var user4 = new User { UserId = 4 }; var user5 = new User { UserId = 5 }; PostTag currentRoot = null; IList<PostTag> results = new List<PostTag>(); var func = (Func<object[], PostTag>)funcFac.DynamicInvoke(currentRoot, results); func(new object[] { tag1, post1, comment1, user1 }); func(new object[] { tag1, post1, comment2, user1 }); func(new object[] { tag1, post1, comment3, user2 }); func(new object[] { tag2, anotherPost1, comment1, user1 }); func(new object[] { tag2, anotherPost1, comment2, user1 }); func(new object[] { tag2, anotherPost1, comment3, user2 }); func(new object[] { tag3, post2, comment4, user3 }); func(new object[] { tag3, post2, comment5, user4 }); func(new object[] { tag3, post2, comment6, user5 }); Assert.Equal(3, results.Count); Assert.Equal(3, results.First().Post.Comments.Count); Assert.Equal(1, results.First().Post.PostId); Assert.Equal(1, results.First().Post.Comments.First().User.UserId); Assert.Equal(1, results.First().Post.Comments.ElementAt(1).User.UserId); Assert.Equal(2, results.First().Post.Comments.ElementAt(2).User.UserId); Assert.Equal(3, results.ElementAt(1).Post.Comments.Count); Assert.Equal(1, results.ElementAt(1).Post.PostId); Assert.Equal(1, results.ElementAt(1).Post.Comments.First().User.UserId); Assert.Equal(1, results.ElementAt(1).Post.Comments.ElementAt(1).User.UserId); Assert.Equal(2, results.ElementAt(1).Post.Comments.ElementAt(2).User.UserId); Assert.Equal(3, results.ElementAt(2).Post.Comments.Count); Assert.Equal(2, results.ElementAt(2).Post.PostId); Assert.Equal(4, results.ElementAt(2).Post.Comments.First().CommentId); Assert.Equal(5, results.ElementAt(2).Post.Comments.ElementAt(1).CommentId); Assert.Equal(6, results.ElementAt(2).Post.Comments.ElementAt(2).CommentId); }
private static Delegate GenerateThenFetchMapper() { var config = new CustomConfig(); var selectQuery = new SelectQuery<Post>(new Mock<ISelectQueryExecutor>().Object).FetchMany(p => p.Comments).ThenFetch(c => c.User) as SelectQuery<Post>; var writer = new SelectWriter(new SqlServer2012Dialect(), config); var result = writer.GenerateSql(selectQuery); var mapper = new SingleCollectionMapperGenerator(config); var func = mapper.GenerateCollectionMapper<Post>(result.FetchTree); return func.Item1; }