c# - 如何使用 AutoMapper 映射我的复杂类型?
问题描述
我对 AutoMapper 有一些问题。有人可以为我解释我应该如何创建地图吗?不要害怕很多代码块。这是非常简单的逻辑,我只是无法理解映射器是如何工作的......谢谢你的帮助。自动映射器配置:
private void MappingApiInputSubscription(IMapperConfigurationExpression expression)
{
expression.CreateMap<ApiInputSubscription, Subscription>()
.ForMember(
dest => dest.SearchRequest,
opt => opt.MapFrom(src =>src.SearchRequest));
}
ApiSearchRequest:
public class ApiSearchRequest
{
public string Text { get; set; }
public int? CategoryId { get; set; }
public bool PriceRequired { get; set; }
public int? MinPrice { get; set; }
public int? MaxPrice { get; set; }
public int? CurrencyId { get; set; }
public DateTime? PublishDateFrom { get; set; }
public DateTime? PublishDateTo { get; set; }
public DateTime? EventDateFrom { get; set; }
public DateTime? EventDateTo { get; set; }
public bool PhotoAttached { get; set; }
public int? OwnerId { get; set; }
public ICollection<int> Offices { get; set; }
}
我的 ApiInputSubscription 模型:
[Serializable]
public class ApiInputSubscription
{
public string SubscriptionName { get; set; }
public ApiSearchRequest SearchRequest { get; set; }
}
我的订阅模型:
public class Subscription : ISubscription
{
public int Id { get; set; }
public int OwnerId { get; set; }
public string SubscriptionName { get; set; }
public SearchRequest SearchRequest { get; set; }
public ISubscription WithOwner(IUser user)
{
user = user ?? throw new ArgumentNullException(nameof(user), "subscription owner can't be null!");
var clone = (Subscription)MemberwiseClone();
clone.OwnerId = user.Id;
return clone;
}
}
搜索请求类:
public class SearchRequest : ISearchRequest
{
public string Text { get; set; }
public int? CategoryId { get; set; }
public bool PriceRequired { get; set; }
public int? MinPrice { get; set; }
public int? MaxPrice { get; set; }
public int? CurrencyId { get; set; }
public DateTime? PublishDateFrom { get; set; }
public DateTime? PublishDateTo { get; set; }
public bool EventDateRequired { get; set; }
public DateTime? EventDateFrom { get; set; }
public DateTime? EventDateTo { get; set; }
public bool PhotoAttached { get; set; }
public int PostsOnPage { get; set; } = 10;
public PostStatus Status { get; set; }
public SortPostsBy SortPostsOrder { get; set; }
public bool OnlyInappropriate { get; set; }
public int? OwnerId { get; set; }
public int Skip { get; set; }
public ICollection<int> Offices { get; set; }
}
解决方案
您首先需要告诉 AutoMapper 如何将 ApiSearchRequest 映射到 SearchRequest,其次要忽略所有其他属性:
private void MappingApiInputSubscription(IMapperConfigurationExpression expression)
{
expression.CreateMap<ApiSearchRequest, SearchRequest>()
// add other properties as required
.ForMember(dest => dest.PostsOnPage, opt => opt.Ignore());
expression.CreateMap<ApiInputSubscription, Subscription>()
.ForMember(
dest => dest.SearchRequest,
opt => opt.MapFrom(src => src.SearchRequest))
.ForAllOtherMembers(dest => dest.Ignore());
}
推荐阅读
- bamboo - Http 到 https 重定向问题
- tsql - 如何每周对 MTD 和 YTD 值的数据进行分组
- mysql - 从 Oracle 从 MySQL 数据库中检索数据时出错
- python - Discord.py 多词参数,另一种选择?
- javascript - 循环 JSON,在尝试对 JS 对象进行字符串化时
- python - 带按钮的调光 LED
- python - Keras,我们是否可以将其设置为一直拟合模型,直到损失函数大于一个值或几乎恒定?
- html - 如何为上一个按钮编写jquery函数?
- java - 使用改造将不一致的 JSON 解析为 POJO
- android - 如何修复“npx react-native run-android”错误?