Inheritance: DbContext
Exemple #1
0
        private static void SetupFetchTest(List<Test> tests) {
            const string TestName = "Fetch";

            // add dapper
            tests.Add(
                new Test(
                    Providers.Dapper,
                    TestName,
                    i => {
                        using (var dapperConn = connectionFactory.OpenDbConnection()) {
                            return
                                dapperConn.Query<Post, User, Post>(
                                    "select t.[PostId], t.[Title], t.[Content], t.[Rating], t.[BlogId], t.[DoNotMap], t_1.[UserId], t_1.[Username], t_1.[EmailAddress], t_1.[Password], t_1.[IsEnabled], t_1.[HeightInMeters] from [Posts] as t left join [Users] as t_1 on t.AuthorId = t_1.UserId where ([PostId] = @l_1)",
                                    (p, u) => {
                                        p.Author = u;
                                        return p;
                                    },
                                    new { l_1 = i },
                                    splitOn: "UserId").First();
                        }
                    }));

            // add Dashing
            tests.Add(
                new Test(
                    Providers.Dashing,
                    TestName,
                    i => {
                        using (var dashingSession = dashingConfig.BeginSession()) {
                            return dashingSession.Query<Post>().Fetch(p => p.Author).First(p => p.PostId == i);
                        }
                    }));

            // dashing without transaction
            tests.Add(
                new Test(
                    Providers.Dashing,
                    TestName,
                    i => {
                        using (var session = dashingConfig.BeginTransactionLessSession()) {
                            return session.Query<Post>().Fetch(p => p.Author).First(p => p.PostId == i);
                        }
                    },
                    "Without transaction"));

            // add ef
            tests.Add(
                new Test(
                    Providers.EntityFramework,
                    TestName,
                    i => {
                        using (var EfDb = new EfContext()) {
                            return EfDb.Posts.AsNoTracking().Include(p => p.Author).First(p => p.PostId == i);
                        }
                    }));

            // add nh stateful
            tests.Add(
                new Test(
                    Providers.NHibernate,
                    TestName,
                    i => {
                        using (var nhSession = Nh.SessionFactory.OpenSession()) {
                            return nhSession.Query<Post>().Fetch(p => p.Author).First(p => p.PostId == i);
                        }
                    },
                    "Stateful"));

            // add nh stateless
            tests.Add(
                new Test(
                    Providers.NHibernate,
                    TestName,
                    i => {
                        using (var nhStatelessSession = Nh.SessionFactory.OpenStatelessSession()) {
                            return nhStatelessSession.Query<Post>().Fetch(p => p.Author).First(p => p.PostId == i);
                            ;
                        }
                    },
                    "Stateless"));
        }
Exemple #2
0
        private static void SetupSelectSingleTest(List<Test> tests) {
            const string TestName = "SelectSingle";

            // add dapper
            tests.Add(
                new Test(
                    Providers.Dapper,
                    TestName,
                    i => {
                        using (var dapperConn = connectionFactory.OpenDbConnection()) {
                            return
                                dapperConn.Query<Post>(
                                    "select [PostId], [Title], [Content], [Rating], [AuthorId], [BlogId], [DoNotMap] from [Posts] where ([PostId] = @l_1)",
                                    new { l_1 = i }).First();
                        }
                    }));

            // add Dashing
            tests.Add(
                new Test(
                    Providers.Dashing,
                    TestName,
                    i => {
                        using (var dashingSession = dashingConfig.BeginSession()) {
                            return dashingSession.Query<Post>().First(p => p.PostId == i);
                        }
                    }));

            // dashing - no transaction
            tests.Add(
                new Test(
                    Providers.Dashing,
                    TestName,
                    i => {
                        using (var dashingSession = dashingConfig.BeginTransactionLessSession()) {
                            return dashingSession.Get<Post>(i);
                        }
                    },
                    "By Id without Transaction"));

            // add Dashing by id
            tests.Add(
                new Test(
                    Providers.Dashing,
                    TestName,
                    i => {
                        using (var dashingSession = dashingConfig.BeginSession()) {
                            return dashingSession.Get<Post>(i);
                        }
                    },
                    "By Id"));

            // add ef
            tests.Add(
                new Test(
                    Providers.EntityFramework,
                    TestName,
                    i => {
                        using (var EfDb = new EfContext()) {
                            return EfDb.Posts.AsNoTracking().First(p => p.PostId == i);
                        }
                    }));

            // add ef2
            tests.Add(
                new Test(
                    Providers.EntityFramework,
                    TestName,
                    i => {
                        using (var EfDb = new EfContext()) {
                            EfDb.Configuration.AutoDetectChangesEnabled = false;
                            var post = EfDb.Posts.Find(i);
                            EfDb.Configuration.AutoDetectChangesEnabled = true;
                            return post;
                        }
                    },
                    "Using Find with AutoDetectChangesEnabled = false"));

            // add ormlite
            tests.Add(
                new Test(
                    Providers.ServiceStack,
                    TestName,
                    i => {
                        using (var ormliteConn = connectionFactory.Open()) {
                            using (ormliteConn.OpenTransaction()) {
                                return ormliteConn.SingleById<Post>(i);
                            }
                        }
                    }));

            // add ormlite
            tests.Add(
                new Test(
                    Providers.ServiceStack,
                    TestName,
                    i => {
                        using (var ormliteConn = connectionFactory.Open()) {
                            return ormliteConn.SingleById<Post>(i);
                        }
                    },
                    "Without transaction"));

            // add simple data
            tests.Add(
                new Test(
                    Providers.SimpleData,
                    TestName,
                    i => {
                        var simpleDataDb = Database.OpenConnection(ConnectionString.ConnectionString);
                        return simpleDataDb.Posts.Get(i);
                    }));

            // add nh stateless
            tests.Add(
                new Test(
                    Providers.NHibernate,
                    TestName,
                    i => {
                        using (var nhStatelessSession = Nh.SessionFactory.OpenStatelessSession()) {
                            return nhStatelessSession.Get<Post>(i);
                        }
                    },
                    "Stateless"));

            // add nh stateful
            tests.Add(
                new Test(
                    Providers.NHibernate,
                    TestName,
                    i => {
                        using (var nhSession = Nh.SessionFactory.OpenSession()) {
                            return nhSession.Get<Post>(i);
                        }
                    },
                    "Stateful"));

            // add lightspeed
            tests.Add(
                new Test(
                    Providers.LightSpeed,
                    TestName,
                    i => {
                        using (var uow = lsContext.CreateUnitOfWork()) {
                            return uow.Posts.Single(p => p.Id == i);
                        }
                    },
                    "Linq"));

            // lightspeed find by id
            tests.Add(
                new Test(
                    Providers.LightSpeed,
                    TestName,
                    i => {
                        using (var uow = lsContext.CreateUnitOfWork()) {
                            return uow.FindById<LightSpeed.Domain.Post>(i);
                        }
                    },
                    "FindById without transaction"));
        }
Exemple #3
0
        private static void SetupFetchChangeTests(List<Test> tests) {
            const string TestName = "Get And Change";
            var r = new Random();

            // dapper
            tests.Add(
                new Test(
                    Providers.Dapper,
                    TestName,
                    i => {
                        using (var dapperConn = connectionFactory.OpenDbConnection()) {
                            var post =
                                dapperConn.Query<Post>(
                                    "select [PostId], [Title], [Content], [Rating], [AuthorId], [BlogId], [DoNotMap] from [Posts] where ([PostId] = @l_1)",
                                    new { l_1 = i }).First();
                            post.Title = Providers.Dapper + "_" + i + r.Next(100000);
                            dapperConn.Execute("Update [Posts] set [Title] = @Title where [PostId] = @PostId", new { post.Title, post.PostId });
                            var thatPost =
                                dapperConn.Query<Post>(
                                    "select [PostId], [Title], [Content], [Rating], [AuthorId], [BlogId], [DoNotMap] from [Posts] where ([PostId] = @l_1)",
                                    new { l_1 = i }).First();
                            if (thatPost.Title != post.Title) {
                                Console.WriteLine(TestName + " failed for " + Providers.Dapper + " as the update did not work");
                            }

                            return post;
                        }
                    }));

            // add Dashing
            tests.Add(
                new Test(
                    Providers.Dashing,
                    TestName,
                    i => {
                        using (var dashingSession = dashingConfig.BeginSession()) {
                            var post = dashingSession.Query<Post>().First(p => p.PostId == i);
                            post.Title = Providers.Dashing + "_" + i + r.Next(100000);
                            dashingSession.Save(post);
                            var thatPost = dashingSession.Query<Post>().First(p => p.PostId == i);
                            if (thatPost.Title != post.Title) {
                                Console.WriteLine(TestName + " failed for " + Providers.Dashing + " as the update did not work");
                            }

                            return post;
                        }
                    }));

            // add Dashing by id method
            tests.Add(
                new Test(
                    Providers.Dashing,
                    TestName,
                    i => {
                        using (var dashingSession = dashingConfig.BeginSession()) {
                            var post = dashingSession.Get<Post>(i);
                            post.Title = Providers.Dashing + "_" + i + r.Next(100000);
                            dashingSession.Save(post);
                            var thatPost = dashingSession.Get<Post>(i);
                            if (thatPost.Title != post.Title) {
                                Console.WriteLine(TestName + " failed for " + Providers.Dashing + " as the update did not work");
                            }

                            return post;
                        }
                    },
                    "By Id"));

            // add Dashing by id without transaction 
            tests.Add(
                new Test(
                    Providers.Dashing,
                    TestName,
                    i => {
                        using (var dashingSession = dashingConfig.BeginTransactionLessSession()) {
                            var post = dashingSession.Get<Post>(i);
                            post.Title = Providers.Dashing + "_" + i + r.Next(100000);
                            dashingSession.Save(post);
                            var thatPost = dashingSession.Get<Post>(i);
                            if (thatPost.Title != post.Title) {
                                Console.WriteLine(TestName + " failed for " + Providers.Dashing + " as the update did not work");
                            }

                            return post;
                        }
                    },
                    "By Id without transaction"));

            // add ef
            tests.Add(
                new Test(
                    Providers.EntityFramework,
                    TestName,
                    i => {
                        using (var EfDb = new EfContext()) {
                            var post = EfDb.Posts.Single(p => p.PostId == i);
                            post.Title = Providers.EntityFramework + "_" + i + r.Next(100000);
                            EfDb.SaveChanges();
                            var thatPost = EfDb.Posts.Single(p => p.PostId == i);
                            if (thatPost.Title != post.Title) {
                                Console.WriteLine(TestName + " failed for " + Providers.EntityFramework + " as the update did not work");
                            }

                            return post;
                        }
                    }));

            // add servicestack
            tests.Add(
                new Test(
                    Providers.ServiceStack,
                    TestName,
                    i => {
                        using (var ormliteConn = connectionFactory.OpenDbConnection()) {
                            var post = ormliteConn.SingleById<Post>(i);
                            post.Title = Providers.ServiceStack + "_" + i + r.Next(100000);
                            ormliteConn.Update(post);
                            var thatPost = ormliteConn.SingleById<Post>(i);
                            if (thatPost.Title != post.Title) {
                                Console.WriteLine(TestName + " failed for " + Providers.ServiceStack + " as the update did not work");
                            }

                            return post;
                        }
                    },
                    "without transaction"));

            // add servicestack with transaction
            tests.Add(
                new Test(
                    Providers.ServiceStack,
                    TestName,
                    i => {
                        using (var ormliteConn = connectionFactory.OpenDbConnection()) {
                            using (var tran = ormliteConn.OpenTransaction()) {
                                var post = ormliteConn.SingleById<Post>(i);
                                post.Title = Providers.ServiceStack + "_" + i + r.Next(100000);
                                ormliteConn.Update(post);
                                var thatPost = ormliteConn.SingleById<Post>(i);
                                if (thatPost.Title != post.Title) {
                                    Console.WriteLine(TestName + " failed for " + Providers.ServiceStack + " as the update did not work");
                                }

                                return post;
                            }
                        }
                    }));

            // add nhibernate
            tests.Add(
                new Test(
                    Providers.NHibernate,
                    TestName,
                    i => {
                        using (var nhSession = Nh.SessionFactory.OpenSession()) {
                            var post = nhSession.Get<Post>(i);
                            post.Title = Providers.NHibernate + "_" + i + r.Next(100000);
                            nhSession.Update(post);
                            nhSession.Flush();
                            var thatPost = nhSession.Get<Post>(i);
                            if (thatPost.Title != post.Title) {
                                Console.WriteLine(TestName + " failed for " + Providers.NHibernate + " as the update did not work");
                            }

                            return post;
                        }
                    }));

            // lightspeed
            tests.Add(
                new Test(
                    Providers.LightSpeed,
                    TestName,
                    i => {
                        using (var uow = lsContext.CreateUnitOfWork()) {
                            var post = uow.FindById<LightSpeed.Domain.Post>(i);
                            post.Title = Providers.LightSpeed + "_" + i + r.Next(100000);
                            uow.SaveChanges();
                            var thatPost = uow.FindById<LightSpeed.Domain.Post>(i);
                            if (thatPost.Title != post.Title) {
                                Console.WriteLine(TestName + " failed for " + Providers.LightSpeed + " as the update did not work");
                            }

                            return post;
                        }
                    },
                    "without explicit transaction"));

            // lightspeed
            tests.Add(
                new Test(
                    Providers.LightSpeed,
                    TestName,
                    i => {
                        using (var uow = lsContext.CreateUnitOfWork()) {
                            using (var tran = uow.BeginTransaction()) {
                                var post = uow.FindById<LightSpeed.Domain.Post>(i);
                                post.Title = Providers.LightSpeed + "_" + i + r.Next(100000);
                                uow.SaveChanges();
                                var thatPost = uow.FindById<LightSpeed.Domain.Post>(i);
                                if (thatPost.Title != post.Title) {
                                    Console.WriteLine(TestName + " failed for " + Providers.LightSpeed + " as the update did not work");
                                }

                                return post;
                            }
                        }
                    }));
        }
Exemple #4
0
        private static void SetupFetchCollectionTests(List<Test> tests) {
            const string TestName = "Fetch Collection";

            // add dapper
            tests.Add(
                new Test(
                    Providers.Dapper,
                    TestName,
                    i => {
                        using (var dapperConn = connectionFactory.OpenDbConnection()) {
                            var post =
                                dapperConn.Query<Post>(
                                    "select [PostId], [Title], [Content], [Rating], [AuthorId], [BlogId], [DoNotMap] from [Posts] where ([PostId] = @l_1)",
                                    new { l_1 = i }).First();
                            var comments =
                                dapperConn.Query<Comment>("select * from [Comments] where [PostId] = @postId", new { postId = post.PostId }).ToList();
                            post.Comments = comments;
                            return post;
                        }
                    },
                    "Naive"));

            tests.Add(
                new Test(
                    Providers.Dapper,
                    TestName,
                    i => {
                        using (var dapperConn = connectionFactory.OpenDbConnection()) {
                            var sql = @"
select * from Posts where PostId = @id
select * from Comments where PostId = @id";

                            var multi = dapperConn.QueryMultiple(sql, new { id = i });
                            var post = multi.Read<Post>().Single();
                            post.Comments = multi.Read<Comment>().ToList();
                            multi.Dispose();
                            return post;
                        }
                    },
                    "Multiple Result Method"));

            // add Dashing
            tests.Add(
                new Test(
                    Providers.Dashing,
                    TestName,
                    i => {
                        using (var dashingSession = dashingConfig.BeginSession()) {
                            return dashingSession.Query<Post>().Fetch(p => p.Comments).First(p => p.PostId == i);
                        }
                    }));

            // add Dashing without transaction
            tests.Add(
                new Test(
                    Providers.Dashing,
                    TestName,
                    i => {
                        using (var dashingSession = dashingConfig.BeginTransactionLessSession()) {
                            return dashingSession.Query<Post>().Fetch(p => p.Comments).First(p => p.PostId == i);
                        }
                    },
                    "without transaction"));

            // add EF
            tests.Add(
                new Test(
                    Providers.EntityFramework,
                    TestName,
                    i => {
                        using (var EfDb = new EfContext()) {
                            return EfDb.Posts.Include(p => p.Comments).First(p => p.PostId == i);
                        }
                    }));

            // add nh stateful
            tests.Add(
                new Test(
                    Providers.NHibernate,
                    TestName,
                    i => {
                        using (var nhSession = Nh.SessionFactory.OpenSession()) {
                            return nhSession.Query<Post>().Fetch(p => p.Comments).First(p => p.PostId == i);
                        }
                    },
                    "Stateful"));

            // add nh stateless
            tests.Add(
                new Test(
                    Providers.NHibernate,
                    TestName,
                    i => {
                        using (var nhStatelessSession = Nh.SessionFactory.OpenStatelessSession()) {
                            return nhStatelessSession.Query<Post>().Fetch(p => p.Comments).First(p => p.PostId == i);
                        }
                    },
                    "Stateless"));
        }
