fluentsprings / ExpressMapper

Mapping .Net types
http://www.expressmapper.org
Other
310 stars 65 forks source link

Lists are copying by reference instead of duplicating #83

Closed bzuillsmith closed 8 years ago

bzuillsmith commented 8 years ago

Maybe I'm missing something obvious or mistaken about how this should code function. The code below shows a test of the .Map(src, dest) functionality. The Assert fails. Why?

// NUnit Test
[Test]
public void ShouldNotBeTheSame()
{
    var mapper = new MappingServiceProvider();

    mapper.Register<AClass, AClass>();
    mapper.Register<BClass, BClass>();

    var a = new AClass()
    {
        AProperty = "test",
        InnerObjects = new List<BClass>() { new BClass { BProperty = "test2!" } }
    };

    var a2 = new AClass();
    mapper.Map(a, a2);
    // Should be different instances with the same data
    Assert.AreNotSame(a.InnerObjects, a2.InnerObjects); //Fails here, but why?
}

public class AClass
{
    public string AProperty { get; set; }
    public List<BClass> InnerObjects { get; set; }
}

public class BClass
{
    public string BProperty { get; set; }
}
bzuillsmith commented 8 years ago

Seems to be specific to that signature of .Map because var a3 = mapper.Map<AClass, AClass>(a); works properly.

anisimovyuriy commented 8 years ago

Dear @bzuillsmith, Thanks for your input! Mapping with destination and without destination is different compiled code. Mapping without destination less heavy, mapping with destination - tries to reuse existing object on destination side. But in case you map with destination the same type - it should work like a deep copy. Let me check and I'll get back to you ASAP.

Thanks.

anisimovyuriy commented 8 years ago

Dear @bzuillsmith,

Actually the should be the same as you are using mapping with destination: mapper.Map(a, a2);. If you want deep copy then use var a2 = mapper.Map(a);

Let me know if you have any concerns.

Thanks, Yuriy

bzuillsmith commented 8 years ago

Ah, I see. I wanted to do a deep copy using mapper.Map(a, this) but perhaps I can do it in two steps: mapper.Map(mapper.Map(a), this)? I'll give it a try. Thanks

anisimovyuriy commented 8 years ago

Yeah I think you are on a right way.

shay1e commented 8 years ago

It'd be interesting to compare that deep cloning method to other high performance libraries.

anisimovyuriy commented 8 years ago

Sure @shay1e - after all p1 - I'll do it :)

Thanks!

sgf commented 6 years ago

@anisimovyuriy Hi,was ExpressMapper support Deep Copy now? i seen a project can do it. https://github.com/ReubenBond/DeepCopy