lundi 11 mai 2015

Property Mapping with Reflection efficiency

I have an object that I am populating via mapping and lookup woth Reflection, here is the call

this.SearchResults = (from a in response.postings
                              select new SearchResponseModel
                              {
                                  Id = a.id,
                                  TimeStampDate = a.timestampDate,
                                  Body = a.body,
                                  Title = a.heading,
                                  Status = a.status,
                                  State = a.state,
                                  Language = a.language,
                                  Currency = a.currency,
                                  CategoryGroup = a.category_group,
                                  Source = a.source,
                                  ExternalId = a.external_id,
                                  ExternalUrl = a.external_url,
                                  Price = a.price,
                                  Location = PopulateLocation(a.location)
                              }
                              ).ToList();

And the method that does the mapping.

 private static List<LocationLookupModel> PopulateLocation(Location location)
    {
        List<LocationLookupModel> allLocations = new List<LocationLookupModel>();
        if (HttpContext.Current.Session["LocationModel"] == null)
        {

            HttpContext.Current.Session["LocationModel"] = allLocations = new LocationModel().LocationList;
        }
        else
        {
            allLocations = (List<LocationLookupModel>)HttpContext.Current.Session["LocationModel"];
        }
        List<LocationLookupModel> modelList = new List<LocationLookupModel>();

        foreach (PropertyInfo propertyInfo in location.GetType().GetProperties())
        {
            var value = propertyInfo.GetValue(location);
            if (value != null)
            {
                LocationLookupModel model = (from a in allLocations
                                             where a.Code == propertyInfo.GetValue(location).ToString()
                                             select a).FirstOrDefault();
                if (model != null)
                {
                    modelList.Add(model);
                }
            }
        }

        return modelList;
    }
}

The issue that I run into is that allLocations object has about 70k records (it represents a list of location lookup values for countries, states, zipcodes, etc), and populating about 100 instances of SearchResponseModel takes about 20 seconds. This is far too long for a UI call, and I have not been able to find a way to make it faster. I understand I am basically doing 3 nested loops (calling helper method for each population, looping over reflected properties, and finally the LINQ call over 70k records) so there are some time efficiency issues, but I am a bit lost on what tricks I can use to make this process more efficient.





Aucun commentaire:

Enregistrer un commentaire