SlapperAutoMapper / Slapper.AutoMapper

Slapper.AutoMapper maps dynamic data to static types. Slap your data into submission!
MIT License
287 stars 76 forks source link

Mapping value type collections #21

Closed BlueInt32 closed 8 years ago

BlueInt32 commented 8 years ago

It does not seem to be possible to map a collection of value types. The "" syntax cannot be used to map.. say a collection of Int32 values right away because no syntax exists to reference the listed type values directly. Next is an example of what I want to do, using the "" syntax and an imaginary "$" notation to reference the value directly. But maybe I'm missing something ? A corresponding stack overflow question can be found here : https://stackoverflow.com/questions/34840171/how-to-map-ilistint-with-slapper-automapper# Thanks.

public class Customer
{
    public int CustomerId;
    public IList<int> OrdersIds;
}
[Test]
public void I_Can_Map_Value_Typed_Collection()
{
    // Arrange
    var dictionary = new Dictionary<string, object>
                         {
                             { "CustomerId", 1 },
                             { "OrdersIds_$", 3 },
                         };

    var dictionary2 = new Dictionary<string, object>
                         {
                             { "CustomerId", 1 },
                             { "OrdersIds_$", 5 }
                         };

    var list = new List<IDictionary<string, object>> { dictionary, dictionary2 };

    // Act
    var customers = Slapper.AutoMapper.Map<Customer>(list);

    // Assert

    // There should only be a single customer
    Assert.That(customers.Count() == 1);

    // There should be two values in OrdersIds, with the correct values
    Assert.That(customers.FirstOrDefault().OrdersIds.Count == 2);
    Assert.That(customers.FirstOrDefault().OrdersIds[0] == 3);
    Assert.That(customers.FirstOrDefault().OrdersIds[1] == 5);
}
odelvalle commented 8 years ago

Great feature @BlueInt32

I'm working to add this feature....

thanks!

odelvalle commented 8 years ago

Hi @BlueInt32, check the last commit...

BlueInt32 commented 8 years ago

This is great, thanks ! Now I'm a bit worried about the fact that the issue's hypothesis is based on value type boxing which is a bad practice in terms of performance. I guess this could explain why slapper did not handle this case in the first place.

odelvalle commented 8 years ago

Yes!

Now Slapper support collections of primitive types, but is your responsibility think about cost. (boxing & unboxing) I would continue refactoring this feature in the future...

The best practice is to encapsulate primitive types inside objects to best performance.

Thanks