GetCustomAttributes performance issue (expression trees is the solution??)

c# expression-trees performance reflection

Question

Because I utilize reflection and GetCustomAttributes for my data access, I have a performance issue. It was discovered by the performance profiler. I use the following extension technique:

public static class DataRowExtensions
{
    /// <summary>
    /// Maps DataRow objecto to entity T depending on the defined attributes. 
    /// </summary>
    /// <typeparam name="T">Entity to map.</typeparam>
    /// <param name="rowInstance">DataRow instance.</param>
    /// <returns>Instance to created entity.</returns>
    public static T MapRow<T>(this DataRow rowInstance) where T : class, new()
    {
        //Create T item
        T instance = new T();

        IEnumerable<PropertyInfo> properties = typeof(T).GetProperties();
        MappingAttribute map;
        DataColumn column;

        foreach (PropertyInfo item in properties)
        {
            //check if custom attribute exist in this property
            object[] definedAttributes = item.GetCustomAttributes(typeof(MappingAttribute), false);

            // Tiene atributos
            if (definedAttributes != null && definedAttributes.Length == 1)
            {
                //recover first attribute
                map = definedAttributes.First() as MappingAttribute;

                column = rowInstance.Table.Columns.OfType<DataColumn>()
                                          .Where(c => c.ColumnName == map.ColumnName)
                                          .SingleOrDefault();

                if (column != null)
                {
                    object dbValue = rowInstance[column.ColumnName];
                    object valueToSet = null;

                    if (dbValue == DBNull.Value)//if value is null
                        valueToSet = map.DefaultValue;
                    else
                        valueToSet = dbValue;

                    //Set value in property 
                    setValue<T>(instance, item, valueToSet);
                }
            }
        }

        return instance;
    }

    /// <summary>
    /// Set "item" property.
    /// </summary>
    /// <typeparam name="T">Return entity type</typeparam>
    /// <param name="instance">T type instance</param>
    /// <param name="item">Property name to return value</param>
    /// <param name="valueToSet">Value to set to the property</param>
    private static void setValue<T>(T instance, PropertyInfo item, object valueToSet) where T : class, new()
    {
        if (valueToSet == null)
        {
            CultureInfo ci = CultureInfo.InvariantCulture;

            if (item.PropertyType.IsSubclassOf(typeof(System.ValueType)))
            {
                //if is a value type and is nullable
                if (item.PropertyType.FullName.Contains("System.Nullable"))
                {
                    item.SetValue(instance, null, BindingFlags.Public, null, null, ci);
                }
                else
                {
                    item.SetValue(instance, Activator.CreateInstance(item.PropertyType, null), BindingFlags.Public, null, null, ci);
                }
            }
            else //property type is reference type
            {
                item.SetValue(instance, null, BindingFlags.Public, null, null, ci);
            }
        }
        else // set not null value
        {
            //if is a value type and is nullable
            if (item.PropertyType.FullName.Contains("System.Nullable"))
            {
                item.SetValue(instance, Convert.ChangeType(valueToSet, Nullable.GetUnderlyingType(item.PropertyType)), null);
            }
            else
            {
                item.SetValue(instance, Convert.ChangeType(valueToSet, item.PropertyType), null);
            }
        }
    }
}

In essence, what I do here is map the domain entities to the database fields, after which a data helper automatically assaults the tables. One of these things is an illustration of this:

public class ComboBox
    {
    /// <summary>
    /// Represents a ComboBox item.
    /// </summary>
    [Mapping("CODE", DefaultValue = 0, DBType = DbParametersTypes.Varchar2, IsKey = true, IdentifierFK = "")]
    public string Code { get; set; }

    /// <summary>
    /// Represents Text.
    /// </summary>
    [Mapping("DESCRIPTION", DefaultValue = "", DBType = DbParametersTypes.Varchar2, IsKey = false, IdentifierFK = "")]
    public string Description { get; set; }

    }

The attribute class I employ is:

public sealed class MappingAttribute : Attribute
    {
        public string ColumnName { get; set; }

        public object DefaultValue { get; set; }

        public DbParametersTypes DBType { get; set; }

        public bool IsKey { get; set; }

        public string IdentifierFK { get; set; }

        public bool IsParameter { get; set; } 

        public MappingAttribute(string columnName)
        {
            if (String.IsNullOrEmpty(columnName))
                throw new ArgumentNullException("columnName");

            ColumnName = columnName;
        }               
    }

I've read in here that an expression tree would be able to improve things, but I'm not an expert in expression trees, and I have to use.NET 3.5 to resolve this (in the sample .NET 4 or 4.5 is used...)

¿Suggestions?

I appreciate it.

1
2
10/16/2012 12:43:48 PM

Accepted Answer

public static class DataRowExtensions
{
    /// <summary>
    /// Maps DataRow objecto to entity T depending on the defined attributes. 
    /// </summary>
    /// <typeparam name="T">Entity to map.</typeparam>
    /// <param name="rowInstance">DataRow instance.</param>
    /// <returns>Instance to created entity.</returns>
    public static T MapRow<T>( this DataRow rowInstance ) where T : class, new()
    {
        //Create T item
        var instance = new T();
        Mapper<T>.MapRow( instance, rowInstance );
        return instance;
    }

    #region Nested type: Mapper

    private static class Mapper<T>
        where T : class
    {
        private static readonly ItemMapper[] __mappers;

        static Mapper()
        {
            __mappers = typeof (T)
                .GetProperties()
                .Where( p => p.IsDefined( typeof (MappingAttribute), false ) )
                .Select( p => new
                {
                    Property = p,
                    Attribute = p
                                  .GetCustomAttributes( typeof (MappingAttribute), false )
                                  .Cast<MappingAttribute>()
                                  .FirstOrDefault()
                } )
                .Select( m => new ItemMapper( m.Property, m.Attribute ) )
                .ToArray();
        }

        public static void MapRow( T instance, DataRow row )
        {
            foreach ( var mapper in __mappers )
            {
                mapper.MapRow( instance, row );
            }
        }

        #region Nested type: ItemMapper

        private sealed class ItemMapper
        {
            private readonly MappingAttribute _attribute;
            private readonly PropertyInfo _property;

            public ItemMapper( PropertyInfo property, MappingAttribute attribute )
            {
                _property = property;
                _attribute = attribute;
            }

            public void MapRow( T instance, DataRow rowInstance )
            {
                //TODO: Implement this with the code already provided
            }
        }

        #endregion
    }

    #endregion
}

The first use of an extension technique for a certain<T> a runtime object will be cached by the static constructor.Mapper For every house that has aMappingAttribute connected. Following that, it will utilize the cached mappers to do the real copy for each subsequent call.

You may also createMapper Use distinct subclasses for each branch in your abstraction, andsetValue<T>() Because of this, the majority of your reflection only occurs once.

4
10/16/2012 7:36:40 PM


Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow