コード例 #1
0
        public void TestEntityRepository()
        {
            var stopwatch = Stopwatch.StartNew();

            using (var uow = new EntityUnitOfWork <TestDbContext>())
            {
                var users = uow.Repository <User>().All();
                stopwatch.Stop();
                Console.WriteLine(stopwatch.ElapsedMilliseconds);

                foreach (var user in users)
                {
                    Console.WriteLine($"{user.Id} {user.Login}");
                }

                var entity = new User
                {
                    Login    = "******",
                    Password = "******"
                };

                entity = uow.Repository <User>().Insert(entity);
                uow.SaveChanges();
                Assert.AreNotEqual(entity.Id, 0);
                Console.WriteLine(entity.Id);
                entity.Password = "******";
                uow.Repository <User>().Update(entity, entity.Id);
                uow.SaveChanges();
                Assert.AreEqual(entity.Password, uow.Repository <User>().Entity(entity.Id).Password);
                uow.Repository <User>().Delete(entity.Id);
                uow.SaveChanges();
                Assert.IsNull(uow.Repository <User>().Entity(entity.Id));
            }
        }
コード例 #2
0
        public void TestCustomRepository()
        {
            var stopwatch = Stopwatch.StartNew();

            using (var uow = new EntityUnitOfWork <TestDbContext>())
            {
                var users = uow.CustomRepository <UserRepository>().NewList();
                stopwatch.Stop();
                Console.WriteLine(stopwatch.ElapsedMilliseconds);
                foreach (var user in users)
                {
                    Console.WriteLine($"{user.Id} {user.Login}");
                }
                var dto = new UserDTO
                {
                    Login    = "******",
                    Password = "******"
                };

                dto = uow.CustomRepository <UserRepository>().Insert(dto);
                uow.SaveChanges();
                var login = dto.Login;
                dto = uow.CustomRepository <UserRepository>().DTO(d => d.Login == login);
                Assert.IsNotNull(dto);
                Assert.AreNotEqual(dto.Id, 0);
                Console.WriteLine(dto.Id);
                dto.Password = "******";
                uow.CustomRepository <UserRepository>().Update(dto, dto.Id);
                uow.SaveChanges();
                Assert.AreEqual(dto.Password, uow.CustomRepository <UserRepository>().DTO(dto.Id).Password);
                uow.CustomRepository <UserRepository>().Delete(dto.Id);
                uow.SaveChanges();
                Assert.IsNull(uow.CustomRepository <UserRepository>().DTO(dto.Id));
            }
        }