Beispiel #1
0
        public void SaveCreatedItems()
        {
            if (_animals == null || _animals.Count <= 0)
            {
                var reason = _animals == null
                        ? "null" : "equal or less than zero";
                var message = string.Format("Saving business logic was not executed, " +
                                            "this was due to the internal list being {0}", reason);
                throw new PetsEntityException(message);
            }

            try
            {
                using (PetShopDBContext _dataContext = new PetShopDBContext())
                {
                    _dataContext.Animals.AddRange(_animals);
                    _dataContext.SaveChanges();
                    _animals.Clear();
                }
            }
            catch (Exception exception)
            {
                throw;
            }
        }
Beispiel #2
0
        public static void TagEntitiesAsDeleted <T>(this PetShopDBContext dbContext, IList <T> collection)
            where T : class
        {
            if (collection == null || collection.Count <= 0)
            {
                var reason = collection == null
                        ? "null" : "equal or less than zero";
                var message = string.Format("Tagging entities as deleted could " +
                                            "not be executed because to the internal " +
                                            "list being {0}", reason);
                throw new PetsEntityException(message);
            }

            foreach (var item in collection)
            {
                dbContext.Entry <T>(item).State = EntityState.Deleted;

                /* Our design pattern for this application was to
                 * have a sepration were all transactions create
                 * a temporary db-context to communicate with the
                 * database.
                 * Because of the sepertaion, we work in a disconneted
                 * way, which means that we have to manually set
                 * the entity-state of the entity before we save
                 * the object to the database.
                 *
                 * If we do not specify an enity-state, then the
                 * entity framework will throw an error.
                 * */
            }
        }
 private static void ScheduleAssociateWithEntites(PetShopDBContext dataContext, IList <IEntityDaBase> list)
 {
     foreach (IEntityDaBase item in list)
     {
         AddToDbContextSet(dataContext, item);
     }
 }
 private void TagEntitiesAsDeleted(PetShopDBContext dbContext)
 {
     foreach (var item in _coursesToDelete)
     {
         dbContext.Entry(item).State = EntityState.Deleted;
     }
 }
        public void DeleteItems()
        {
            if (_customersToDelete == null || _customersToDelete.Count <= 0)
            {
                var reason = _customersToDelete == null
                        ? "null" : "equal or less than zero";
                var message = string.Format("Deletion could not be executed " +
                                            "because to the internal list being {0}", reason);
                throw new PetsEntityException(message);
            }

            try
            {
                using (PetShopDBContext _dbContext = new PetShopDBContext())
                {
                    //_dbContext.Customers.RemoveRange(_customersToDelete);
                    _dbContext.TagEntitiesAsDeleted <Customer>(_customersToDelete);
                    _dbContext.SaveChanges();
                    _customersToDelete.Clear();
                }
            }
            catch (Exception exception)
            {
                // logging here
                throw;
            }
        }
 private void AddNewChildEntities(PetShopDBContext dbcontext)
 {
     if (_currentCustomer.Messages != null && _currentCustomer.Messages.Count > 0)
     {
         dbcontext.AddNewItems <Message>(_currentCustomer.Messages, m => m.ID == 0);
         //this.AddNewMessages(dbContext);
     }
 }
 public static void AssociateWithEntities <T>(PetShopDBContext dataContext, T items)
 {
     if (items is IEnumerable <IEntityDaBase> )
     {
         List <IEntityDaBase> castedItems = items as List <IEntityDaBase>;
         ScheduleAssociateWithEntites(dataContext, castedItems);
     }
 }
        private void AddNewMessages(PetShopDBContext dbContext)
        {
            var newMessages = _currentCustomer.Messages
                              .Where(m => m.ID == 0).ToList();

            foreach (var item in newMessages)
            {
                dbContext.Entry(item).State = EntityState.Added;
            }
        }
