Automatic and general matching

Anyway, condense it with automapper? It just gets big.

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>>(); 
+4
source share
3 answers

What you can do is use the extension method. If you add this class:

 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; } } 

then you can enable paged mappings as part of your object for modeling mappings, namely:

 Mapper.CreateMap<Headline, HeadlineModel>().IncludePagedResultMapping(); Mapper.CreateMap<Event, EventModel>().IncludePagedResultMapping(); Mapper.CreateMap<GymCourt, GymCourtModel>().IncludePagedResultMapping(); Mapper.CreateMap<Player, PlayerModel>().IncludePagedResultMapping(); 
+11
source

As far as I know, in Automapper there is no built-in support for user agreements on registering a cartographer, but you can dry it a little with some reflection:

 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])); } 

Or, if you always follow your agreement Headline HeadlineModel , etc. With one more thought, you do not need to create a mapping manually:

 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)); } 
+1
source

I'm afraid not. At some point, you will need to tell AutoMapper about your class mappings.

If you want to consolidate your Global.aspx (assuming you use AutoMapper in Asp.Net), you can try Bootstrapper.

More on Bootstrapper: http://weblogs.asp.net/rashid/archive/2009/02/17/use-bootstrapper-in-your-asp-net-mvc-application-and-reduce-code-smell.aspx

0
source

Source: https://habr.com/ru/post/1389985/


All Articles