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);
        }
        /// <summary>
        /// Commits the customer manager changes specified in the unit of work passed in.
        /// </summary>
        /// <param name="uow">The unit of work with the work to commit.</param>
        /// <returns>true if commit succeeded, false otherwise</returns>
        public bool CommitCustomerManagerChanges(UnitOfWork2 uow)
        {
            if (uow == null)
            {
                return(false);
            }
            int numberOfElementsProcessed = 0;

            using (var adapter = new DataAccessAdapter())
            {
                numberOfElementsProcessed = uow.Commit(adapter);
            }
            return(numberOfElementsProcessed > 0);
        }
        public void DeleteByKey(int productLineKey)
        {
            // let's directly delete the entities, without fetching them. Use a unit of work for this
            // to wrap everything neatly in a transaction when it's committed. A Unit of work is a
            // persistence agnostic object you can pass on freely to add work and then have all the work
            // performed in a single transaction.
            var uow = new UnitOfWork2();

            uow.AddDeleteEntitiesDirectlyCall(typeof(ProductEntity),
                                              new RelationPredicateBucket(ProductFields.ProductLineKey.Equal(productLineKey)));
            uow.AddDeleteEntitiesDirectlyCall(typeof(ProductLineEntity),
                                              new RelationPredicateBucket(ProductLineFields.ProductLineKey.Equal(productLineKey)));
            using (var adapter = new DataAccessAdapter())
            {
                uow.Commit(adapter);
            }
        }
        public void Delete(ProductLineEntity productLine)
        {
            if (productLine == null)
            {
                throw new ArgumentNullException(nameof(productLine), $"{nameof(productLine)} is null.");
            }

            // We'll have to delete the whole graph so first the related entities, then the main entity.
            // Let's use a Unit of work here for that.
            var uow = new UnitOfWork2();

            uow.AddCollectionForDelete(productLine.Products);
            uow.AddForDelete(productLine);
            using (var adapter = new DataAccessAdapter())
            {
                uow.Commit(adapter);
            }
        }
        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);
            }
        }