2017-04-24 100 views
0

我有一個表的消息:自動映射一個表實體兩個DTO子

CREATE TABLE [Communication].[Message] 
(
    [MessageID] BIGINT NOT NULL PRIMARY KEY IDENTITY, 
    [MessageChannelID] INT NOT NULL, 
    [MessageStatusID] INT NOT NULL, 
    [Sender] NVARCHAR(100) NOT NULL, 
    [SenderDisplayName] NVARCHAR(100) NULL, 
    [Receiver] NVARCHAR(100) NOT NULL, 
    [Content] NVARCHAR(MAX) NULL, 
    [EmailUseSSL] BIT NOT NULL DEFAULT 0, 
    [EmailReceiverCC] NVARCHAR(100) NULL, 
    [EmailReceiverBCC] NVARCHAR(100) NULL, 
    [EmailSenderReplyTo] NVARCHAR(100) NULL, 
    [EmailSubject] NVARCHAR(300) NULL, 
    [SMSGUID] NVARCHAR(100) NULL, 
    [CreatedAt] DATETIME NOT NULL DEFAULT getDate(), 
    [Note] NVARCHAR(300) NULL, 
    CONSTRAINT [FK_Message_MessageStatus] FOREIGN KEY ([MessageStatusID]) REFERENCES [Communication].[MessageStatus]([MessageStatusID]), 
    CONSTRAINT [FK_Message_MessageChannel] FOREIGN KEY ([MessageChannelID]) REFERENCES [Communication].[MessageChannel]([MessageChannelID]) 
) 

而且我有DTO類:

public class MessageDTO 
{ 
    public int MessageID { get; set; } 
    public string Sender { get; set; } 
    public string SenderDisplayName { get; set; } 
    public string Receiver { get; set; } 
    public string Content { get; set; } 
    public DateTime CreatedAt { get; internal set; } 
    public string Note { get; set; } 
    public MessageStatus Status { get; set; } 
    public MessageChannel Channel { get; internal set; } 
} 

public class EmailMessageDTO: MessageDTO 
{ 
    public bool EmailUseSSL { get; set; } 
    public string EmailReceiverCC { get; set; } 
    public string EmailReceiverBCC { get; set; } 
    public string EmailSenderReplyTo { get; set; } 
    public string EmailSubject { get; set; } 
} 

public class SMSMessageDTO: MessageDTO 
{ 
    public string SMSGUID { get; set; } 
} 

消息類型可以從MessageChannelID(電子郵件,SMS)來解決。是否有可能使用Automapper來獲取具有特定狀態的所有消息,以便將某些消息映射到EmailMessageDTO,並將其他消息映射到SMSMessageDTO?我想編寫一個GetMessages方法,它根據消息類型返回所有​​消息,包含對象EmailMessageDTO或SMSMessageDTO。

+0

你有沒有嘗試過這樣的:http://stackoverflow.com/questions/9746650/automapper-conditional-mapping-for-type - 實質上是使用'ConstructUsing'函數和映射到基類型 - 任何映射都將通過'ConstructUsing'創建它們的實例。唯一的問題可能是特定的映射選項可能不可行,我不確定AutoMapper是否會意識到它不再是'dto - > base'映射並使用更具體的映射配置。 – Charleh

+0

我看到了這個解決方案,它幫助我解決了這個問題! – vpetrovic

回答

1

Charleh的評論後,我想出了這個:

x.CreateMap<Message, MessageDTO>() 
        .ForMember(m => m.Status, opts => opts.MapFrom(src => src.MessageStatusID)) 
        .ForMember(m => m.Channel, opts => opts.MapFrom(src => src.MessageChannelID)) 
        .ConstructUsing(m => 
        { 
         if (m.MessageChannelID == (int)Data.MessageChannel.Email) 
          return new EmailMessageDTO 
          { 
           EmailReceiverBCC = m.EmailReceiverBCC, 
           EmailReceiverCC = m.EmailReceiverCC, 
           EmailSenderReplyTo = m.EmailSenderReplyTo, 
           EmailSubject = m.EmailSubject, 
           EmailUseSSL = m.EmailUseSSL 
          }; 
         else return new SMSMessageDTO 
         { 
          SMSGUID = m.SMSGUID 
         }; 
        }); 
+0

酷,它仍然有點手動,但至少它包含在映射配置,所以你不必考慮它:) – Charleh

相關問題