示例#1
0
        /// <summary>
        /// Transforms the domain Vehicle to our Answer's Vehicle object.
        /// </summary>
        public static DtoVehicle From(Vehicle vehicle)
        {
            DtoVehicle dtoVehicle = new DtoVehicle
            {
                VehicleId = vehicle.VehicleId,
                Year      = vehicle.Year,
                Make      = vehicle.Make,
                Model     = vehicle.Model
            };

            return(dtoVehicle);
        }
示例#2
0
        /// <summary>
        /// Creates a new Answer for this datasetId
        /// </summary>
        /// <remarks>
        /// Gets information for all vehicles in parallel.
        /// When information for a vehicle returns, get the information for its dealer, unless we have it or are already getting it.
        /// Wait for all vehicle and dealer information, transform it into an Answer, then return the Answer.
        /// </remarks>
        private async Task <Answer> GetAnswer(string datasetId)
        {
            ICollection <int> vehicleIds = (await apiService.GetVehicles(datasetId)).VehicleIds;

            Dictionary <int, ICollection <DtoVehicle> > dealerVehicleMap = new Dictionary <int, ICollection <DtoVehicle> >();
            var tasks = new List <Task <Model.Dealers.Dealer> >();

            foreach (int vehicleId in vehicleIds)
            {
                // Get all vehicle information in parallel
                var dealer = apiService.GetVehicle(datasetId, vehicleId).ContinueWith(task =>
                {
                    if (task.IsFaulted)
                    {
                        throw GetInnerMostException(task.Exception);
                    }

                    Vehicle vehicle = task.Result;
                    int dealerId    = vehicle.DealerId;

                    bool dealerAlreadyExists = UpdateDealerVehicleMap(dealerVehicleMap, dealerId, vehicle);

                    // Get this vehicle's dealer name if we aren't already getting it
                    return(dealerAlreadyExists ?
                           null :
                           apiService.GetDealer(datasetId, dealerId).Result);
                });

                tasks.Add(dealer);
            }

            // Wait for all tasks to finish then return Answer
            return(await Task.WhenAll(tasks).
                   ContinueWith(task =>
            {
                if (task.IsFaulted)
                {
                    throw GetInnerMostException(task.Exception);
                }
                return DtoConverter.From(dealerVehicleMap, task.Result);
            }));
        }
示例#3
0
        /// <summary>
        /// Adds this dealer to our dealer dictionary, and adds this vehicle to the dealer's vehicle list.
        /// </summary>
        /// <returns>
        /// Returns true if this dealer was already in our dictionary, otherwise false.
        /// </returns>
        private bool UpdateDealerVehicleMap(Dictionary <int, ICollection <DtoVehicle> > dictionary, int dealerId, Vehicle vehicle)
        {
            bool dealerAlreadyExists;
            ICollection <DtoVehicle> vehicleList = new List <DtoVehicle>();

            lock (dictionary)
            {
                dealerAlreadyExists = dictionary.ContainsKey(dealerId);
                if (dealerAlreadyExists)
                {
                    vehicleList = dictionary[dealerId];
                }
                else
                {
                    dictionary.Add(dealerId, vehicleList);
                }
            }
            vehicleList.Add(DtoConverter.From(vehicle));

            return(dealerAlreadyExists);
        }