Exemplo n.º 1
0
 public T Get(int id)
 {
     using (var session = SessionFactorySingleton.OpenSession())
     {
         return(session.Get <T>(id));
     }
 }
Exemplo n.º 2
0
 public IList <T> GetAll()
 {
     using (var session = SessionFactorySingleton.OpenSession())
     {
         return(session.CreateCriteria <T>().List <T>());
     }
 }
Exemplo n.º 3
0
 public long GetTotalReviewCount()
 {
     using (var session = SessionFactorySingleton.OpenSession())
     {
         return(session.CreateQuery("select count(r.Id) from Review r")
                .UniqueResult <long>());
     }
 }
Exemplo n.º 4
0
 public static void ClearDatabase()
 {
     using (var session = SessionFactorySingleton.OpenSession())
     {
         session.CreateQuery("delete Review").ExecuteUpdate();
         session.CreateQuery("delete User").ExecuteUpdate();
     }
 }
Exemplo n.º 5
0
        public void Save(T entity)
        {
            using (var session = SessionFactorySingleton.OpenSession())
                using (var transaction = session.BeginTransaction())
                {
                    session.SaveOrUpdate(entity);

                    transaction.Commit();
                }
        }
Exemplo n.º 6
0
        public void Delete(int id)
        {
            using (var session = SessionFactorySingleton.OpenSession())
                using (var transaction = session.BeginTransaction())
                {
                    var proxy = session.Load <User>(id);
                    session.Delete(proxy);

                    transaction.Commit();
                }
        }
Exemplo n.º 7
0
 public long GetReviewCountForUser(int userId)
 {
     using (var session = SessionFactorySingleton.OpenSession())
     {
         return(session.CreateQuery(
                    "select count(review.Id) from User user left join user.Reviews review " +
                    "where user.Id = :userId")
                .SetInt32("userId", userId)
                .UniqueResult <long>());
     }
 }
Exemplo n.º 8
0
 public IList <User> GetUsersWithReviewsWithRating_QueryOver(int rating)
 {
     using (var session = SessionFactorySingleton.OpenSession())
     {
         return(session.QueryOver <User>()
                .JoinQueryOver(u => u.Reviews)
                .Where(r => r.Rating == rating)
                .TransformUsing(Transformers.DistinctRootEntity)
                .List <User>());
     }
 }
Exemplo n.º 9
0
 public IList <User> GetUsersWithReviewsWithRating(int rating)
 {
     using (var session = SessionFactorySingleton.OpenSession())
     {
         return(session.CreateQuery(
                    "select u from User u join u.Reviews r where r.Rating = :rating")
                .SetInt32("rating", rating)
                .SetResultTransformer(Transformers.DistinctRootEntity)
                .List <User>());
     }
 }
Exemplo n.º 10
0
 public void Can_write_and_read_user()
 {
     using (var session = SessionFactorySingleton.OpenSession())
     {
         new PersistenceSpecification <User>(session)
         .CheckProperty(x => x.Username, "username")
         .CheckProperty(x => x.Password, "password")
         .CheckProperty(x => x.LastLoginDate, new DateTime(2011, 1, 2, 3, 4, 5))
         .CheckList(x => x.Reviews, _reviews, new EntityIdComparer(),
                    (u, r) => u.AddReview(r));
     }
 }
Exemplo n.º 11
0
        public void Save(IEnumerable <T> entities)
        {
            using (var session = SessionFactorySingleton.OpenSession())
                using (var transaction = session.BeginTransaction())
                {
                    foreach (var entity in entities)
                    {
                        session.SaveOrUpdate(entity);
                    }

                    transaction.Commit();
                }
        }
Exemplo n.º 12
0
        private void SaveAllUsers()
        {
            using (var session = SessionFactorySingleton.OpenSession())
                using (var transaction = session.BeginTransaction())
                {
                    session.Save(_user1);
                    session.Save(_user2);
                    session.Save(_user3);
                    session.Save(_user4);

                    transaction.Commit();
                }
        }
Exemplo n.º 13
0
        private static void PrintReviewCountsWithLazyLoading()
        {
            WriteConsoleSeparator();

            using (var session = SessionFactorySingleton.OpenSession())
            {
                var users = session.CreateQuery("from User").List <User>();

                foreach (var user in users)
                {
                    System.Console.WriteLine(user.Username + ": " + user.Reviews.Count());
                }
            }
        }
Exemplo n.º 14
0
        private static void PrintReviewCountsWithEagerLoading()
        {
            WriteConsoleSeparator();

            using (var session = SessionFactorySingleton.OpenSession())
            {
                var users = session.CreateQuery("from User user join fetch user.Reviews")
                            .SetResultTransformer(Transformers.DistinctRootEntity)
                            .List <User>();

                foreach (var user in users)
                {
                    System.Console.WriteLine(user.Username + ": " + user.Reviews.Count());
                }
            }
        }
Exemplo n.º 15
0
        private static void CreateTestData()
        {
            using (var session = SessionFactorySingleton.OpenSession())
                using (var transaction = session.BeginTransaction())
                {
                    var user1 = new User
                    {
                        Username      = "******",
                        Password      = "******",
                        LastLoginDate = new DateTime(2011, 1, 2, 3, 4, 5),
                    };

                    var user2 = new User
                    {
                        Username      = "******",
                        Password      = "******",
                        LastLoginDate = new DateTime(2022, 2, 2, 3, 4, 5),
                    };

                    var user3 = new User
                    {
                        Username      = "******",
                        Password      = "******",
                        LastLoginDate = new DateTime(3033, 3, 3, 3, 4, 5),
                    };

                    user1.AddReview(new Review
                    {
                        Title    = "User 1's first review",
                        Body     = "This is the first review written by User 1",
                        DateTime = new DateTime(2011, 2, 3, 4, 5, 6),
                        Rating   = 5
                    });

                    user1.AddReview(new Review
                    {
                        Title    = "User 1's second review",
                        Body     = "This is the second review written by User 1",
                        DateTime = new DateTime(2011, 2, 3, 6, 7, 8),
                        Rating   = 4
                    });

                    user2.AddReview(new Review
                    {
                        Title    = "User 2's first review",
                        Body     = "This is the first review written by User 2",
                        DateTime = new DateTime(2011, 2, 4, 5, 6, 7),
                        Rating   = 5
                    });

                    user2.AddReview(new Review
                    {
                        Title    = "User 2's second review",
                        Body     = "This is the second review written by User 2",
                        DateTime = new DateTime(2022, 2, 4, 6, 7, 8),
                        Rating   = 4
                    });

                    user2.AddReview(new Review
                    {
                        Title    = "User 2's third review",
                        Body     = "This is the third review written by User 2",
                        DateTime = new DateTime(2022, 2, 4, 7, 8, 9),
                        Rating   = 3
                    });

                    user3.AddReview(new Review
                    {
                        Title    = "User 3's first review",
                        Body     = "This is the first review written by User 3",
                        DateTime = new DateTime(3011, 3, 4, 5, 6, 7),
                        Rating   = 1
                    });

                    session.Save(user1);
                    session.Save(user2);
                    session.Save(user3);

                    transaction.Commit();
                }
        }