Click here to Skip to main content
15,881,173 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C# .Net :
I have big object which contains complex mapping with 20 properties and simple mapping with 38 properties ,Currently i'm mapping these using reflection but still i need a performance improvement in my mapping.

Can any one suggest which library is best to improve the mapping performance?

What I have tried:

i tried with automapper and also manual mapping of properties.
still we need improvement in performance.
My current implementation:
public void FillObjectFromData(object objectToFill, object fromData)
		{
			if (fromData != null)
			{
				System.Reflection.PropertyInfo[] contractProperties = (objectToFill.GetType()).GetProperties();
				foreach (System.Reflection.PropertyInfo contractProperty in contractProperties)
				{
					if (contractProperty.CanWrite && fromData.GetType().GetProperty(contractProperty.Name) != null)
					{
						try
						{
							object dataValue = fromData.GetType().GetProperty(contractProperty.Name).GetValue(fromData, null);
							if ((dataValue != null) || (contractProperty.PropertyType.IsGenericType && contractProperty.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>)))
							{
								contractProperty.SetValue(objectToFill, dataValue, null);
							}
						}
						catch //(Exception ex)
						{

						}
					}
				}
			}
		}
Posted
Updated 21-Jul-17 6:54am
v2
Comments
Afzaal Ahmad Zeeshan 21-Jul-17 8:32am    
That is entirely the default behavior, by the way, why are you mapping the objects, why can't you actually pass objects or try something like serializers?

1 solution

My advice would also be to skip all that mapping. You can easily hide properties by simply defining interfaces and pass those. For example, you receive Abcde but pass it as IAbc which effectively restricts access to some properties.
public interface IAbc 
{
  int A { get; }
  int B { get; }
  int C { get; }
}

public class Abcde : IAbc
{
  public int A { get; set; }
  public int B { get; set; }
  public int C { get; set; }
  public int D { get; set; }
  public int E { get; set; }
}

Good luck!
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900