I have some problems with AutoMapper. Can someone explain for me how i should create map? Don't be afraid a lot of code blocks pls. It's very simple logic i just can't understand how mapper work... thank you for help. AutoMapperConfig:
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; }
}
My ApiInputSubscription model :
[Serializable]
public class ApiInputSubscription
{
public string SubscriptionName { get; set; }
public ApiSearchRequest SearchRequest { get; set; }
}
My Subscription model :
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;
}
}
SearchRequest Class:
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; }
}
You need to tell AutoMapper firstly how to map ApiSearchRequest to SearchRequest and secondly to ignore all the other properties:
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());
}
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.