Beispiel #9
0
        public static List <Customer> GetCustomers()
        {
            List <Customer> customer;

            using (PetShopDBContext _context = new PetShopDBContext())
            {
                customer = _context.Customers.ToList();
            }

            return(customer);
        }
        private void UpdateExistingChildEntities(PetShopDBContext dbcontext)
        {
            if (_currentCustomer.Messages != null && _currentCustomer.Messages.Count > 0)
            {
                this.UpdateMessages(dbcontext);
            }

            if (_currentCustomer.Courses != null && _currentCustomer.Courses.Count > 0)
            {
                this.UpdateCourses(dbcontext);
            }
        }
Beispiel #11
0
        public static void ManyToManyCourse()
        {
            using (PetShopDBContext _context = new PetShopDBContext())
            {
                var course = new Course()
                {
                    NAME         = "Customer 111 test 1",
                    SUBJECT_TYPE = "Custimer 111 test",
                    Customers    = GetJoinManyToManyCustomer()
                };
                _context.Courses.Add(course);
                _context.SaveChanges();
            }

            Customer      customer;
            List <Course> courses;

            using (PetShopDBContext _context = new PetShopDBContext())
            {
                customer = _context.Customers.Find(9162);
                courses  = _context.Courses.ToList();
            }

            /*using (PetShopDBContext _context = new PetShopDBContext())
             * {
             *  _context.Entry(customer).State = System.Data.Entity.EntityState.Modified;
             *  _context.Entry(customer).Collection(dc => dc.Courses).Load();
             *  var it = courses.Where(c => c.ID < 2).Select(c => new JoinCustomerCourse
             *  {
             *      CUSTOMER_ID = customer.ID,
             *      COURSE_ID = c.ID
             *  }).ToList();
             *  customer.Courses = it;
             *  _context.SaveChanges();
             * }*/

            var it = courses.Where(c => c.ID < 2).Select(c => new JoinCustomerCourse
            {
                CUSTOMER_ID = customer.ID,
                COURSE_ID   = 2
            }).ToList();

            it.Add(new JoinCustomerCourse
            {
                CUSTOMER_ID = customer.ID,
                COURSE_ID   = 1
            });
            customer.Courses = it;

            UpdateCustomerClass updateCustomer = new UpdateCustomerClass(customer);

            updateCustomer.SaveUpdate();
        }
Beispiel #12
0
 public void SaveUpdate()
 {
     using (PetShopDBContext _dbcontext = new PetShopDBContext())
     {
         if (_currentStock == null)
         {
             return;
         }
         _dbcontext.Entry(_currentStock).State = EntityState.Modified;
         _dbcontext.SaveChanges();
     }
 }
Beispiel #13
0
 public static void CreateCustomer(string firstname, string lastname)
 {
     using (PetShopDBContext _context = new PetShopDBContext())
     {
         Customer customer = new Customer();
         customer.FIRSTNAME = firstname;
         customer.LASTNAME  = lastname;
         customer.AGE       = 25;
         customer.ADDRESS   = "Test address + " + firstname + " " + lastname;
         _context.Customers.Add(customer);
         _context.SaveChanges();
     }
 }
        public static void TestAccountClass()
        {
            Customer customer;

            using (PetShopDBContext _context = new PetShopDBContext())
            {
                customer = _context.Customers.Find(9162);
            }

            CreateAccountClass createAccount = new CreateAccountClass(null);

            createAccount.CreateAccount(customer.ID);
            createAccount.SaveCreatedItems();
        }
        private void UpdateCourses(PetShopDBContext dbContext)
        {
            var detatchedCollection = this.GetDetatchedCollection <JoinCustomerCourse>(dbContext,
                                                                                       _currentCustomer.Courses, c => c.Courses, true);

            foreach (var item in detatchedCollection)
            {
                var existingData = dbContext.JoinCustomerCourses
                                   .FirstOrDefault(x => x.CUSTOMER_ID == item.CUSTOMER_ID &&
                                                   x.COURSE_ID == item.COURSE_ID);
                dbContext.Entry(item).State =
                    existingData != null ? EntityState.Modified : EntityState.Added;
            }

            _currentCustomer.Courses = detatchedCollection;
        }
        public static void TestTransactionClass()
        {
            Account account1;
            Account account2;

            using (PetShopDBContext _context = new PetShopDBContext())
            {
                account1 = _context.Accounts.Find(1);
                account2 = _context.Accounts.Find(2);
            }

            CreateTransactionClass transaction = new CreateTransactionClass(null);

            transaction.CreateTransaction(account1.ID, account2.ID, 500);
            transaction.SaveCreatedItems();
        }
        private List <TEntity> GetDetatchedCollection <TEntity>(PetShopDBContext dbcontext, ICollection <TEntity> collection,
                                                                Expression <Func <Customer, ICollection <TEntity> > > loadFunction, bool loadPrevious) where TEntity : class
        {
            if (!loadPrevious)
            {
                return(collection.ToList());
            }

            var detatchedCollection = collection.ToList();

            dbcontext.Entry(_currentCustomer)
            .Collection(loadFunction)
            .Load();
            collection.Clear();
            return(detatchedCollection);
        }
