我正在尝试使用自定义值解析程序将Assignees列表的映射从Entity类委托给服务类。下面是我进行映射的课程
using AutoMapper;
using WebApi2Book.Common.TypeMapping;
using WebApi2Book.Data.Entities;
namespace WebApi2Book.Web.Api.AutoMappingConfiguration
{
public class TaskEntityToTaskAutoMapperTypeConfigurator : IAutoMapperTypeConfigurator
{
public void Configure()
{
Mapper.Initialize(cfg => {
cfg.CreateMap<Task, Models.Task>()
.ForMember(opt => opt.Links, x => x.Ignore())
.ForMember(opt => opt.Assignees,
x => x.ResolveUsing<TaskAssigneesResolver1>());
});
}
}
}
Configure()方法是从IAutoMapperTypeConfigurator接口
实现的CustomResolver
using System.Collections.Generic;
using System.Linq;
using AutoMapper;
using WebApi2Book.Common.TypeMapping;
using WebApi2Book.Data.Entities;
using WebApi2Book.Web.Common;
using User = WebApi2Book.Web.Api.Models.User;
using Task = WebApi2Book.Data.Entities.Task;
namespace WebApi2Book.Web.Api.AutoMappingConfiguration
{
public class TaskAssigneesResolver : IValueResolver<Task, List<User>>//Task, List<User>, List<User>>
{
public IAutoMapper AutoMapper
{
get { return WebContainerManager.Get<IAutoMapper>(); }
}
// protected override List<User> ResolveCore(Task source)
// {
// return source.Users.Select(x => AutoMapper.Map<User>(x)).ToList();
// }
public List<User> Resolve(Task source)
{
return source.Users.Select(x => AutoMapper.Map<User>(x)).ToList();
}
}
Models.Task类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WebApi2Book.Web.Api.Models
{
public class Task
{
private List<Link> _links;
public long? TaskId { get; set; }
public string Subject { get; set; }
public DateTime? StartDate { get; set; }
public DateTime? DueDate { get; set; }
public DateTime? CreatedDate { get; set; }
public DateTime? CompletedDate { get; set; }
public Status Status { get; set; }
public List<User> Assignees { get; set; }
public List<Link> Links
{
get { return _links ?? (_links = new List<Link>()); }
set { _links = value; }
}
public void AddLink(Link link)
{
Links.Add(link);
}
}
}
Entities.Task class
using System;
using System.Collections.Generic;
namespace WebApi2Book.Data.Entities
{
public class Task : IVersionedEntity
{
private readonly IList<User> _users = new List<User>();
public virtual long TaskId { get; set; }
public virtual string Subject { get; set; }
public virtual DateTime? StartDate { get; set; }
public virtual DateTime? DueDate { get; set; }
public virtual DateTime? CompletedDate { get; set; }
public virtual Status Status { get; set; }
public virtual DateTime CreatedDate { get; set; }
public virtual User CreatedBy { get; set; }
public virtual IList<User> Users
{
get { return _users; }
}
public virtual byte[] Version { get; set; }
}
}
这应该是一个相当简单的映射,它忽略链接,因为它们不存在于源中,并且它将Assignees映射委派给解析器。
错误:
类型&#39; TaskAssigneesResolver&#39;不能用作类型参数&#39; TValueResolver&#39;在通用类型或方法'AutoMapper.IMemberConfigurationExpression<TSource,TDestination,TMember>.ResolveUsing<TValueResolver>()'
中。
从'TaskAssigneesResolver'
到'AutoMapper.IValueResolver<Data.Models.Task, Entities.Task, object>'
没有隐式引用转换。
我怀疑我误解了IValueResolver接口。请给我可能的解决方案: