1

Is it possible to map a ICollection to a single entity BonusDto?

I am using the ICollection inside the Customer class to get all rows and from the rows i am calculation the Retained & PreviouslyPaid fields in a partial Bonus class that i want to map to BonusDto.

public class BonusDto 
    {
        public virtual long CustomerId { get; set; }

        public virtual decimal Retained { get; set; }
        public virtual decimal PreviouslyPaid { get; set; }
    }

  public class Customer
    {
        public virtual string Firstname { get; set; }
        public virtual ICollection<Bonus> Bonuses { get; set; }

    }

  public class CustomerDto
  {
        public virtual string Firstname { get; set; }
        public virtual BonusDto Bonus { get; set; }
  }

public partial class Bonus
    {
        private decimal _previouslyPaid;

        public decimal PreviouslyPaid
        {
            get
            {
                if (UsedDate.HasValue)
                {
                    _previouslyPaid += Amount;
                }
                return _previouslyPaid;
            }
        }

        private decimal _retained;

        public decimal Retained
        {
            get
            {
                if (!UsedDate.HasValue && DueDate > DateTime.UtcNow)
                {
                    _retained += Amount;
                }

                return _retained;
            }
        }
    }
  • 1
    How do you want to aggregate the values of every `Bonus` inside the collection into one `BonusDto`? – Andrew Whitaker Aug 25 '14 at 14:25
  • We need more detail about what exactly you want to do with the information – Juan Aug 25 '14 at 14:27
  • I want to map the Retained & PreviouslyPaid values from the ICollection to a BonusDto class, all othere entities should be ignored. Do you need anything more? –  Aug 25 '14 at 14:32
  • You should provide an example function `BonusDTO ToDTO(ICollection)` that shows your logic. – hansmaad Aug 25 '14 at 14:33
  • @hansmaad but i want to use automapper? Sorry i don't know if im slow :) –  Aug 25 '14 at 14:39
  • Nobody knows how you want to merge multipe `Bonus` objects into a single `BonusDTP`. Even AutoMapper can't know that. – hansmaad Aug 25 '14 at 14:40
  • You might need the Custom Value Resolvers feature of AutoMapper. Possible related question: http://stackoverflow.com/questions/3471333/mapping-from-list-down-to-object-with-automapper – Kevin D Aug 25 '14 at 14:54
  • Basically what are the rules for turning multiple `Bonus` entities into a single `BonusDTO`? Are you adding together each `Bonus`'s `Retained` and `PreviouslyPaid` properties? Without this information there's no way to come up with a valid solution. – Andrew Whitaker Aug 25 '14 at 15:02

1 Answers1

1

You have to define the mapping from Bonuses to Bonus using ForMember and ResolveUsing. Pass your own mapping logic from ICollection<Bonus> to a single BonusDto:

    static BonusDto YourLogic(Customer customer)
    {
        return new BonusDto() 
        { 
           Retained = customer.Bonuses.Select(b => b.Retained).Sum() 
        };
    }

    static void Main(string[] args)
    {
        AutoMapper.Mapper.CreateMap<Customer, CustomerDto>()
            .ForMember(dto => dto.Bonus, opt => opt.ResolveUsing(YourLogic));

        var customerDto = AutoMapper.Mapper.Map<CustomerDto>(customer);
    }
hansmaad
  • 18,417
  • 9
  • 53
  • 94