Beispiel #1
0
        /*
         * Optimize the calculation process by using multiple threads (8) for calculation
         *
         * We want to know the total invoice values
         * This is calculated by taking all sold products and combine them
         * With 21% (VAT) and 10% (Handling cost)
         * Orders greater than 1,000 euro will receive a 5% deduction before calculating additional costs
         *
         * Use blockingcollection
         */
        public async Task <IEnumerable <OrderTotalAmount> > GetTotalAmountPerInvoice(PageModel page)
        {
            var orders = await _context.Orders
                         .Skip((page.Page - 1) *page.PageSize)
                         .Take(page.PageSize)
                         .Include(o => o.OrderProducts)
                         .AsNoTracking()
                         .ToListAsync();

            //List<OrderTotalAmount> result = new List<OrderTotalAmount>(orders.Count);
            //foreach (var order in orders ?? Enumerable.Empty<Order>())
            //{
            //    result.Add(_orderProcessor.CalculateInvoiceTotal(order));
            //}

            //return result;

            var result = new BlockingCollection <OrderTotalAmount>(orders.Count);

            Parallel.ForEach(
                orders,
                new ParallelOptions {
                MaxDegreeOfParallelism = 8
            },                                                      // how many threads will be used at one moment
                order =>
            {
                result.Add(_orderProcessor.CalculateInvoiceTotal(order));
            }
                );

            return(result);
        }
Beispiel #2
0
        /*
         * Optimize the calculation process by using multiple threads (8) for calculation
         *
         * We want to know the total invoice values
         * This is calculated by taking all sold products and combine them
         * With 21% (VAT) and 10% (Handling cost)
         * Orders greater than 1,000 euro will receive a 5% deduction before calculating additional costs
         *
         * Use blockingcollection
         */
        public async Task <IEnumerable <OrderTotalAmount> > GetTotalAmountPerInvoice(PageModel page)
        {
            var orders = await _context.Orders
                         .Skip((page.Page - 1) *page.PageSize)
                         .Take(page.PageSize)
                         .Include(o => o.OrderProducts)
                         .AsNoTracking()
                         .ToListAsync();

            List <OrderTotalAmount> result = new List <OrderTotalAmount>(orders.Count);

            foreach (var order in orders ?? Enumerable.Empty <Order>())
            {
                result.Add(_orderProcessor.CalculateInvoiceTotal(order));
            }

            return(result);
        }