public void DeleteFriendUsingGenericRepo()
        {
            using (var context = new PluralSightBookContext())
            {
                var initializer = new TestDbInitializer();
                initializer.Reseed(context);

                int friendCount = context.Friends.Count();
                var testUserId  = context.aspnet_Users.FirstOrDefault().UserId;

                var repo   = new Repositories.Repository <Friend>(context);
                var friend = new Friend()
                {
                    UserId = testUserId, EmailAddress = "*****@*****.**"
                };
                repo.Add(friend);
                repo.Save();

                // Hence always use the same context while saving and deleting the same object/entity
                // Fails because we're using a different object context
                //var repo2 = new Repositories.Repository<Friend>();
                var repo2 = new Repositories.Repository <Friend>(context);
                repo2.Remove(friend);
                repo2.Save();

                Assert.AreEqual(friendCount, context.Friends.Count());
            }
        }
        public void DeleteFriendUsingGenericRepoAndIoC()
        {
            var context = _container.TryGetInstance<DbContext>() as PluralSightBookContext;
            var initializer = new TestDbInitializer();
            initializer.Reseed(context);

            int friendCount = context.Friends.Count();
            var testUserId = context.Users
                .FirstOrDefault(u => u.UserName == TestDbInitializer.TEST_USERNAME)
                .UserId;

            var repo = new Repositories.Repository<Friend>(context);
            var friend = new Friend()
            {
                UserId = testUserId,
                EmailAddress = "*****@*****.**"
            };
            repo.Add(friend);
            repo.Save();

            var anotherContext = _container.TryGetInstance<DbContext>() as PluralSightBookContext;
            var repo2 = new Repositories.Repository<Friend>(anotherContext);
            repo2.Remove(friend);
            repo2.Save();

            Assert.AreEqual(friendCount, context.Friends.Count());
        }
Exemplo n.º 3
0
        public void ListFriendsUsingMapper()
        {
            var friendRepository = new MappingFriendRepository();

            var context =new PluralSightBook.Data.PluralSightBookContext();
            var initializer = new TestDbInitializer();
            initializer.Reseed(context);

            var testUserId = context.Users.FirstOrDefault().UserId;
            friendRepository.Create(testUserId, "*****@*****.**");
            var result = friendRepository.ListFriendsOfUser(testUserId);

            Assert.IsInstanceOfType(result.FirstOrDefault(), typeof(PluralSightBook.Core.Model.Friend));
        }
        public void WhyNotExposeIQueryable()
        {
            using (var context = new PluralSightBookContext())
            {
                var initializer = new TestDbInitializer();
                initializer.Reseed(context);

                var friendRepo = new QueryableFriendRepo(context);
                var testUserId = context.aspnet_Users.FirstOrDefault().UserId;

                // populate some friend sample data
                friendRepo.Add(new Friend()
                {
                    UserId = testUserId, EmailAddress = "*****@*****.**"
                });
                friendRepo.Add(new Friend()
                {
                    UserId = testUserId, EmailAddress = "*****@*****.**"
                });
                friendRepo.Add(new Friend()
                {
                    UserId = testUserId, EmailAddress = "*****@*****.**"
                });
                friendRepo.Save();

                var result = friendRepo.List().Where(f => f.EmailAddress.Contains("foo"));

                Assert.AreEqual(2, result.Count());

                // this tries to run a query on the database but can't
                //var moreResults = friendRepo.List()
                //    .Where(f => f.IsMicrosoftEmployee());
                //foreach (var res in moreResults)
                //{
                //    Console.WriteLine(res);
                //}
                //Assert.AreEqual(1, moreResults.Count());

                // using IEnumerable works as expected, but may fetch more data than desired
                // however, it never will *surprise* the developer with whether they are working
                // with the database or not
                var nonQueryableRepo = new Repository <Friend>(context);
                var msFriends        = nonQueryableRepo.GetAll()
                                       .Where(f => f.IsMicrosoftEmployee());

                Assert.AreEqual(1, msFriends.Count());
            }
        }
        public void AddFriendUsingGenericRepo()
        {
            using (var context = new PluralSightBookContext())
            {
                var initializer = new TestDbInitializer();
                initializer.Reseed(context);

                int friendCount = context.Friends.Count();
                var testUserId  = context.aspnet_Users
                                  .FirstOrDefault(u => u.UserName == TestDbInitializer.TEST_USERNAME)
                                  .UserId;

                var repo = new Repositories.Repository <Friend>();
                repo.Add(new Friend()
                {
                    UserId = testUserId, EmailAddress = "*****@*****.**"
                });
                repo.Save();

                Assert.AreEqual(friendCount + 1, context.Friends.Count());
            }
        }
Exemplo n.º 6
0
        public void CreateAndDeleteFriendWithNHRepo()
        {
            var repo = new NHFriendRepository(_sessionFactory);
            string testFriendEmailAddress = Guid.NewGuid().ToString();
            using (var context = new PluralSightBookContext())
            {
                var initializer = new TestDbInitializer();
                initializer.Reseed(context);
                Guid testUserId = context.Users
                    .FirstOrDefault(u => u.UserName == TestDbInitializer.TEST_USERNAME)
                    .UserId;

                int initialFriendCount = context.Friends
                    .Count(f => f.UserId == testUserId);

                repo.Create(testUserId, testFriendEmailAddress);

                Assert.AreEqual(initialFriendCount + 1,
                    context.Friends.Count(f => f.UserId == testUserId));

                var friends = repo.ListFriendsOfUser(testUserId);
                Assert.AreEqual(friends.Count(), initialFriendCount + 1);

                var friend = _sessionFactory.OpenSession()
                    .QueryOver<Friend>()
                    .Where(f => f.EmailAddress == testFriendEmailAddress)
                    .List()
                    .FirstOrDefault();

                repo.Delete(friend.Id);

                Assert.AreEqual(initialFriendCount, context.Friends
                    .Count(f => f.UserId == testUserId));

            }
        }