private static void TestCache()
        {
            for (int i = 0; i < 10; i++)
            {
                using (var session = NHMapping.GetSessionFactory().OpenSession())
                {
                    var peopleList =
                        (from p in session.Query <Person>()
                         // .Cacheable()
                         .Fetch(x => x.CountryBorned)
                         orderby p.CountryBorned.CountryId
                         select new { PersonName = p.PersonName, BornedAt = p.CountryBorned.CountryName }).ToList();


                    foreach (var x in peopleList)
                    {
                        Console.WriteLine("{0} {1}", x.PersonName, x.BornedAt);
                    }


                    // With Cache: 28 seconds
                    // Without Cache: 41 seconds
                }
            }
        }
Beispiel #2
0
        public string AddPerson(Person p)
        {
            using (var session = NHMapping.GetSessionFactory().OpenSession())
            {
                using (var tx = session.BeginTransaction())
                {
                    p.CountryBorned = session.Load <Country>(1);
                    session.Save(p);

                    tx.Commit();
                }
            }

            return(p.PersonName);
        }
Beispiel #3
0
        //static IList<PersonInfo> _peopleList = null;

        //public HomeController()
        //{
        //    if (_peopleList != null) return;
        //    using (var session = NHMapping.GetSessionFactory().OpenSession())
        //    {
        //        _peopleList =
        //            (from p in session.Query<Person>().Cacheable().Fetch(x => x.CountryBorned)
        //             orderby p.CountryBorned.CountryId
        //             select new PersonInfo { PersonName = p.PersonName, BornedAt = p.CountryBorned.CountryName }).ToList();
        //    }
        //}


        public JsonResult People()
        {
            using (var session = NHMapping.GetSessionFactory().OpenSession())
            {
                var peopleList =
                    (from p in session.Query <Person>()
                     .Cacheable()
                     .Fetch(x => x.CountryBorned)
                     orderby p.CountryBorned.CountryId, p.PersonId
                     select new { PersonName = p.PersonName, BornedAt = p.CountryBorned.CountryName }).ToList();



                // With caching: 23 seconds
                // Without caching: 38 seconds

                return(Json(peopleList, JsonRequestBehavior.AllowGet));
            }
        }
        static void Main(string[] args)
        {
            for (; ;)
            {
                Console.Write("Enter name of person to add: ");
                string personName = Console.ReadLine();

                if (personName.Trim().Length == 0)
                {
                    Console.WriteLine("Blank not allowed. Retry");
                    continue;
                }

                using (var session = NHMapping.GetSessionFactory().OpenSession())
                {
                    using (var tx = session.BeginTransaction())
                    {
                        // var px = session.Get<Person>(1);
                        //var px = session.Query<Person>().Single(x => x.PersonId == 2);
                        //px.PersonName = personName;
                        //session.Save(px);

                        // session.Save(new Person { PersonName = personName, CountryBorned = session.Load<Country>(1) });


                        // session.Delete(session.Load<Person>(3));


                        session.Delete(new Person {
                            PersonId = 4
                        });

                        tx.Commit();
                    }
                }
            }


            Console.ReadLine();
        }