Beispiel #18
0
 private void SaveToDataBase <T>(IList <T> items)
 {
     try
     {
         using (PetShopDBContext _datacontxt = new PetShopDBContext())
         {
             PersistenceFactory.AssociateWithEntities(_datacontxt, items);
             _datacontxt.SaveChanges();
         }
     }
     catch (Exception e)
     {
         this.ErrorMessage("Error encountered when trying to load cached data to database.",
                           "Location: SaveToCahcedData :: PersistEntityAsyncro");
         this.BackupFailedDatabasePersistence(items);
     }
 }
Beispiel #19
0
 public static void ManyTwoManyTest()
 {
     using (PetShopDBContext _context = new PetShopDBContext())
     {
         _context.Database.Log = Console.WriteLine;
         var cus = new Customer()
         {
             ADDRESS   = "vsdvdfv",
             AGE       = 5,
             FIRSTNAME = "csv",
             LASTNAME  = "efgbn",
             Courses   = GetJoinCustomerCourseManyToMany()
         };
         _context.Customers.Add(cus);
         _context.SaveChanges();
     }
 }
        /*public static void LoadAndMarkEnityCollection<S, T>(this IEntityDaBase entityObject, PetShopDBContext dbcontext,
         *  Func<S, ICollection<T>> loadFunction) where S: class where T : class
         * {
         * dbcontext.Entry(entityObject).Collection(loadFunction.ToString()).Load();
         * }*/

        public static void AddNewItems <TEntity>(this PetShopDBContext dbContext, ICollection <TEntity> collection,
                                                 Expression <Func <TEntity, bool> > lamdaQuery) where TEntity : class
        {
            var newItems = collection.AsQueryable().Where(lamdaQuery)
                           .ToList();

            if (newItems != null)
            {
                foreach (var item in newItems)
                {
                    var castedItem = item as IEntityDaBase;
                    if (castedItem != null)
                    {
                        dbContext.Entry(castedItem).State = EntityState.Added;
                    }
                }
            }
        }
Beispiel #21
0
        public static IList <Customer> GetAllCustomers()
        {
            IList <Customer> customers = null;

            try
            {
                using (PetShopDBContext _dataContext = new PetShopDBContext())
                {
                    customers = _dataContext.Customers
                                .ToList();
                }
            }
            catch (Exception exception)
            {
                // logging here
            }

            return(customers);
        }