Exemple #5
0
        private static void SetupFetchMultiCollectionTests(List<Test> tests) {
            const string TestName = "Fetch Multiple Collections";

            // add dashing
            tests.Add(
                new Test(
                    Providers.Dashing,
                    TestName,
                    i => {
                        using (var dashingSession = dashingConfig.BeginSession()) {
                            return dashingSession.Query<Post>().Fetch(p => p.Comments).Fetch(p => p.Tags).Single(p => p.PostId == i);
                        }
                    }));

            // add EF
            tests.Add(
                new Test(
                    Providers.EntityFramework,
                    TestName,
                    i => {
                        using (var EfDb = new EfContext()) {
                            return EfDb.Posts.Include(p => p.Tags).Include(p => p.Comments).First(p => p.PostId == i);
                        }
                    }));

            // add nh stateful
            tests.Add(
                new Test(
                    Providers.NHibernate,
                    TestName,
                    i => {
                        using (var nhSession = Nh.SessionFactory.OpenSession()) {
                            // First(p => p.PostId == i) doesn't work?
                            // ok, nHIbernate linq broken (now I remember the pain!)
                            var posts = nhSession.QueryOver<Post>().Where(p => p.PostId == i).Future<Post>();
                            var comments = nhSession.QueryOver<Post>().Fetch(p => p.Comments).Eager.Where(p => p.PostId == i).Future<Post>();
                            var tags = nhSession.QueryOver<Post>().Fetch(p => p.Tags).Eager.Where(p => p.PostId == i).Future<Post>();
                            return posts.First();
                        }
                    },
                    "Stateful"));

            // add nh stateless
            // No can do, get NotSupportedException on first line here.
            //tests.Add(
            //    new Test(
            //        Providers.NHibernate,
            //        TestName,
            //        i => {
            //            // First(p => p.PostId == i) doesn't work?
            //            // ok, nHIbernate linq broken (now I remember the pain!)
            //            var posts = nhStatelessSession.QueryOver<Post>().Future<Post>();
            //            var comments =
            //                nhStatelessSession.QueryOver<Post>().Fetch(p => p.Comments).Eager.Future<Post>();
            //            var tags =
            //                nhStatelessSession.QueryOver<Post>().Fetch(p => p.Tags).Eager.Future<Post>();
            //            var post = posts.Where(p => p.PostId == i).First();
            //        },
            //        "Stateless"));
        }
