无论如何都要用automapper来压缩它吗?它刚刚变大。
Mapper.CreateMap<PagedResult<Headline>, PagedResult<HeadlineModel>>();
Mapper.CreateMap<PagedResult<Event>, PagedResult<EventModel>>();
Mapper.CreateMap<PagedResult<GymCourt>, PagedResult<GymCourtModel>>();
Mapper.CreateMap<PagedResult<Gym>, PagedResult<GymModel>>();
Mapper.CreateMap<PagedResult<EventGymCourt>, PagedResult<EventGymCourtModel>>();
Mapper.CreateMap<PagedResult<Division>, PagedResult<DivisionModel>>();
Mapper.CreateMap<PagedResult<Team>, PagedResult<TeamModel>>();
Mapper.CreateMap<PagedResult<DivisionTeam>, PagedResult<DivisionTeamModel>>();
Mapper.CreateMap<PagedResult<MemberTeam>, PagedResult<MemberTeamModel>>();
Mapper.CreateMap<PagedResult<TeamCoach>, PagedResult<TeamCoachModel>>();
Mapper.CreateMap<PagedResult<DivisionAsset>, PagedResult<DivisionAssetModel>>();
Mapper.CreateMap<PagedResult<EventAsset>, PagedResult<EventAssetModel>>();
Mapper.CreateMap<PagedResult<Event>, PagedResult<ApiEvent>>();
Mapper.CreateMap<PagedResult<Price>, PagedResult<PriceModel>>();
Mapper.CreateMap<PagedResult<DivisionPrice>, PagedResult<DivisionPriceModel>>();
Mapper.CreateMap<PagedResult<EventPrice>, PagedResult<EventPriceModel>>();
Mapper.CreateMap<PagedResult<Division>, PagedResult<ApiDivision>>();
Mapper.CreateMap<PagedResult<Data.Entities.Player>, PagedResult<PlayerModel>>();
答案 0 :(得分:12)
您可以做的是使用扩展方法。如果你添加这个类:
public static class MappingExtensions
{
public static IMappingExpression<TSrc, TDest> IncludePagedResultMapping<TSrc, TDest>(this IMappingExpression<TSrc, TDest> expression)
{
Mapper.CreateMap<PagedResult<TSrc>, PagedResult<TDest>>()
.ForMember(dest => dest.HasMoreResults, opt => opt.MapFrom(src => src.HasMoreResults))
.ForMember(dest => dest.NextPage, opt => opt.MapFrom(src => src.NextPage));
return expression;
}
}
然后您可以将分页映射作为实体的一部分包含在模型映射中,即:
Mapper.CreateMap<Headline, HeadlineModel>().IncludePagedResultMapping();
Mapper.CreateMap<Event, EventModel>().IncludePagedResultMapping();
Mapper.CreateMap<GymCourt, GymCourtModel>().IncludePagedResultMapping();
Mapper.CreateMap<Player, PlayerModel>().IncludePagedResultMapping();
答案 1 :(得分:1)
据我所知,Automapper中没有自定义映射器注册约定的支持,但是你可以通过一些反思将它干掉一点:
var mappingDictionary = new Dictionary<Type, Type>
{
{typeof (Headline), typeof (HeadlineModel)},
{typeof (Event), typeof (EventModel)},
//...
};
foreach (var sourceType in mappingDictionary.Keys)
{
Mapper.CreateMap(
typeof (PagedResult<>).MakeGenericType(sourceType),
typeof (PagedResult<>).MakeGenericType(mappingDictionary[sourceType]));
}
或者如果您始终遵循惯例Headline
- &gt; HeadlineModel
等。通过更多反射,您无需手动构建映射:
var modelAssembly = Assembly.GetAssembly(typeof(HeadlineModel));
var otherAssembly = Assembly.GetAssembly(typeof(Headline));
foreach (var destinationType in modelAssembly.GetTypes()
.Where(t => t.Namespace == "ModelNaspace" && t.Name.EndsWith("Model")))
{
var destinationName = destinationType.Name.Replace("Model", "");
var sourceType = otherAssembly.GetTypes()
.SingleOrDefault(t => t.Namespace == "OtherNamespace" && t.Name == destinationName);
if (sourceType == null)
{
//log warning
continue;
}
Mapper.CreateMap(
typeof (PagedResult<>).MakeGenericType(sourceType),
typeof (PagedResult<>).MakeGenericType(destinationType));
}
答案 2 :(得分:0)
我不敢。在某些时候,您必须告诉AutoMapper您的类映射。
如果您要整合Global.aspx(假设您在Asp.Net中使用AutoMapper),您可能需要尝试使用Bootstrapper。
有关Bootstrapper的更多信息:http://weblogs.asp.net/rashid/archive/2009/02/17/use-bootstrapper-in-your-asp-net-mvc-application-and-reduce-code-smell.aspx