Beispiel #22
0
        public void SaveUpdate()
        {
            try
            {
                if (_currentAccount == null)
                {
                    return;
                }

                using (PetShopDBContext _dbcontext = new PetShopDBContext())
                {
                    _dbcontext.Entry(_currentAccount).State = EntityState.Modified;
                    _dbcontext.SaveChanges();
                }
            }
            catch (Exception exception)
            {
                MessageBox.Show("");
            }
        }
 public void SaveCreatedItems()
 {
     try
     {
         using (PetShopDBContext _dbcontext = new PetShopDBContext())
         {
             if (_accounts != null && _accounts.Count > 0)
             {
                 _dbcontext.Accounts.AddRange(_accounts);
                 _dbcontext.SaveChanges();
                 _accounts.Clear();
             }
         }
     }
     catch (Exception exception)
     {
         MessageBox.Show("Problem encountered during persisting account." +
                         "Message" + exception.Message);
     }
 }
 public void SaveCreatedItems()
 {
     try
     {
         using (PetShopDBContext _dataContext = new PetShopDBContext())
         {
             if (_messages != null && _messages.Count > 0)
             {
                 _dataContext.Messages.AddRange(_messages);
                 _dataContext.SaveChanges();
                 _messages.Clear();
             }
         }
     }
     catch (Exception exception)
     {
         System.Windows.Forms.MessageBox.Show("Problem encountered during persisting Message." +
                                              "Message" + exception.Message);
     }
 }
Beispiel #25
0
        public static IList <Animal> GetAllAnimals()
        {
            IList <Animal> animals = null;

            try
            {
                using (PetShopDBContext _datacontext = new PetShopDBContext())
                {
                    //animals = _datacontext.Set<Animal>()
                    animals = _datacontext.Animals
                              .ToList();
                }
            }
            catch (Exception exception)
            {
                //
            }

            return(animals);
        }
Beispiel #26
0
        public static IList <Animal> GetAnimalsTop(int number)
        {
            IList <Animal> animals = null;

            try
            {
                using (PetShopDBContext _datacontext = new PetShopDBContext())
                {
                    animals = _datacontext.Animals
                              .Take(number)
                              .ToList();
                }
            }
            catch (Exception exception)
            {
                //
            }

            return(animals);
        }
Beispiel #27
0
        public static IList <AnimalSold> GetAllAnimalsSold()
        {
            IList <AnimalSold> animalSolds = null;

            try
            {
                using (PetShopDBContext _dataContext = new PetShopDBContext())
                {
                    animalSolds = _dataContext.AnimalSolds
                                  .Include(x => x.Customer.AnimalSolds)
                                  .ToList();
                }
            }
            catch (Exception exception)
            {
                // logging here
            }

            return(animalSolds);
        }
 public void SaveCreatedItems()
 {
     try
     {
         using (PetShopDBContext _dataContext = new PetShopDBContext())
         {
             if (_animalSoldList != null && _animalSoldList.Count > 0)
             {
                 _dataContext.Database.Log = Console.Write;
                 _dataContext.AnimalSolds.AddRange(_animalSoldList);
                 _dataContext.SaveChanges();
                 _animalSoldList.Clear();
             }
         }
     }
     catch (Exception exception)
     {
         MessageBox.Show("Problem encountered during persisting sold animal." +
                         "Message" + exception.Message);
     }
 }
Beispiel #29
0
        public void SaveUpdate()
        {
            if (_currentAnimal == null)
            {
                var message = "Animal could not be updated because it is null";
                throw new PetsEntityException(message);
            }

            try
            {
                using (PetShopDBContext _context = new PetShopDBContext())
                {
                    _context.Entry(_currentAnimal).State = EntityState.Modified;
                    _context.SaveChanges();
                }
            }
            catch (Exception exception)
            {
                throw;
            }
        }
        private void UpdateMessages(PetShopDBContext dbContext)
        {
            var detatchedCollection = this.GetDetatchedCollection <Message>(dbContext,
                                                                            _currentCustomer.Messages, c => c.Messages, false);

            // dbcontext.Entry(_currentCustomer).State = EntityState.Modified;

            /* When tagging child entities in a collection as Entitystate.Modified,
             * make sure you tag the parent entity as Modified, or else it will throw
             * am error.
             * */
            var existingMessages = detatchedCollection.Where(m => m.ID > 0)
                                   .ToList();

            foreach (var item in existingMessages)
            {
                dbContext.Entry(item).State = EntityState.Modified;
            }

            _currentCustomer.Messages = detatchedCollection;
        }