Exemple #1
0
        public GuestbookDto Add(string sName)
        {
            IGuestbook newBook = Guestbook.CreateGuestbook(sName);

            newBook = mGuestbookRepository.Add(newBook); // engine code with storage

            //Mapping domain object to ui object
            GuestbookDto newBookDto = new GuestbookDto
            {
                Name = newBook.Name,
                Id   = newBook.Id
            };

            return(newBookDto);
        }
Exemple #2
0
 private void InitializeData()
 {
     if (!_guestbookRepository.List().Any())
     {
         var newGuestbook = new Guestbook()
         {
             Name = "My Guestbook"
         };
         newGuestbook.AddEntry(new GuestbookEntry()
         {
             EmailAddress = "*****@*****.**",
             Message      = "Hi!"
         });
         _guestbookRepository.Add(newGuestbook);
     }
 }
        public async Task <bool> TestTranInRepositoryAOP()
        {
            var passwords = await _passwordLibRepository.Query();

            Console.WriteLine($"first time : the count of passwords is :{passwords.Count}");


            Console.WriteLine($"insert a data into the table PasswordLib now.");
            var insertPassword = await _passwordLibRepository.Add(new PasswordLib()
            {
                IsDeleted     = false,
                plAccountName = "aaa",
                plCreateTime  = DateTime.Now
            });


            passwords = await _passwordLibRepository.Query(d => d.IsDeleted == false);

            Console.WriteLine($"second time : the count of passwords is :{passwords.Count}");

            //......

            Console.WriteLine($"");
            var guestbooks = await _dal.Query();

            Console.WriteLine($"first time : the count of guestbooks is :{guestbooks.Count}");

            int ex = 0;

            Console.WriteLine($"\nThere's an exception!!");
            int throwEx = 1 / ex;

            Console.WriteLine($"insert a data into the table Guestbook now.");
            var insertGuestbook = await _dal.Add(new Guestbook()
            {
                username   = "******",
                blogId     = 1,
                createdate = DateTime.Now,
                isshow     = true
            });

            guestbooks = await _dal.Query();

            Console.WriteLine($"second time : the count of guestbooks is :{guestbooks.Count}");

            return(true);
        }
        public IActionResult Index()
        {
            if (!_guestbookRepository.List().Any())
            {
                var newGuestbook = new Guestbook()
                {
                    Name = "My Guestbook"
                };
                newGuestbook.Entries.Add(new GuestbookEntry {
                    EmailAddress = "*****@*****.**", Message = "Hi!", DateTimeCreated = DateTime.UtcNow.AddHours(-2)
                });
                _guestbookRepository.Add(newGuestbook);
            }

            var guestbook = _guestbookRepository.GetById(1);
            var viewModel = new HomePageViewModel();

            viewModel.GuestbookName = guestbook.Name;
            viewModel.PreviousEntries.AddRange(guestbook.Entries);

            return(View(viewModel));
        }
        public async Task <MessageModel <string> > TestTranInRepository()
        {
            try
            {
                Console.WriteLine($"");
                Console.WriteLine($"事务操作开始");
                _unitOfWork.BeginTran();
                Console.WriteLine($"");

                Console.WriteLine($"insert a data into the table PasswordLib now.");
                var insertPassword = await _passwordLibRepository.Add(new PasswordLib()
                {
                    IsDeleted     = false,
                    plAccountName = "aaa",
                    plCreateTime  = DateTime.Now
                });


                var passwords = await _passwordLibRepository.Query(d => d.IsDeleted == false);

                Console.WriteLine($"second time : the count of passwords is :{passwords.Count}");

                //......

                Console.WriteLine($"");
                var guestbooks = await _dal.Query();

                Console.WriteLine($"first time : the count of guestbooks is :{guestbooks.Count}");

                int ex = 0;
                Console.WriteLine($"\nThere's an exception!!");
                int throwEx = 1 / ex;

                Console.WriteLine($"insert a data into the table Guestbook now.");
                var insertGuestbook = await _dal.Add(new Guestbook()
                {
                    username   = "******",
                    blogId     = 1,
                    createdate = DateTime.Now,
                    isshow     = true
                });

                guestbooks = await _dal.Query();

                Console.WriteLine($"second time : the count of guestbooks is :{guestbooks.Count}");


                _unitOfWork.CommitTran();

                return(new MessageModel <string>()
                {
                    success = true,
                    msg = "操作完成"
                });
            }
            catch (Exception)
            {
                _unitOfWork.RollbackTran();
                var passwords = await _passwordLibRepository.Query();

                Console.WriteLine($"third time : the count of passwords is :{passwords.Count}");

                var guestbooks = await _dal.Query();

                Console.WriteLine($"third time : the count of guestbooks is :{guestbooks.Count}");

                return(new MessageModel <string>()
                {
                    success = false,
                    msg = "操作异常"
                });
            }
        }
Exemple #6
0
        static void Main(string[] args)
        {
            /*Console.WriteLine("Hello World!");
             * IEntity<int> theEntity = new Guestbook();
             * Console.WriteLine(theEntity.ToString());
             * //Console.WriteLine(theEntity.IsTransient().ToString());
             * Entity theOtherEntity = new Guestbook();
             *
             * Console.WriteLine(theOtherEntity.IsTransient().ToString());
             *
             * // #2 static factory
             * Entity theThirdEntity = Guestbook.CreateGuestbook("novia visitor book");
             * ////////////////////////
             * IGuestbook theBook = Guestbook.CreateGuestbook("visit book");
             *
             * IGuestbookEntry theFirstEntry = new GuestbookEntry();
             * IGuestbookEntry theSecondEntry = new GuestbookEntry();
             *
             * // --------------------------------
             * theBook.AddEntry(theFirstEntry);
             * theBook.AddEntry(theSecondEntry);
             *
             * // --------------------------------
             * ///*/

            ////////////////////////
            var serviceCollection = new ServiceCollection();

            var bootStrapper = new Startup();

            bootStrapper.ConfigureServices(serviceCollection);

            var serviceProvider = serviceCollection.BuildServiceProvider();

            using (EfGuestbookDbContext theContext = serviceProvider.GetService <EfGuestbookDbContext>())
            {
                ////////////////////////
                // hard work
                // The transient objects
                IGuestbook guestbook = serviceProvider.GetService <IGuestbook>();
                guestbook.Name = "Novia";

                IGuestbookEntry guestbookEntry = null;

                //#1
                guestbookEntry = serviceProvider.GetService <IGuestbookEntry>();
                guestbook.AddEntry(guestbookEntry);

                //#2
                guestbookEntry         = serviceProvider.GetService <IGuestbookEntry>();
                guestbookEntry.Message = "Testing entry.";
                guestbook.AddEntry(guestbookEntry);


                // Add the transient object to a repository, which knows how to store
                IGuestbookRepository theBookRepository = serviceProvider.GetService <IGuestbookRepository>();
                theBookRepository.Add(guestbook);

                //-------
                IGuestbook theBook = theBookRepository.GetById(3);
                //IGuestbook theBook = theBookRepository.ListAll()
                //    .ToList() // tuns the sql
                //    .Where(theIteratorBook => theIteratorBook.Name == "Novia").FirstOrDefault();

                theBook.Name = "Novia2";

                theBookRepository.Update(theBook as Guestbook);

                //-------

                ////////////////////////

                // Commit to the database
                theContext.SaveChanges();
            }
        }