Beispiel #1
0
        private static Dictionary<string, Container> BuildContainerDictionaryAndUpdateDbIfNeeded(List<LcboProduct> lcboProducts)
        {
            Dictionary<string, Container> knownContainerDictionary = new Dictionary<string, Container>();
            List<string> knownContainerIdsList = new List<string>();

            // Force the query to evaluate and put the results in a list
            foreach (var container in unitOfWork.Containers.FindAll())
            {
                string containerKey = String.Format(containerIdFormat, container.NumberOfContainers, container.ContainerCapacity);
                knownContainerIdsList.Add(containerKey);
            }

            Dictionary<string, LcboContainerFormat> uniqueLcboContainers = new Dictionary<string, LcboContainerFormat>();
            foreach (LcboProduct lcboProduct in lcboProducts)
            {
                string lcboContainerKey = String.Format(containerIdFormat, lcboProduct.Container.NumberOfContainers, lcboProduct.Container.ContainerCapacity);
                if (!uniqueLcboContainers.ContainsKey(lcboContainerKey))
                {
                    uniqueLcboContainers.Add(lcboContainerKey, lcboProduct.Container);
                }
            }

            Console.WriteLine(String.Format("There are {0} containers in the DB", knownContainerIdsList.Count()));

            IEnumerable<LcboContainerFormat> newContainers = from c in uniqueLcboContainers.Values
                                                             where !knownContainerIdsList.Contains(String.Format(containerIdFormat, c.NumberOfContainers, c.ContainerCapacity))
                                                             select c;

            int newContainerCount = 0;
            foreach (var lcboContainer in newContainers)
            {
                Container newContainer = new Container()
                {
                    ContainerCapacity = lcboContainer.ContainerCapacity,
                    NumberOfContainers = lcboContainer.NumberOfContainers
                };

                unitOfWork.Containers.Add(newContainer);
                newContainerCount++;
            }

            Console.WriteLine(String.Format("There are {0} new containers on the LCBO website", newContainerCount));

            unitOfWork.Commit();
            Console.WriteLine("Updated DB");

            foreach (Container container in unitOfWork.Containers.FindAll())
            {
                knownContainerDictionary.Add(String.Format(containerIdFormat, container.NumberOfContainers, container.ContainerCapacity), container);
            }

            Console.WriteLine(String.Format("Built dictionary of {0} containers", knownContainerDictionary.Count));
            return knownContainerDictionary;
        }
        private Dictionary<string, Container> BuildContainerDictionaryAndUpdateDbIfNeeded2(List<LcboProduct> lcboProducts, StringBuilder result, IConnection signalrConnection)
        {
            Dictionary<string, Container> knownContainerDictionary = new Dictionary<string, Container>();
            List<string> knownContainerIdsList = new List<string>();

            // Force the query to evaluate and put the results in a list
            foreach (var container in domainContext.Containers)
            {
                string containerKey = String.Format(containerIdFormat, container.NumberOfContainers, container.ContainerCapacity);
                knownContainerIdsList.Add(containerKey);
            }

            Dictionary<string, LcboContainerFormat> uniqueLcboContainers = new Dictionary<string, LcboContainerFormat>();
            foreach (LcboProduct lcboProduct in lcboProducts)
            {
                string lcboContainerKey = String.Format(containerIdFormat, lcboProduct.Container.NumberOfContainers, lcboProduct.Container.ContainerCapacity);
                if (!uniqueLcboContainers.ContainsKey(lcboContainerKey))
                {
                    uniqueLcboContainers.Add(lcboContainerKey, lcboProduct.Container);
                }
            }

            signalrConnection.Broadcast(String.Format("There are {0} containers in the DB", knownContainerIdsList.Count()));
            result.AppendFormat("There are {0} containers in the DB\r\n", knownContainerIdsList.Count());

            IEnumerable<LcboContainerFormat> newContainers = from c in uniqueLcboContainers.Values
                                                             where !knownContainerIdsList.Contains(String.Format(containerIdFormat, c.NumberOfContainers, c.ContainerCapacity))
                                                             select c;

            int newContainerCount = 0;
            foreach (var lcboContainer in newContainers)
            {
                Container newContainer = new Container()
                {
                    ContainerCapacity = lcboContainer.ContainerCapacity,
                    NumberOfContainers = lcboContainer.NumberOfContainers
                };

                domainContext.Save(newContainer);
                newContainerCount++;
            }

            signalrConnection.Broadcast(String.Format("There are {0} new containers on the LCBO website", newContainerCount));
            result.AppendFormat("There are {0} new containers on the LCBO website\r\n", newContainerCount);

            domainContext.SaveChanges();
            signalrConnection.Broadcast("Updated DB");
            result.Append("Updated DB\r\n");

            foreach (Container container in domainContext.Containers)
            {
                knownContainerDictionary.Add(String.Format(containerIdFormat, container.NumberOfContainers, container.ContainerCapacity), container);
            }

            signalrConnection.Broadcast(String.Format("Built dictionary of {0} containers", knownContainerDictionary.Count));
            result.AppendFormat("Built dictionary of {0} containers\r\n", knownContainerDictionary.Count);
            return knownContainerDictionary;
        }