Example #1
0
        public ActionResult Index(int?page)
        {
            var data = new NorthwindDataContext();

            const int pageSize = 3;

            // This example passes lambda functions that are called
            // when the view passes into the cache-miss part of the template.
            // They take advantage to closure scope to use local variables and
            // action arguments.

            ViewData["page"]      = page ?? 1;
            ViewData["pageCount"] = ValueHolder.For(() => (data.Employees.Count() + pageSize - 1) / pageSize);
            ViewData["employees"] = ValueHolder.For(() =>
            {
                ++Application.FetchEmployeeListCalls;

                var employeesInOrder = data.Employees
                                       .OrderBy(x => x.LastName);

                return(employeesInOrder
                       .Skip(((page ?? 1) - 1) * pageSize)
                       .Take(pageSize));
            });

            return(View());
        }
Example #2
0
        public ActionResult Show(int id)
        {
            var productHolder = ValueHolder.For(id, () => {
                Response.Write("FETCHING PRODUCT #" + id);
                return(_products.SingleOrDefault(p => p.Id == id));
            });

            return(View(productHolder));
        }
Example #3
0
        public ActionResult Details(int id)
        {
            // This example uses a method reference instead of a lambda
            // with closure variables. The key property is passed along to make this work

            ViewData["employee"] = ValueHolder.For <int, Employee>(id, FetchEmployee);

            return(View());
        }
Example #4
0
        public void KeyPassesThrough()
        {
            var calls  = 0;
            var holder = ValueHolder.For("hello", k =>
            {
                ++calls;
                return(new string(k.Reverse().ToArray()));
            });

            Assert.That(calls, Is.EqualTo(0));
            Assert.That(holder.Value, Is.EqualTo("olleh"));
            Assert.That(holder.Value, Is.EqualTo("olleh"));
            Assert.That(calls, Is.EqualTo(1));
        }
Example #5
0
        public void AcquireMethodShouldBeCalledOnlyOnce()
        {
            var calls  = 0;
            var holder = ValueHolder.For(() =>
            {
                ++calls;
                return("hello");
            });

            Assert.That(calls, Is.EqualTo(0));
            Assert.That(holder.Value, Is.EqualTo("hello"));
            Assert.That(holder.Value, Is.EqualTo("hello"));
            Assert.That(calls, Is.EqualTo(1));
        }