开发者

Cost of mapping POCO's in a high load system C#

I have a poco that needs to be mapped to another poco in a high traffic system. I intend to map these objects together in a simple mapper similar to this:

public class a 
{
    public int MyValue { get;set; }
    public string YAV { get; set; }
}

public class B
{
    public int开发者_如何学运维 aTestValue { get;set; }
    public string YetAnotherValue { get; set; }
}

public class Mapper 
{
    public static B MapIt(A a)
    {
        return new B { aTestValue = a.MyValue, YetAnotherValue = a.YAV };
    }
}

How much does a mapping like this really affect performance? Ignore the fact that we'll have to write a mapping for all our types and just focus on the performance lost doing the actual mapping.


How much does a mapping like this really affect performance?

I would say that such mapping wouldn't affect performance even in a high traffic system. The cost of calling getters and setters will probably be negligible compared to other operations you might be doing.

Obviously that's just some 2 cents, if you want real stuff do performance benchmarks and measure the difference with and without the mapping.

At least that's what I would do: make something that corresponds to the requirements, then benchmark it, then two possibilities: you are satisfied with the result => ship in production and enjoy life, or you are not satisfied with the results and those benchmarks have allowed you to identify that this part is the bottleneck for your application => refactor the code and start thinking about optimizing it. But never do premature optimization or you will hardly respect the project deadlines.


From our experience, the overhead won't be much. I tested this recently by retrieving 75,000 rows of data using Linq to SQL and then mapping the L2S entities to POCO entities using mapping code we wrote. The cost of doing this was amazing small. If I recall correctly, it was something like 75 to 100 Ms to map 75K rows.


It's almost impossible to know how this will affect performance without knowing something about the scale of the system, the looping structure in which this mapping occurs, etc.

In general, these types of simple mappings are quick, but you can always run into issues that are associated with the scaling issues I mentioned when things such as serialization are involved.


Best thing to do is hook it up to the profiler and make some measurements. Doing a manual mapping like that is a fairly light way to do it so shouldn't be significant. The AutoMapper tool is also available and will reduce the coding time but has a little more overhead as it does other services besides just mapping: Analyzing AutoMapper Performance


How about using conversion operators. Only worry about its performance if a profiler shows it to be a bottleneck.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