簡體   English   中英

自動映射配置復雜映射

[英]Automapper configuration complex mappings

我一直在使用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方法中轉換它?

提前致謝

在您的情況下,我沒有看到使用任何自定義轉換器的任何要求。 您可以使用簡單的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;
            }
        }

別忘了注冊“InvoiceProfile”

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM