Ejemplo n.º 1
0
        static void TestLinqToSql2(int loopCounter)
        {
            LinqToSql.Query<AdventureWorksDataContext>.Clear();
            var options = new { Colors = new List<string> { "Red" }, City = "Bothell", CompanyName = "Bike" };

            var query = new LinqToSql.Query<AdventureWorksDataContext>();
            var exp1a = To.Expression((AdventureWorksDataContext db, int option) => from m in db.Product where options.Colors.Contains(m.Color) select m);
            var exp1b = To.Expression((AdventureWorksDataContext db, List<string> option) => from m in db.Product where option.Contains(m.Color) select m);
            var exp1c = To.Expression((AdventureWorksDataContext db, string[] option) => from m in db.Product where option.Contains(m.Color) select m);
            var exp2 = To.Expression((AdventureWorksDataContext db, string option) => from m in db.Address where m.City == option select m);
            var exp3 = To.Expression((AdventureWorksDataContext db, string option) => from m in db.Customer where m.CompanyName.StartsWith(option) select m);

            var reader = new TestExecuteReader();
            using(var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["AdventureWorks"].ConnectionString, reader))
            using(var context = new AdventureWorksDataContext(connection))
            {
                context.ObjectTrackingEnabled = false;
                context.DeferredLoadingEnabled = false;
                Test("Ad hoc", () =>
                {
                    (from m in context.Product where options.Colors.Contains(m.Color) select m).ToArray();
                    (from m in context.Product where new List<string> { "Red" }.Contains(m.Color) select m).ToArray();
                    (from m in context.Address where m.City == options.City select m).ToArray();
                    (from m in context.Customer where m.CompanyName.StartsWith(options.CompanyName) select m).ToArray();
                });

                Test("Expression", () =>
                {
                    exp1a.Compile()(context, 0).ToArray();
                    exp1b.Compile()(context, new List<string> { "Red" }).ToArray();
                    exp2.Compile()(context, options.City).ToArray();
                    exp3.Compile()(context, options.CompanyName).ToArray();
                });

                Test("Fast 1", () =>
                {
                    query.Fast(exp1a)(context, 1).ToArray();
                    query.Fast(exp1b)(context, new List<string> { "Red" }).ToArray();
                    query.Fast(exp2)(context, options.City).ToArray();
                    query.Fast(exp3)(context, options.CompanyName).ToArray();
                });

                Test("Fast 2", () =>
                {
                    query.Fast(
                        (AdventureWorksDataContext db, int option) => from m in db.Product where options.Colors.Contains(m.Color) select m
                    )(context, 2).ToArray();
                    query.Fast(
                        (AdventureWorksDataContext db, List<string> option) => from m in db.Product where option.Contains(m.Color) select m
                    )(context, new List<string> { "Red" }).ToArray();
                    query.Fast(
                        (AdventureWorksDataContext db, string option) => from m in db.Address where m.City == option select m
                    )(context, "Bothell").ToArray();
                    query.Fast(
                        (AdventureWorksDataContext db, string option) => from m in db.Customer where m.CompanyName.StartsWith(option) select m
                    )(context, "Bike").ToArray();
                });

                Test("Cache A", () =>
                {
                    options.Colors.Clear(); options.Colors.Add("Black");
                    var black = query.Fast(exp1a)(context, 11).FirstOrDefault();

                    options.Colors.Clear(); options.Colors.Add("Blue");
                    var blue = query.Fast(exp1a)(context, 12).FirstOrDefault();

                    options.Colors.Clear(); options.Colors.Add("White");
                    var white = query.Fast(exp1a)(context, 13).FirstOrDefault();

                    if (black.Color != "Black" || blue.Color != "Blue" || white.Color != "White")
                        throw new Exception("Bad cache!");
                });

                Test("Cache B", () =>
                {
                    var black = query.Fast(exp1b)(context, new List<string> { "Black" }).ToArray();
                    var blue = query.Fast(exp1b)(context, new List<string> { "Blue" }).ToArray();
                    var white = query.Fast(exp1b)(context, new List<string> { "White" }).ToArray();

                    if (black.First().Color != "Black" || blue.First().Color != "Blue" || white.First().Color != "White")
                        throw new Exception("Bad cache!");
                });

                Test("Cache C", () =>
                {
                    var black = query.Fast(exp1c)(context, new[] { "Black" }).ToArray();
                    var blue = query.Fast(exp1c)(context, new[] { "Blue" }).ToArray();
                    var white = query.Fast(exp1c)(context, new[] { "White" }).ToArray();

                    if (black.First().Color != "Black" || blue.First().Color != "Blue" || white.First().Color != "White")
                        throw new Exception("Bad cache!");
                });
            }
        }
Ejemplo n.º 2
0
        static void TestLinqToSqlCache(int loopCounter)
        {
            LinqToSql.Query<AdventureWorksDataContext>.Clear();
            var options = new { Colors = new List<string> { "Red" }, City = "Bothell", CompanyName = "Bike" };

            var query = new LinqToSql.Query<AdventureWorksDataContext>();
            var exp1a = To.Expression((AdventureWorksDataContext db, int option) =>
                from m in db.Product where options.Colors.Contains(m.Color) select m);
            var exp1b = To.Expression((AdventureWorksDataContext db, List<string> option) =>
                from m in db.Product where option.Contains(m.Color) select m);
            var exp1c = To.Expression((AdventureWorksDataContext db, string[] option) =>
                from m in db.Product where option.Contains(m.Color) select m);

            using (var connection = new System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings["AdventureWorks"].ConnectionString))
            using (var context = new AdventureWorksDataContext(connection))
            {
                context.ObjectTrackingEnabled = false;
                context.DeferredLoadingEnabled = false;
                Test("Cache A", () =>
                {
                    options.Colors.Clear(); options.Colors.Add("Black");
                    var black = query.Fast(exp1a)(context, 11).FirstOrDefault();

                    options.Colors.Clear(); options.Colors.Add("Blue");
                    var blue = query.Fast(exp1a)(context, 12).FirstOrDefault();

                    options.Colors.Clear(); options.Colors.Add("White");
                    var white = query.Fast(exp1a)(context, 13).FirstOrDefault();

                    if (black.Color != "Black" || blue.Color != "Blue" || white.Color != "White")
                        throw new Exception("Bad cache!");
                });

                Test("Cache B", () =>
                {
                    var black = query.Fast(exp1b)(context, new List<string> { "Black" }).FirstOrDefault();
                    var blue = query.Fast(exp1b)(context, new List<string> { "Blue" }).FirstOrDefault();
                    var white = query.Fast(exp1b)(context, new List<string> { "White" }).FirstOrDefault();

                    if (black.Color != "Black" || blue.Color != "Blue" || white.Color != "White")
                        throw new Exception("Bad cache!");
                });

                Test("Cache C", () =>
                {
                    var black = query.Fast(exp1c)(context, new[] { "Black" }).FirstOrDefault();
                    var blue = query.Fast(exp1c)(context, new[] { "Blue" }).FirstOrDefault();
                    var white = query.Fast(exp1c)(context, new[] { "White" }).FirstOrDefault();

                    if (black.Color != "Black" || blue.Color != "Blue" || white.Color != "White")
                        throw new Exception("Bad cache!");
                });
            }
        }
Ejemplo n.º 3
0
        static void TestLinqToSql(int loopCounter)
        {
            LinqToSql.Query<AdventureWorksDataContext>.Clear();
            var options = new { Colors = new List<string> { "Red" }, City = "Bothell", CompanyName = "Bike" };

            var query = new LinqToSql.Query<AdventureWorksDataContext>();
            var exp1a = To.Expression((AdventureWorksDataContext db, int option) =>
                from m in db.Product where options.Colors.Contains(m.Color) select m);
            var exp1b = To.Expression((AdventureWorksDataContext db, List<string> option) =>
                from m in db.Product where option.Contains(m.Color) select m);
            var exp2 = To.Expression((AdventureWorksDataContext db, string option) =>
                from m in db.Address where m.City == option select m);
            var exp3 = To.Expression((AdventureWorksDataContext db, string option) =>
                from m in db.Customer where m.CompanyName.StartsWith(option) select m);

            using (var connection = new System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings["AdventureWorks"].ConnectionString))
            using (var context = new AdventureWorksDataContext(connection))
            {
                context.ObjectTrackingEnabled = false;
                context.DeferredLoadingEnabled = false;
                Test("Ad hoc", () =>
                {
                    (from m in context.Product
                     where options.Colors.Contains(m.Color)
                     select m).FirstOrDefault();
                    (from m in context.Product
                     where new List<string> { "Red" }.Contains(m.Color)
                     select m).FirstOrDefault();
                    (from m in context.Address
                     where m.City == options.City
                     select m).FirstOrDefault();
                    (from m in context.Customer
                     where m.CompanyName.StartsWith(options.CompanyName)
                     select m).FirstOrDefault();
                });

                Test("Expression", () =>
                {
                    exp1a.Compile()(context, 0).FirstOrDefault();
                    exp1b.Compile()(context, new List<string> { "Red" }).FirstOrDefault();
                    exp2.Compile()(context, options.City).FirstOrDefault();
                    exp3.Compile()(context, options.CompanyName).FirstOrDefault();
                });

                Test("Fast 1", () =>
                {
                    query.Fast(exp1a)(context, 1).FirstOrDefault();
                    query.Fast(exp1b)(context, new List<string> { "Red" }).FirstOrDefault();
                    query.Fast(exp2)(context, options.City).FirstOrDefault();
                    query.Fast(exp3)(context, options.CompanyName).FirstOrDefault();
                });

                Test("Fast 2", () =>
                {
                    query.Fast(
                        (AdventureWorksDataContext db, int option) =>
                            from m in db.Product where options.Colors.Contains(m.Color) select m
                    )(context, 2).FirstOrDefault();
                    query.Fast(
                        (AdventureWorksDataContext db, List<string> option) =>
                            from m in db.Product where option.Contains(m.Color) select m
                    )(context, new List<string> { "Red" }).FirstOrDefault();
                    query.Fast(
                        (AdventureWorksDataContext db, string option) =>
                            from m in db.Address where m.City == option select m
                    )(context, "Bothell").FirstOrDefault();
                    query.Fast(
                        (AdventureWorksDataContext db, string option) =>
                            from m in db.Customer where m.CompanyName.StartsWith(option) select m
                    )(context, "Bike").FirstOrDefault();
                });
            }
        }