Exemple #6
0
        private static void SetupFetchMultipleMultiCollection(List<Test> tests) {
            const string TestName = "Fetch Multiple Multiple Collections";

            // add dashing
            tests.Add(
                new Test(
                    Providers.Dashing,
                    TestName,
                    i => {
                        using (var dashingSession = dashingConfig.BeginSession()) {
                            return
                                dashingSession.Query<Post>()
                                              .Fetch(p => p.Comments)
                                              .Fetch(p => p.Tags)
                                              .Where(p => p.PostId > i && p.PostId < i + 3)
                                              .ToList();
                        }
                    }));

            // add dashing
            tests.Add(
                new Test(
                    Providers.Dashing,
                    TestName,
                    i => {
                        using (var dashingSession = dashingConfig.BeginTransactionLessSession()) {
                            var j = i + 3;
                            return
                                dashingSession.Query<Post>()
                                              .Fetch(p => p.Comments)
                                              .Fetch(p => p.Tags)
                                              .Where(p => p.PostId > i && p.PostId < j)
                                              .ToList();
                        }
                    },
                    "without Transaction"));

            // add EF
            tests.Add(
                new Test(
                    Providers.EntityFramework,
                    TestName,
                    i => {
                        using (var EfDb = new EfContext()) {
                            var j = i + 3;
                            return EfDb.Posts.Include(p => p.Comments).Include(p => p.Tags).Where(p => p.PostId > i && p.PostId < j).ToList();
                        }
                    }));

            // add nh stateful
            tests.Add(
                new Test(
                    Providers.NHibernate,
                    TestName,
                    i => {
                        using (var nhSession = Nh.SessionFactory.OpenSession()) {
                            // First(p => p.PostId == i) doesn't work?
                            // ok, nHIbernate linq broken (now I remember the pain!)
                            var j = i + 3;
                            var posts = nhSession.QueryOver<Post>().Where(p => p.PostId > i && p.PostId < j).Future<Post>();
                            var comments =
                                nhSession.QueryOver<Post>().Fetch(p => p.Comments).Eager.Where(p => p.PostId > i && p.PostId < j).Future<Post>();
                            var tags = nhSession.QueryOver<Post>().Fetch(p => p.Tags).Eager.Where(p => p.PostId > i && p.PostId < j).Future<Post>();
                            return posts.ToList();
                        }
                    },
                    "Stateful"));
        }
Exemple #7
0
        private static void SetupFetchMultipleChain(List<Test> tests) {
            const string TestName = "Fetch Multiple Chained Collections";

            // add dashing
            tests.Add(
                new Test(
                    Providers.Dashing,
                    TestName,
                    i => {
                        using (var dashingSession = dashingConfig.BeginSession()) {
                            return dashingSession.Query<Blog>().FetchMany(b => b.Posts).ThenFetch(p => p.Tags).SingleOrDefault(b => b.BlogId == i / 5);
                        }
                    }));

            // add dashing without transaction
            tests.Add(
                new Test(
                    Providers.Dashing,
                    TestName,
                    i => {
                        using (var dashingSession = dashingConfig.BeginTransactionLessSession()) {
                            return dashingSession.Query<Blog>().FetchMany(b => b.Posts).ThenFetch(p => p.Tags).SingleOrDefault(b => b.BlogId == i / 5);
                        }
                    },
                    "without Transaction"));

            // add EF
            tests.Add(
                new Test(
                    Providers.EntityFramework,
                    TestName,
                    i => {
                        using (var EfDb = new EfContext()) {
                            return EfDb.Blogs.Include(b => b.Posts.Select(p => p.Tags)).SingleOrDefault(b => b.BlogId == i / 5);
                        }
                    }));
        }