Esempio n. 1
0
        public ActionResult Delete(Guid guid)
        {
            //always use Unit of work for save/update
            using (UnitOfWorkScope.Start())
            {
                var repo = ServiceLocator.Current.GetInstance <IRepository <TEntity> >();
                repo.Delete(guid);
            }

            return(RedirectToAction("List").WithFlash(new { alert_success = "Item deleted !" }));
        }
Esempio n. 2
0
        public ActionResult SaveBook(VmBook vmbook)
        {
            if (ModelState.IsValid)
            {
                string flashMsg = vmbook.Book.IsNew ? "New Book added successfully !" : "Book saved successfully !";
                //always use Unit of work for save/update
                using (UnitOfWorkScope.Start())
                {
                    var repoBook = ServiceLocator.Current.GetInstance <IRepository <Book> >();
                    var repoPub  = ServiceLocator.Current.GetInstance <IRepository <Publisher> >();
                    vmbook.Book.Publisher = repoPub.Get(vmbook.SelectPublisherId);

                    repoBook.SaveOrUpdate(vmbook.Book);
                }

                return(RedirectToAction("List").WithFlash(new { alert_success = flashMsg }));
            }
            else
            {
                vmbook.PublisherList = new SelectList(_repositoryPublisher.GetAll(), "Id", "PublisherName");

                return(View(vmbook));
            }
        }
Esempio n. 3
0
        public ActionResult Edit(TEntity entity)
        {
            if (ModelState.IsValid)
            {
                bool   edit     = entity.IsNew;
                string flashMsg = entity.IsNew
                                      ? "New <strong>" + entity.GetType().Name + "</strong> added successfully"
                                      : "<strong>" + entity.GetType().Name + "</strong> saved successfully";
                //always use Unit of work for save/update
                using (UnitOfWorkScope.Start())
                {
                    var repo = ServiceLocator.Current.GetInstance <IRepository <TEntity> >();

                    repo.SaveOrUpdate(entity);

                    return(RedirectToAction("List").WithFlash(new { alert_success = flashMsg }));
                }
            }

            return(View("Edit", new EditViewModel <TEntity>()
            {
                Model = entity
            }));
        }
Esempio n. 4
0
        public void RepositoryInstanceGet()
        {
            //// without Unit Of Work from PerWebRequest lifecycle
            //var r = ServiceLocator.Current.GetInstance<IRepository<Book>>();

            //Assert.NotNull(r);

            // with Unit Of Work
            using (UnitOfWorkScope.Start())
            {
                var repoBook = ServiceLocator.Current.GetInstance <IRepository <Book> >();
                var repoPub  = ServiceLocator.Current.GetInstance <IRepository <Publisher> >();

                Assert.NotNull(repoBook);
                Assert.NotNull(repoPub);

                Assert.True(repoBook is NhRepository <Book>);
                Assert.True(repoPub is NhRepository <Publisher>);

                //see if two repos in same unit of work operates on same ISession, required for a
                // business transaction
                Assert.True((((NhRepository <Book>)repoBook).Session == ((NhRepository <Publisher>)repoPub).Session));
            }
        }