Object copy approaches in .net: Auto Mapper, Emit Mapper, Implicit Operation, Property Copy
Asked Answered
K

3

12

If some one knows any more ways of doing this in .NET and also what is your opinions about that approaches? Which approach you choose and why?

Here is the tests of different ways of object copy in .NET.

Tests Related to this original thread: How to copy value from class X to class Y with the same property name in c#?

So, here it is, you can run it yourself:

static void Main(string[] args)
    {
        Student _student = new Student();
        _student.Id = 1;
        _student.Name = "Timmmmmmmmaaaahhhh";
        _student.Courses = new List<int>();
        _student.Courses.Add(101);
        _student.Courses.Add(121);

        Stopwatch sw = new Stopwatch();

        Mapper.CreateMap<Student, StudentDTO>();            

        StartTest(sw, "Auto Mapper");

        for (int i = 0; i < 1000000; i++)
        {
            StudentDTO dto = Mapper.Map<Student, StudentDTO>(_student);
        }

        StopTest(sw);

        StartTest(sw, "Implicit Operator");

        for (int i = 0; i < 1000000; i++)
        {
            StudentDTO itemT = _student;
        }

        StopTest(sw);

        StartTest(sw, "Property Copy");

        for (int i = 0; i < 1000000; i++)
        {

            StudentDTO itemT = new StudentDTO
            {
                Id = _student.Id,
                Name = _student.Name,
            };

            itemT.Courses = new List<int>();
            foreach (var course in _student.Courses)
            {
                itemT.Courses.Add(course);
            }
        }

        StopTest(sw);

        StartTest(sw, "Emit Mapper");

        ObjectsMapper<Student, StudentDTO> emitMapper = ObjectMapperManager.DefaultInstance.GetMapper<Student, StudentDTO>();

        for (int i = 0; i < 1000000; i++)
        {
            StudentDTO itemT = emitMapper.Map(_student);
        }

        StopTest(sw);
    }

Tests results on my PC:

Test Auto Mapper:22322 ms

Test Implicit Operator:310 ms

Test Property Copy:250 ms

Test Emit Mapper:281 ms

You can get emit and auto -mappers from here:

http://emitmapper.codeplex.com/

http://automapper.codeplex.com/

Koblick answered 11/8, 2010 at 11:12 Comment(3)
That's great, but what is your question?Islam
Yeah, I don't see the point of all this. You need a goal before you can even decide that you need a copy (instead of using the original object), and you need a goal in order to choose the right approach. That said, another copier is MemberwiseClone().Epistasis
Are you trying to create a copy of object for testing..I don't understand question...u can use mole in .net4...Mapper would work best..But please clarify your questionCarabao
J
5

It is also possible to use T4 to generate classes that will generate property copy code.

Good: runs as fast as it is possible Bad: "coding" in T4 Ugly: Making build scripts that allow you to compile it all in one go

Jutland answered 2/11, 2010 at 21:52 Comment(2)
To the spam flaggers, please stop. Also, this answer is not half bad.Noranorah
Do you know of any resources to get started on T4 to come up with this type of copy helpers?Gyn
R
1

Have you tried overriding the Clone method to copy object instances? This way, you get a new student object like this:

for (int i = 0; i < 1000000; i++) 
{ 
     StudentDTO itemT = _student.Clone(); 
}

I find this approach the easiest way of copying objects into new objects, though I haven't done any speed tests to find out how well it performs against the methods you suggest.

Rici answered 8/11, 2010 at 11:21 Comment(0)
G
1

the Clone is for copy the same Type not for copying from 2 different objects Type then can't be used for this scope.

Grovergroves answered 20/6, 2013 at 8:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.