简体   繁体   中英

Retrieving generic data without knowing the List<Type> to be returned, is that possible?

I dont know if the question is correct but what I need to do is to received the correct entity from the messagetoprocess repository method with the corresponding data, how can I do that in this scenario? (code below)

Im using AutoMapper.

I know that I can create a MessageEntity and eliminate the Interface and put all properties together in it but that is exactly what Im trying not to do.

Here is what I've got:

Interface:

public interface IMessage
{
    string MessageFrom { get; set; }
    string MessageTo { get; set; }
{

Implementer Entities

public class EmailMessageEntity : IMessage
{
    public bool IsMessageBodyHtml { get; set; }
}

public class SmsMessageEntity : IMessage
{
    public bool IsMmsMessage { get; set; }
}

Models:

public class EmailMessage
{
    public string MessageFrom { get; set; }
    public string MessageTo { get; set; }    
    public bool IsMessageBodyHtml { get; set; }
}
public class SMSMessage
{
    public string MessageFrom { get; set; }
    public string MessageTo { get; set; }
    public bool IsMmsMessage { get; set; }
}

Repositry:

 public static List<*****Entity problem*****> RetrieveMessageToProcess()
 {
     var commandSettings = new CommandSettings
     {
         CommandText = @"[Schema].[RetrieveMessageToProcess]",
         CommandType = CommandType.StoredProcedure
     };

     return new MsSqlProviderBase(DbConnectionString, commandSettings).ExecuteQuery<*****Entity problem*****>();
  }

Using it:

//code excerpt

var messagesToProcess = Db.RetrieveMessageToProcess(); //repository

if (messagesToProcess == null) return;

// Process Message(s)
foreach (var messageEntity in messagesToProcess)
{
    if (Email) // this is just the verification example not the actual statement and not a variable
    {
        Mapper.CreateMap<EmailMessageEntity, EmailMessage>();
        var emailMessage = Mapper.Map<EmailMessage>(messageEntity);
    }
    else if (SMS) 
    {
        Mapper.CreateMap<SMSMessageEntity, SMSMessage>();
        var smsMessage = Mapper.Map<SmsMessage>(messageEntity);
    }
 }

Could you consider having a MessageType member on your IMessage interface instead of the two IsXXX properties ?

You could return an enum, or whatever other value you deem appropriate, and use that in your if(EMAIL) statement.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM