我正在使用 PosgreSQL 将数据返回到我的 Web API。
我创建了如下所示的 DTO 类:
public class SuppressionItem
{
public long AccountId { get; set; }
public Account Account { get; set; }
public CommunicationChannel Channel { get; set; }
public NotificationLogStatus Status { get; set; }
public string RecipientId { get; set; }
public string ContactId { get; set; }
public string Reason { get; set; }
public DateTime CreatedOn { get; set; }
}
在上面的DTO类中
CommunicationChannel
,NotificationLogStatus
代表一个enum
,如下所示:
public enum CommunicationChannel
{
Email = 0,
Sms = 1,
WhatsApp = 2,
[Description("Push Notification")]
Push = 3,
// Some more values were removed for brevity
}
public enum NotificationLogStatus
{
Created = 0,
Processing = 1,
Sent = 2,
Failed = 3,
Delivered = 4,
Undelivered = 5,
// Some more values were removed for brevity
}
我的
DndContact
类的定义如下。此类还包含两种方法,用于根据 switch case 语句返回其整数等效项的适当字符串。
[PrimaryKey(nameof(AccountId), nameof(Channel), nameof(RecipientId), nameof(ContactId), nameof(Status))]
public class DndContact
{
public long AccountId { get; set; }
public Account Account { get; set; }
public CommunicationChannel Channel { get; set; }
public NotificationLogStatus Status { get; set; }
public string RecipientId { get; set; }
public string ContactId { get; set; }
public string Reason { get; set; }
public DateTime CreatedOn { get; set; }
public string GetChannelName()
{
switch (Channel)
{
case CommunicationChannel.Email:
return "Email";
case CommunicationChannel.Sms:
return "Sms";
case CommunicationChannel.WhatsApp:
return "WhatsApp";
case CommunicationChannel.Push:
return "Push";
// Some more entries were removed for brevity
default:
return string.Empty;
}
}
public string GetStatusString()
{
switch (Status)
{
case NotificationLogStatus.Created:
return "Created";
case NotificationLogStatus.Processing:
return "Processing";
case NotificationLogStatus.Sent:
return "Sent";
case NotificationLogStatus.Failed:
return "Failed";
case NotificationLogStatus.Delivered:
return "Delivered";
// Some more entries were removed for brevity
default:
return string.Empty;
}
}
}
编写以下代码将
DndContact
数据转换为 SuppressionItem
数据。
public async Task<IEnumerable<SuppressionItem>> GetSuppressedItemsAsync()
{
var dndContacts = await _db.DndContacts
.Where(x => x.Status == NotificationLogStatus.Suppressed)
.ToListAsync();
var suppressionList = _mapper.Map<IEnumerable<SuppressionItem>>(dndContacts);
return suppressionList;
}
但是,我无法根据我的
Contact
类中编写的 switch 语句获取 Status
和 DndContact
字段的转换值。我不确定哪里出了问题。
在 Google 中搜索,但没有得到任何可以帮助我解决问题的结果。
如果我做错了或者遗漏了什么,有人可以建议我吗?
问候, 阿肖克·库马尔 K.
问题似乎是由于 AutoMapper 处理枚举映射的方式造成的。映射枚举时,AutoMapper 可能不会使用 GetChannelName 和 GetStatusString 函数。相反,它可能会尝试直接转换枚举值。
为了解决这个问题,您可以使用 AutoMapper 的 ConvertUsing 方法来定义枚举的转换方式。以下是将枚举映射添加到 AutoMapper 配置中的方法:
public class MappingProfile : Profile
{
public MappingProfile()
{
CreateMap<DndContact, SuppressionItem>()
.ForMember(dest => dest.Channel, opt => opt.MapFrom(src => src.GetChannelName()))
.ForMember(dest => dest.Status, opt => opt.MapFrom(src => src.GetStatusString()));
// Add other mappings as needed
}
}
ForMember 在此配置中用于指定 Channel 和 Status 属性的特定映射。这些属性的值由 GetChannelName 和 GetStatusString 方法提供。
检查您的应用程序中是否配置了 AutoMapper 配置文件,并且在映射期间是否正在使用它。
检查 DndContact 类中的 GetChannelName 和 GetStatusString 方法是否正常运行。如果您仍然遇到问题,请考虑添加一些日志记录或调试语句以观察这些方法在映射过程中返回的值。