Example #1
0
        private static void FunctionalTest()
        {
            Console.WriteLine("Functional test");

            {
                // Object can be retreived after adding it to the DbContext
                var customerIds = new IdGenerator();
                using (var db = new MyDatabase())
                {
                    var finder = new FasterFind(db);
                    db.Customers.Add(new Customer()
                    {
                        Id     = customerIds.Next(),
                        Name   = "Some name",
                        Orders = new List <Order>()
                    });
                    Debug.Assert(finder.Find <Customer>(customerIds.All().First()) != null);
                    Debug.Assert(finder.Find <Order>(customerIds.All().First()) == null);
                }
            }

            {
                // Object cannot be retreived after deleting it from the DbContext
                var customerIds = new IdGenerator();
                using (var db = new MyDatabase())
                {
                    var finder   = new FasterFind(db);
                    var customer = db.Customers.Add(new Customer()
                    {
                        Id     = customerIds.Next(),
                        Name   = "Some name",
                        Orders = new List <Order>()
                    });
                    db.SaveChanges();
                    db.Customers.Remove(customer);
                    Debug.Assert(finder.Find <Customer>(customerIds.All().First()) == null);
                    db.SaveChanges();
                    Debug.Assert(finder.Find <Customer>(customerIds.All().First()) == null);
                }
            }

            {
                // Nested Object can be retrieved
                var customerIds = new IdGenerator();
                var orderIds    = new IdGenerator();
                using (var db = new MyDatabase())
                {
                    var finder   = new FasterFind(db);
                    var customer = db.Customers.Add(new Customer()
                    {
                        Id     = customerIds.Next(),
                        Name   = "Some name",
                        Orders = new List <Order>()
                        {
                            new Order()
                            {
                                Id = orderIds.Next(),
                            }
                        }
                    });
                    Debug.Assert(finder.Find <Order>(orderIds.All().First()) != null);
                    db.SaveChanges();
                    Debug.Assert(finder.Find <Order>(orderIds.All().First()) != null);
                }
            }

            {
                // Nested Object can be retrieved - late bound
                // WARNING THIS IS THE ONLY USE-CASE THAT THIS
                // IMPLEMENTATION DOES NOT SUPPORT - UNLESS SAVED WITH SAVECHANGES
                var customerIds = new IdGenerator();
                var orderIds    = new IdGenerator();
                using (var db = new MyDatabase())
                {
                    var finder   = new FasterFind(db);
                    var customer = db.Customers.Add(new Customer()
                    {
                        Id     = customerIds.Next(),
                        Name   = "Some name",
                        Orders = new List <Order>()
                    });
                    customer.Orders.Add(new Order()
                    {
                        Customer = customer,
                        Id       = orderIds.Next(),
                    });
                    // This is the case where EF can find the object
                    // and our implementation cannot find it
                    // but EF is slow because of this 'only' case.
                    Debug.Assert(finder.Find <Order>(orderIds.All().First()) == null);
                    Debug.Assert(db.Orders.Find(orderIds.All().First()) != null);

                    db.SaveChanges();
                    Debug.Assert(finder.Find <Order>(orderIds.All().First()) != null);
                }
            }
        }