AutoMapper objects with various types of properties

I want to bind Entity Framework entities (created from an obsolete database) to user DTO objects (which should be nice and clean).

My old DB has entities that look something like this:

internal class Order {
    int id;
    string Shipping_date;
    string quantity;
}

And I want to map it to a more convenient DTO object:

public class OrderDto {
    int id;
    DateTime? ShippingDate;
    int Quantity;
}

I wrote an โ€œobject containerโ€ to provide dependency injection, which returns values โ€‹โ€‹as follows:

public IEnumerable<OrderDto> GetPaginatedOrders(int page, int pageSize)
{
    return this.db.Orders
               .OrderByDescending(c => c.id)
               .Paginate(page, pageSize)
               .Project()
               .To<OrderDto>()
               .AsEnumerable();
}

So: changing types and changing property names.

If it were just a change of property names, it would be easy-but-tiring:

Mapper.CreateMap<Order, OrderDto>()
  .ForMember(dest => dest.Quantity, opt => opt.MapFrom(src => src.quantity))
  .ForMember(dest => dest.ShippingDate, opt => opt.MapFrom(src => src.Shipping_date));

This is not enough to change the type. I tried a whole bunch of things:

  • Parsing properties in a mapping declaration, for example src => int.Parse(src.quantity), but he doesn't like Linq.
  • EF , QuantityInt { get { return int.Parse(this.quantity) } }, , AutoMapper .
  • Mapper.CreateMap<string, int>().ConvertUsing(Convert.ToInt32), Unable to create a map expression from System.String to System.Int32.
  • , ResolutionContext.SourceValues ( , , AutoMapper - ).

, AutoMapper , , , , ?

!

+4
1

.Project() Linq , SQL , , .

Mapper.Map<IEnumerable<Order>, IEnumerable<OrderDto>>(src) 

.

+2

All Articles