Performance problem in Get/Set Property when using private backing fields

Performance problem in Get/Set Property when using private backing fields

Old forum URL: forums.lhotka.net/forums/t/10964.aspx


xal posted on Wednesday, December 07, 2011

Hey everyone, I found an issue in the latest version.

BusinessBase has this code:

    protected P GetProperty<P>(PropertyInfo<P> propertyInfo, P field)
    {
      return GetProperty<P>(propertyInfo.Name, field, propertyInfo.DefaultValue, Security.NoAccessBehavior.SuppressException);
    }

Then the string version of GetProperty that this is calling gets the property info object based on the string name like this:

var propertyInfo = FieldManager.GetRegisteredProperties().Where(c => c.Name == propertyName).FirstOrDefault();

if the propertyInfo object returned is null, it throws an exception.

 

This would all be better if all the logic in that function was moved to

protected P GetProperty<P>(PropertyInfo<P> propertyInfo, P field, P defaultValue, Security.NoAccessBehavior noAccess)

 

I tested this change in a dummy business object with 3 properties and the perfomance improvement was 4 times better. I'm sure the results would vary depending on the number of properties.

 

It would also be good to implement some better method for looking up propertyinfo objects by name, probably by using a dictionary, as opposed to the current ".Where(c => c.Name == propertyName).FirstOrDefault();" I could explore this as well if you're interested.

 

Anyway, I'm adding an attachment to this post with the proposed changes so far, just moving code from one method to the other. This affects all versions of GetProperty, SetProperty for private fields and their *Convert versions.

 

Cheers,

Andrés

 

Copyright (c) Marimer LLC