public virtual void UpdateGraphWithChildDeletes(ProductLineEntity productLine)
        {
            if (productLine == null)
            {
                throw new ArgumentNullException(nameof(productLine), $"{nameof(productLine)} is null.");
            }

            // this update method will update the related products. Any removed product has to be removed as it's orphaned.
            // We have to remove all products which key isn't in the set of products currently related to the passed in productline.
            // To do that we'll do a delete directly using a where clause where all entities with a key not in the set of
            // keys of the current related product entities are removed. We'll wrap it all in a unit of work for easy transaction handling.
            // In the unit of work, we have to schedule the direct deletes before the insert of the new row, otherwise it's removed,
            // as it doesn't have a PK yet, so the IN clause we're using won't match it.
            var currentKeys = productLine.Products.Select(p => p.ProductKey).ToList();
            var uow         = new UnitOfWork2(new List <UnitOfWorkBlockType>()
            {
                UnitOfWorkBlockType.DeletesPerformedDirectly,
                UnitOfWorkBlockType.Inserts,
                UnitOfWorkBlockType.Updates
            });

            uow.AddDeleteEntitiesDirectlyCall(typeof(ProductEntity), new RelationPredicateBucket(ProductFields.ProductKey.NotIn(currentKeys)));
            uow.AddForSave(productLine);
            using (var adapter = new DataAccessAdapter())
            {
                uow.Commit(adapter);
            }
        }
示例#2
0
        public IList <EmployeeEntity> InsertBatchReturnRows(IList <EmployeeEntity> employees)
        {
            if (employees == null || employees.Count == 0)
            {
                throw new ArgumentException($"{nameof(employees)} is null or empty.", nameof(employees));
            }

            // Use a unit of work here. We could have created a new EntityCollection here but as well
            var uow = new UnitOfWork2();

            foreach (var e in employees)
            {
                uow.AddForSave(e);
            }

            using (var adapter = new DataAccessAdapter())
            {
                // use batching for inserts. We'll use 100 for a batch size here.
                // This will send at most 100 inserts at one time to the database
                // in a single DbCommand
                adapter.BatchSize = 100;
                uow.Commit(adapter);
            }

            // LLBLGen Pro will update entities in-place after an insert, so we can return what we received.
            return(employees);
        }
示例#3
0
        /// <summary>
        /// Resets the password for the user related to the password token specified to the newPassword specified
        /// It'll then remove the password reset token entity specified
        /// </summary>
        /// <param name="newPassword">the new password specified</param>
        /// <param name="passwordResetToken">the reset token. Will be removed in this method if password reset is successful</param>
        /// <returns>true if successful, false otherwise</returns>
        public static async Task <bool> ResetPasswordAsync(string newPassword, PasswordResetTokenEntity passwordResetToken)
        {
            if (string.IsNullOrWhiteSpace(newPassword) || passwordResetToken == null)
            {
                return(false);
            }

            using (var adapter = new DataAccessAdapter())
            {
                var q    = new QueryFactory().User.Where(UserFields.UserID.Equal(passwordResetToken.UserID));
                var user = await adapter.FetchFirstAsync(q).ConfigureAwait(false);

                if (user == null)
                {
                    return(false);
                }

                user.Password = HnDGeneralUtils.HashPassword(newPassword, performPreMD5Hashing: true);
                var uow = new UnitOfWork2();
                uow.AddForSave(user);
                uow.AddForDelete(passwordResetToken);
                var toReturn = await uow.CommitAsync(adapter);

                return(toReturn == 2);
            }
        }
        public void UpdateGraph(ProductLineEntity productLine)
        {
            if (productLine == null)
            {
                throw new ArgumentNullException(nameof(productLine), $"{nameof(productLine)} is null.");
            }

            var uow = new UnitOfWork2(new List <UnitOfWorkBlockType>()
            {
                UnitOfWorkBlockType.DeletesPerformedDirectly,
                UnitOfWorkBlockType.Inserts,
                UnitOfWorkBlockType.Updates
            });

            uow.AddForSave(productLine);
            using (var adapter = new DataAccessAdapter())
            {
                uow.Commit(adapter);
            }
        }
        public override void UpdateGraphWithChildDeletes(ProductLineEntity productLine)
        {
            if (productLine == null)
            {
                throw new ArgumentNullException(nameof(productLine), $"{nameof(productLine)} is null.");
            }

            // this update method will update the related products. Any removed product has to be removed as it's orphaned.
            // we inserted a removal tracker in the productline entity to track these, so we can just delete them from
            // this collection. We also have to update the entity and related entities. We'll use a unit of work object
            // for this to have easy transaction management.
            var uow = new UnitOfWork2();

            uow.AddForSave(productLine);
            uow.AddCollectionForDelete(productLine.Products.RemovedEntitiesTracker);
            using (var adapter = new DataAccessAdapter())
            {
                uow.Commit(adapter);
            }
        }
        public void Update(ProductLineEntity productLine)
        {
            if (productLine == null)
            {
                throw new ArgumentNullException(nameof(productLine), $"{nameof(productLine)} is null.");
            }

            // Specify the order of operations for the unit of work, so it will first perform delete operations
            // directly on the database and then do inserts followed by updates. We have to specify the order
            // here as it's different from the default, where DeletesPerformedDirectly are done last.
            var uow = new UnitOfWork2(new List <UnitOfWorkBlockType>()
            {
                UnitOfWorkBlockType.DeletesPerformedDirectly,
                UnitOfWorkBlockType.Inserts,
                UnitOfWorkBlockType.Updates
            });

            uow.AddForSave(productLine, null, refetch: true, recurse: false);
            using (var adapter = new DataAccessAdapter())
            {
                uow.Commit(adapter);
            }
        }
        public void UpdateGraphWithDeletes(ProductLineEntity productLine, IList <int> productKeysToRemove)
        {
            if (productLine == null)
            {
                throw new ArgumentNullException(nameof(productLine), $"{nameof(productLine)} is null.");
            }

            var uow = new UnitOfWork2(new List <UnitOfWorkBlockType>()
            {
                UnitOfWorkBlockType.DeletesPerformedDirectly,
                UnitOfWorkBlockType.Inserts,
                UnitOfWorkBlockType.Updates
            });

            if (productKeysToRemove?.Count > 0)
            {
                uow.AddDeleteEntitiesDirectlyCall(typeof(ProductEntity), new RelationPredicateBucket(ProductFields.ProductKey.In(productKeysToRemove)));
            }
            uow.AddForSave(productLine);
            using (var adapter = new DataAccessAdapter())
            {
                uow.Commit(adapter);
            }
        }