예제 #1
0
        private async Task Save(IList <ProductInfo> productInfos)
        {
            var products = new List <Product>(productInfos.Count);

            foreach (var info in productInfos)
            {
                try
                {
                    var product = new Product()
                    {
                        Url         = info.Url,
                        RawHtml     = info.RawHtml,
                        Description = info.Description,
                        Price       = info.Price,
                        VendorCode  = info.VendorCode,
                        Categories  = JsonConvert.SerializeObject(info.Categories),
                        Images      = JsonConvert.SerializeObject(info.Images),
                        Variations  = JsonConvert.SerializeObject(info.Variations),
                        Updated     = DateTime.Now,
                    };
                    products.Add(product);
                }
                catch (Exception ex)
                {
                    Error.Invoke(this, ex.Message);
                }
            }
            await _saver.SaveAsync(products);
        }
        /// <summary>
        /// Saves items to external data source using configured saver component.
        /// </summary>
        /// <param name="correlationId">(optional) transaction id to trace execution through call chain.</param>
        public Task SaveAsync(string correlationId)
        {
            if (_saver == null)
            {
                return(Task.Delay(0));
            }

            _lock.EnterWriteLock();

            try
            {
                var task = _saver.SaveAsync(correlationId, _items);
                task.Wait();

                _logger.Trace(correlationId, "Saved {0} of {1}", _items.Count, _typeName);

                return(Task.Delay(0));
            }
            finally
            {
                _lock.ExitWriteLock();
            }
        }