I am using AutoMapper for object mapping. Sometime the source value is null so i want to substitute it with "" rather than putting NULL in database. The source object have many nullable data types like Date, Decimal and Int. Currently i am using
public class Invoice
{
public Guid Id { get; set; }
public string Number { get; set; }
public Contact Contact { get; set; }
public InvoiceType Type { get; set; }
public InvoiceStatus Status { get; set; }
public LineAmountType LineAmountTypes { get; set; }
public DateTime? Date { get; set; }
public DateTime? DueDate { get; set; }
public DateTime? ExpectedPaymentDate { get; set; }
public DateTime? PlannedPaymentDate { get; set; }
public decimal? SubTotal { get; set; }
public decimal? TotalTax { get; set; }
public decimal? Total { get; set; }
public decimal? TotalDiscount { get; set;
public decimal? CurrencyRate { get; set; }
public DateTime? FullyPaidOnDate { get; set; }
public decimal? AmountDue { get; set; }
public decimal? AmountPaid { get; set; }
public decimal? AmountCredited { get; set; }
}
public partial class TempInvoice
{
public int RowId { get; set; }
public System.Guid InvoiceID { get; set; }
public string InvoiceNumber { get; set; }
public Nullable<System.Guid> ContactID { get; set; }
public string Type { get; set; }
public string Status { get; set; }
public string LineAmountType { get; set; }
public Nullable<System.DateTime> InvoiceDate { get; set; }
public Nullable<System.DateTime> DueDate { get; set; }
public Nullable<System.DateTime> ExpectedPaymentDate { get; set; }
public Nullable<System.DateTime> PlannedPaymentDate { get; set; }
public Nullable<decimal> SubTotal { get; set; }
public Nullable<decimal> TotalTax { get; set; }
public Nullable<decimal> Total { get; set; }
public Nullable<decimal> TotalDiscount { get; set; }
public Nullable<decimal> CurrencyRate { get; set; }
public Nullable<System.DateTime> FullyPaidOnDate { get; set; }
public Nullable<decimal> AmountDue { get; set; }
public Nullable<decimal> AmountCredited { get; set; }
}
public class InvoiceMapper : Profile
{
public InvoiceMapper()
{
CreateMap<Invoice, TempInvoice>()
.ForMember(des => des.PaymentID, map => map.MapFrom(src => src.Id))
.ForMember(des => des.InvoiceID, map => map.MapFrom(src => src.Invoice.Id))
.ForMember(des => des.PaymentDate, map => map.MapFrom(src => src.Date))
.ForAllMembers(src => src.NullSubstitute(""))//this doesn't work
;
}
}
But i am getting the above error. Is there a way i can exclude the datatime type and set int or decimal to 0 and string to "" ? Is there a generic method i can use for all datatypes? Thanks
You could use a resolver function with ResolveUsing, using your own custom mapping function that returns an empty string if the destination is a string, otherwise null.
For example:
private static object MyMapperResolver(object source, bool returnEmptyString = true) {
return source ?? (returnEmptyString ? "" : source);
}
public static IMapper InitMappings() {
Mapper.Initialize(cfg => {
cfg.CreateMap<Invoice, TempInvoice>()
.ForMember(des => des.PaymentID, map => map.ResolveUsing(src => MyMapperResolver(src.Id)))
.ForMember(des => des.InvoiceID, map => map.ResolveUsing(src => MyMapperResolver(src.Invoice.Id)))
.ForMember(des => des.PaymentDate, map => map.ResolveUsing(src => MyMapperResolver(src.Date, false)))
;
});
return Mapper.Instance;
}
You can use AddTransform
public class InvoiceMapper : Profile
{
public InvoiceMapper()
{
CreateMap<Invoice, TempInvoice>()
.ForMember(des => des.PaymentID, map => map.MapFrom(src => src.Id))
.ForMember(des => des.InvoiceID, map => map.MapFrom(src => src.Invoice.Id))
.ForMember(des => des.PaymentDate, map => map.MapFrom(src => src.Date))
.AddTransform<string>(s => string.IsNullOrWhiteSpace(s) ? "" : s); // this line
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.