自动映射配置复杂映射

时间:2015-02-15 08:43:53

标签: c# automapper

我一直在使用automapper尝试弄清楚如何处理不同的情况。我遇到了下面的情况,需要一些帮助来找出最好的方法。以下是我的EF相关课程;

public sealed class Invoice
{
    public int InvoiceID { get; set; }

    public DateTime InvoiceDate { get; set; }

    public string CustomerName { get; set; }

    public string CustomerAddress { get; set; }

    public double? DiscountAmt { get; set; }

    public Transaction InvoiceTransaction { get; set; }

    public int TransactionID { get; set; }

}

public sealed class Transaction
{
    public Transaction()
    {
        this.TransactionItems = new List<TransactionDetail>();
    }

    public int TransactionID { get; set; }

    public DateTime TransactionDate { get; set; }

    public DateTime TransactionLogDate { get; set; }

    public TransactionType TransactionType { get; set; }

    public IList<TransactionDetail> TransactionItems { get; set; }

    public Invoice RefferingInvoice { get; set; }

    public string Remarks { get; set; }
}

public sealed class TransactionDetail
{
    public int TransactionID { get; set; }

    public string ProductItemcode { get; set; }

    public Product Product { get; set; }

    public double Qty
    {
        get
        {
            return Math.Abs(this.StockChangeQty);
        }
    }

    public double StockChangeQty { get; set; }

    public double? UnitPrice { get; set; }
}

public sealed class Product
{
    public Product()
    {
        this.StockTransactions = new List<TransactionDetail>();
    }

    public string ItemCode { get; set; }

    public string ProductName { get; set; }

    public string Manufacturer { get; set; }

    public double UnitPrice { get; set; }

    public IList<TransactionDetail> StockTransactions { get; set; }

    public double Qty
    {
        get
        {
            if (this.StockTransactions.Count == 0)
            {
                return 0;
            }
            else
            {
                return this.StockTransactions.Sum(x => x.StockChangeQty);
            }
        }
    }

    public bool Discontinued { get; set; }

}

这些是我的视图模型类;

public class InvoiceReportViewModel
{
    public InvoiceReportViewModel()
    {
        LineItems = new List<InvoiceReportLineItemViewModel>();
    }

    public int InvoiceID { get; set; }

    public DateTime InvoiceDate { get; set; }

    public string CustomerName { get; set; }

    public string CustomerAddress { get; set; }

    public double? DiscountAmt { get; set; }

    public string Remarks { get; set; }

    public string StringInvoiceNo
    {
        get
        {
            return InvoiceID.ToString("########");
        }
    }

    public IList<InvoiceReportLineItemViewModel> LineItems { get; set; }
}

public class InvoiceReportLineItemViewModel
{
    public string ItemCode { get; set; }

    public string ProductName { get; set; }

    public string Manufacturer { get; set; }

    public double? UnitPrice { get; set; }

    public double Qty { get; set; }

    public double LineTotal
    {
        get
        {
            if (UnitPrice.HasValue)
            {
                return UnitPrice.Value * Qty;
            }
            else
            {
                return 0;
            }
        }
    }
}

我的要求是将Invoice EF对象转换为InvoiceReportViewModel对象
为此,我需要设置配置文件。这是我遇到问题的地方;因为它不直接。我看到这个完成的唯一方法是通过扩展TypeConverter并通过重写ConvertCore方法手动执行转换来创建我自己的Resolver。

如果有另一种方法可以完成这项工作(工作量较少的话)???

另外我觉得我可以使用Mapper.CreateMap()将Transaction TransactionDetails EF类映射到InvoiceReportLineItemViewModel类.ForMember(...
但是如何使用映射器在ConvertCore方法中转换它呢?

提前致谢

1 个答案:

答案 0 :(得分:0)

在您的情况下,我没有看到使用任何自定义转换器的任何要求。 您可以使用简单的Mapper.CreateMap将Invoice EF对象转换为InvoiceReportViewModel,如下所示:

     public class InvoiceProfile: Profile
        {
            protected override void Configure()
            {
                Mapper.CreateMap<Invoice, InvoiceReportViewModel>()
                    .ForMember(c => c.CustomerName, op => op.MapFrom(v => v.CustomerName))
                    .ForMember(c => c.DiscountAmt, op => op.MapFrom(v => v.DiscountAmt))
                    .ForMember(c => c.InvoiceDate, op => op.MapFrom(v => v.InvoiceDate))
                    .ForMember(c => c.LineItems, op => op.MapFrom(v => v.InvoiceTransaction.TransactionItems));

                Mapper.CreateMap<TransactionDetail, InvoiceReportLineItemViewModel>()
                    .ForMember(c => c.ProductName, op => op.MapFrom(v => v.Product.ProductName))
                    .ForMember(c => c.Qty, op => op.MapFrom(v => v.Qty))
//and so on;
            }
        }

不要忘记注册&#34; InvoiceProfile&#34;