Exemplo n.º 1
0
        public virtual TKey Create(TEntity entity)
        {
            var result = ObjectSet.Add(entity);

            EfUnitOfWork.Commit();
            return(result.Id);
        }
        protected void ButtonFillPostgre_Click(object sender, EventArgs e)
        {
            var championship = new Championship();

            championship.Name = "World Championship";

            var sponsor = new Sponsor();

            sponsor.Name = "Union Cycliste Internationale Test";

            sponsor.Championships = new List <Championship>()
            {
                championship
            };
            championship.Sponsors = new List <Sponsor>()
            {
                sponsor
            };

            using (var unitOfWork = new EfUnitOfWork(new CyclingDbContextPostgre()))
            {
                unitOfWork.ChampionshipsRepository.Add(championship);
                unitOfWork.SponsorsRepository.Add(sponsor);

                unitOfWork.Commit();
            }
        }
Exemplo n.º 3
0
        public void StopCronJob(CronJobType cronJobID, DateTime stopTime)
        {
            using (var _unitOfWork = new EfUnitOfWork())
            {
                Logger.Current.Informational("Request for stopping cronjob");
                var cronJobHistory = _unitOfWork.CronJobHistoryRepository.Find(ch => ch.CronJobID == cronJobID).OrderByDescending(ch => ch.StartTime).FirstOrDefault();
                //var cronJobHistory = _unitOfWork.CronJobHistoryRepository.Find(ch => ch.CronJobID == cronJobID).OrderByDescending(ch => ch.StartTime).Take(1).FirstOrDefault();
                var cronJob = _unitOfWork.CronJobsRepository.Single(cj => cj.CronJobID == cronJobID);
                cronJob.IsRunning          = false;
                cronJob.LastNotifyDateTime = stopTime;

                if (cronJobHistory != null)
                {
                    cronJobHistory.EndTime = DateTime.UtcNow;
                }
                else
                {
                    Logger.Current.Error("Start entry doesn't exists for jobid " + cronJobID);
                }

                _unitOfWork.CronJobsRepository.Edit(cronJob);
                _unitOfWork.CronJobHistoryRepository.Edit(cronJobHistory);
                _unitOfWork.Commit();
                Logger.Current.Informational("Cronjob stopped successfully");
            }
        }
Exemplo n.º 4
0
        // TODO: maybe implement remove by Id not by first and last name
        public void Remove()
        {
            using (var unitOfWork = new EfUnitOfWork(new CyclingDbContext()))
            {
                unitOfWork.CyclingExtendedRepository.RemoveByFirstAndLastName(this.FirstName, this.LastName);

                unitOfWork.Commit();
            }
        }
Exemplo n.º 5
0
 public void UpdateLastNotifyDateTime(CronJobType cronJobID, DateTime lastNotifyDateTime)
 {
     using (var _unitOfWork = new EfUnitOfWork())
     {
         var cronJob = _unitOfWork.CronJobsRepository.Single(cj => cj.CronJobID == cronJobID);
         cronJob.LastNotifyDateTime = lastNotifyDateTime;
         _unitOfWork.CronJobsRepository.Edit(cronJob);
         _unitOfWork.Commit();
     }
 }
Exemplo n.º 6
0
        public void EfUnitOfWork_Commit_Should_Call_SaveChanges()
        {
            var context = new Mock <NotebooksDbContext>();

            context.Setup(c => c.SaveChanges());
            EfUnitOfWork unitOfWork = new EfUnitOfWork();

            unitOfWork.setContext(context.Object);
            unitOfWork.Commit();

            context.Verify(c => c.SaveChanges(), Times.Once);
        }
Exemplo n.º 7
0
        public void TestCommit_ShouldCallDbContextSaveChanges()
        {
            // Arrange
            var mockedDbContext = new Mock <ISqlDbContext>();

            var unitOfWork = new EfUnitOfWork(mockedDbContext.Object);

            // Act
            unitOfWork.Commit();

            // Assert
            mockedDbContext.Verify(c => c.SaveChanges(), Times.Once);
        }
Exemplo n.º 8
0
        [Fact] public void Commit_ShouldMakeEntityFrameworkCommitPendingChanges()
        {
            _inMemoryContext.Posts.Add(new BlogPost {
                BlogPostId = Guid.NewGuid()
            });

            _inMemoryContext.ChangeTracker.Entries <BlogPost>().Select(e => e.State)
            .Should().AllBeEquivalentTo(EntityState.Added,
                                        "we haven't commited anything yet");

            _unitOfWork.Commit().Wait();

            _inMemoryContext.ChangeTracker.Entries <BlogPost>().Select(e => e.State)
            .Should().AllBeEquivalentTo(EntityState.Unchanged,
                                        "we called Commit()");
        }
Exemplo n.º 9
0
        public void TheLightsComeOn()
        {
            using (var db = new EfUnitOfWork(new TestDbContext()))
            {
                var repo = new EfRepository <Thing, int>(db);

                var newThing = new Thing {
                    Name = "Thing 1"
                };
                var result = repo.Create(newThing);

                db.Commit();

                var created = repo.GetById(result.Id);
                Assert.AreEqual(result.Id, created.Id);
            }
        }
Exemplo n.º 10
0
        public void StopAllCronJobs()
        {
            var cronJobs = this.unitOfWork.CronJobsRepository.Find(cj => cj.IsRunning == true).ToList();

            foreach (var item in cronJobs)
            {
                item.IsRunning          = false;
                item.LastNotifyDateTime = DateTime.UtcNow;
                using (var _unitOfWork = new EfUnitOfWork())
                {
                    _unitOfWork.CronJobsRepository.Edit(item);
                    if (cronJobs.Count > 0)
                    {
                        _unitOfWork.Commit();
                    }
                }
            }
        }
Exemplo n.º 11
0
        public void CreateMany(ICollection <Cyclist> cyclists)
        {
            using (var unitOfWork = new EfUnitOfWork(new CyclingDbContext()))
            {
                var cyclistsInDb = unitOfWork.CyclistsRepository.GetAll().ToList();

                foreach (var item in cyclists)
                {
                    if (!cyclistsInDb.Exists(x =>
                                             x.FirstName.ToLower() == item.FirstName.ToLower() &&
                                             x.LastName.ToLower() == item.LastName.ToLower()))
                    {
                        unitOfWork.CyclistsRepository.Add(item);
                    }
                }

                unitOfWork.Commit();
            }
        }
Exemplo n.º 12
0
        public void StartJob(CronJobType cronJobID, DateTime lastRunTime)
        {
            using (var _unitOfWork = new EfUnitOfWork())
            {
                var cronJob = _unitOfWork.CronJobsRepository.Single(cj => cj.CronJobID == cronJobID);
                cronJob.LastRunOn          = lastRunTime;
                cronJob.IsRunning          = true;
                cronJob.LastNotifyDateTime = lastRunTime;
                _unitOfWork.CronJobsRepository.Edit(cronJob);

                var cronJobHistory = new CronJobHistoryDb
                {
                    CronJobID = cronJobID,
                    StartTime = DateTime.UtcNow,
                };
                _unitOfWork.CronJobHistoryRepository.Add(cronJobHistory);
                _unitOfWork.Commit();
            }
        }
Exemplo n.º 13
0
        public void CreateMany(ICollection <Bicycle> bicycles)
        {
            using (var unitOfWork = new EfUnitOfWork(new CyclingDbContext()))
            {
                var bicyclesInDb = unitOfWork.BicyclesRepository.GetAll().ToList();

                foreach (var item in bicycles)
                {
                    if (!bicyclesInDb.Exists(x =>
                                             x.Brand.ToLower() == item.Brand.ToLower() &&
                                             x.Model.ToLower() == item.Model.ToLower()))
                    {
                        unitOfWork.BicyclesRepository.Add(item);
                    }
                }

                unitOfWork.Commit();
            }
        }
        protected void ButtonFillSQLite_Click(object sender, EventArgs e)
        {
            var destination = new CyclingDestination();

            destination.Name    = "Somewhere in Pirin";
            destination.Country = "Bulgaria";

            var instructor = new CyclingInstructor();

            instructor.Name    = "Nikodim Nikodimov";
            instructor.Country = "Bulgaria";

            using (var unitOfWork = new EfUnitOfWork(new CyclingDbContextSQLite()))
            {
                unitOfWork.CyclingDestinationsRepository.Add(destination);
                unitOfWork.CyclingInstructorsRepository.Add(instructor);

                unitOfWork.Commit();
            }
        }
Exemplo n.º 15
0
        public void CreateOne(string firstName, string lastName, int age, int tourWins, int giroWins, int vueltaWins, string team)
        {
            var cyclistNew = new Cyclist()
            {
                FirstName        = firstName,
                LastName         = lastName,
                Age              = age,
                TourDeFranceWins = tourWins,
                GiroDItaliaWins  = giroWins,
                VueltaEspanaWins = vueltaWins,
                CurrentTeam      = team
            };

            using (var unitOfWork = new EfUnitOfWork(new CyclingDbContext()))
            {
                unitOfWork.CyclistsRepository.Add(cyclistNew);

                unitOfWork.Commit();
            }
        }
Exemplo n.º 16
0
        public void MuiltChangeTest()
        {
            var unitOfWorkObj = new EfUnitOfWork();//{ DbContextFactory = _dbContextFactory };

            var unitOfWorkDbContextFactory = new DefualtDbContextFactory()
            {
                UnitOfWork = unitOfWorkObj
            };
            var orderRepo = new FakeOrderRepository()
            {
                DbContextFactory = unitOfWorkDbContextFactory
            };
            var productRepo = new FakeProductRepository()
            {
                DbContextFactory = unitOfWorkDbContextFactory
            };
            var userRepo = new FakeUserRepository()
            {
                DbContextFactory = unitOfWorkDbContextFactory
            };

            unitOfWorkObj.BeginTransaction(new UnitOfWorkOptions());
            try
            {
                //orderRepo.Insert(FakeOrder.Fake());
                productRepo.Insert(FakeProduct.Fake());
                userRepo.Insert(FakeUser.Fake());
                unitOfWorkObj.Commit();
            }
            catch (Exception ex)
            {
                unitOfWorkObj.RollBack();
                Assert.Fail("发生异常:" + ex.Message);
            }
            finally
            {
                unitOfWorkObj.Dispose();
            }
        }
Exemplo n.º 17
0
 public void SaveChanges()
 {
     _unitOfWork.Context.SaveChanges();
     _unitOfWork.Commit();
 }
Exemplo n.º 18
0
 public virtual void Delete(TEntity entity)
 {
     ObjectSet.Remove(entity);
     EfUnitOfWork.Commit();
 }
Exemplo n.º 19
0
 public virtual void Update(TEntity entity)
 {
     ObjectSet.AddOrUpdate(entity);
     EfUnitOfWork.Commit();
 }
Exemplo n.º 20
0
        public void EfUnitOfWork_Should_Throw_When_Not_SetContext_and_call_Commit()
        {
            EfUnitOfWork unitOfWork = new EfUnitOfWork();

            Assert.Throws <NullReferenceException>(() => unitOfWork.Commit());
        }