HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Property Mapping with Reflection efficiency

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
reflectionwithpropertymappingefficiency

Problem

I have an object that I am populating via mapping and lookup with Reflection:

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 PopulateLocation(Location location)
{
List allLocations = new List();
if (HttpContext.Current.Session["LocationModel"] == null)
{

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

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);
}
}

Solution

Well to start with reflection is incredibly slow. And second I'm not even sure why you're using reflection to begin with since you have the strongly typed Location. Don't use reflection. I don't think you need to compare every single property. Pick only the ones that could possibly match.

If you insist on using reflection here's a faster impl:

private static readonly locationProperties = typeof(Location).GetProperties();

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


Without reflection:

private static List PopulateLocation(Location location)
{
    List allLocations = new List();
    if (HttpContext.Current.Session["LocationModel"] == null)
        HttpContext.Current.Session["LocationModel"] = allLocations = new LocationModel().LocationList;
    else
        allLocations = List)HttpContext.Current.Session["LocationModel"];
    return allLocations.Select(x => x.Code == location.city || x.Code == location.country || x.Code == location.county || x.Code == location.locality || x.Code == location.metro || x.Code == location.region || x.Code == location.state || x.Code == location.zipcode).ToList();
}

Code Snippets

private static readonly locationProperties = typeof(Location).GetProperties();

foreach (PropertyInfo propertyInfo in locationProperties)
{
    var value = propertyInfo.GetValue(location);
    if (value != null)
    {
        LocationLookupModel model = allLocations.FirstOrDefault(a.Code == value.ToString());
        if (model != null)
        {
            modelList.Add(model);
        }
    }
}
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"];
    return allLocations.Select(x => x.Code == location.city || x.Code == location.country || x.Code == location.county || x.Code == location.locality || x.Code == location.metro || x.Code == location.region || x.Code == location.state || x.Code == location.zipcode).ToList();
}

Context

StackExchange Code Review Q#90411, answer score: 2

Revisions (0)

No revisions yet.