Mardo
Mardo

Reputation: 117

Abstract Classes Mapping Derived Types

I have the following abstract class an its implementations:

public abstract class TransactionResult : AuditableEntity
{
    public int Id { get; set; }
    [Required, MaxLength(25)]
    public string Status { get; set; }

    [Required, MaxLength(50)]
    public string ReferenceId { get; set; }

    public string ResultMetaData { get; set; }

    [Required, MaxLength(10)]
    public string CardType { get; set; }
    [Required, MaxLength(10)]
    public string CardDescription { get; set; }
    public int? CustomerSavedCardId { get; set; }
    public CustomerSavedCard CustomerSavedCard { get; set; }

    public int? PaymentPlanDetailId { get; set; }
    public PaymentPlanDetail PaymentPlanDetail { get; set; }

    public int? CustomerMembershipBillingId { get; set; }
    public CustomerMembershipBilling CustomerMembershipBilling { get; set; }

    public Guid? NotificationId { get; set; }
    
  
    public Notification Notification { get; set; }


    [Required, MaxLength(25)]
    public string Source { get; set; }

    public string CustomerKey { get; set; }
    public Customer Customer { get; set; }


    public int Amount { get; set; }
}

public class Approve : TransactionResult
{

}

public class Decline : TransactionResult
{
    public string Reason { get; set; }
}

I am trying to get the reason while using auto mapper

 profile.CreateMap<Entities.TransactionResult, TransactionsDto>()
                .ForMember(dest => dest.CustomerName, opt => opt.MapFrom(src => src.Customer.FirstName + " " + src.Customer.LastName))
                .ForMember(dest => dest.NotificationId, opt => opt.MapFrom(src => src.NotificationId))
                .ForMember(dest => dest.Reason, opt => opt.MapFrom(src => src.GetType() == typeof(Entities.Decline) ? ((Entities.Decline)src).Reason : null))

I get the error when adding the Decline casting to the transaction results.

The client projection contains a reference to a constant expression of 'System.RuntimeType'. This could potentially cause a memory leak; consider assigning this constant to a local variable and using the variable in the query instead. See https://go.microsoft.com/fwlink/?linkid=2103067 for more information.

Upvotes: 0

Views: 165

Answers (1)

Oignon_Rouge
Oignon_Rouge

Reputation: 307

Automapper supports inheritance.

Remove the line for the property Reason and create a specific profile for Decline as follows:

profile.CreateMap<Entities.Decline, TransactionsDto>()
    .IncludeBase<Entities.TransactionResult, TransactionsDto>();

Upvotes: 1

Related Questions