自动映射:如何映射有条件的类型?

时间:2010-07-23 00:50:17

标签: c# model-view-controller automapper

问候大师,

所以基本上我有一个我设置用于TripFeeSchedule的地图,旅行费用表是IEnumerable类型。我试图映射这个,所以我没有5个左右的ValueResolvers,我被卡住了。

我基本上想要做的是从单一费用表中映射一种OPtional费用。因此,例如,如果类型是滞纳金,我想映射该特定类型,其中价格和费用被适当地检索和显示。因为这些费用并不总是存在,所以我试图尽可能简化这些费用。

Mapper.CreateMap<Trip, TripManagementViewModelCreateEdit>().ForMember(dto => dto.TripOverride, opt => opt.ResolveUsing<TripCompResolver>().FromMember(x => x.TripComp))
.ForMember(dto => dto.BusOverride, opt => opt.ResolveUsing<TripCompResolver>().FromMember(x => x.TransportComp)).ForMember(dto => dto.RentalSki, opt => opt.MapFrom<TripFeeSchedule>(x=> x.ScheduledFees.First()) );
对象Not Refrenced表示,预定费用已经消失了。

我有地图,它似乎正在工作(调试器显示它被调用)

[NullReferenceException:对象引用未设置为对象的实例。]    AutoMapper.Mappers.PropertyMapMappingStrategy.MapPropertyValue(ResolutionContext context,IMappingEngineRunner mapper,Object mappedObject,PropertyMap propertyMap)+393    AutoMapper.Mappers.PropertyMapMappingStrategy.Map(ResolutionContext context,IMappingEngineRunner mapper)+309    AutoMapper.Mappers.TypeMapMapper.Map(ResolutionContext context,IMappingEngineRunner mapper)+221    AutoMapper.MappingEngine.AutoMapper.IMappingEngineRunner.Map(ResolutionContext context)+723

[AutoMapperMappingException: Trying to map FreeLivinEnertainment.Core.TripFeeSchedule to FreeLivinEnertainment.Core.ViewModels.OptionalFee.
Using mapping configuration for FreeLivinEnertainment.Core.TripFeeSchedule to FreeLivinEnertainment.Core.ViewModels.OptionalFee
Destination property: RentalSki
Exception of type 'AutoMapper.AutoMapperMappingException' was thrown.]
   AutoMapper.MappingEngine.AutoMapper.IMappingEngineRunner.Map(ResolutionContext context) +811
   AutoMapper.Mappers.PropertyMapMappingStrategy.MapPropertyValue(ResolutionContext context, IMappingEngineRunner mapper, Object mappedObject, PropertyMap propertyMap) +601

[AutoMapperMappingException: Trying to map FreeLivinEnertainment.Core.TripFeeSchedule to FreeLivinEnertainment.Core.ViewModels.OptionalFee.
Using mapping configuration for FreeLivinEnertainment.Core.TripFeeSchedule to FreeLivinEnertainment.Core.ViewModels.OptionalFee
Destination property: RentalSki
Exception of type 'AutoMapper.AutoMapperMappingException' was thrown.]
   AutoMapper.Mappers.PropertyMapMappingStrategy.MapPropertyValue(ResolutionContext context, IMappingEngineRunner mapper, Object mappedObject, PropertyMap propertyMap) +699
   AutoMapper.Mappers.PropertyMapMappingStrategy.Map(ResolutionContext context, IMappingEngineRunner mapper) +309
   AutoMapper.Mappers.TypeMapMapper.Map(ResolutionContext context, IMappingEngineRunner mapper) +221
   AutoMapper.MappingEngine.AutoMapper.IMappingEngineRunner.Map(ResolutionContext context) +723

[AutoMapperMappingException: Trying to map FreeLivinEnertainment.Core.Trip to FreeLivinEnertainment.Core.ViewModels.TripManagementViewModelCreateEdit.
Using mapping configuration for FreeLivinEnertainment.Core.Trip to FreeLivinEnertainment.Core.ViewModels.TripManagementViewModelCreateEdit
Exception of type 'AutoMapper.AutoMapperMappingException' was thrown.]
   AutoMapper.MappingEngine.AutoMapper.IMappingEngineRunner.Map(ResolutionContext context) +811
   AutoMapper.MappingEngine.Map(Object source, Type sourceType, Type destinationType) +132
   AutoMapper.MappingEngine.Map(TSource source) +150
   AutoMapper.Mapper.Map(TSource source) +117
   FreeLivinEnertainment.Web.Controllers.Admin.TripsController.ShowEditTrip(Int32 id) in D:\Projects\jimbo\app\FreeLivinEnertainment.Web.Controllers\Controllers\Admin\TripManagementController.cs:138
   lambda_method(ExecutionScope , ControllerBase , Object[] ) +79
   System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +17
   System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +178
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +24
   System.Web.Mvc.<>c__DisplayClassd.<InvokeActionMethodWithFilters>b__a() +52
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +254
   System.Web.Mvc.<>c__DisplayClassf.<InvokeActionMethodWithFilters>b__c() +19
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +254
   System.Web.Mvc.<>c__DisplayClassf.<InvokeActionMethodWithFilters>b__c() +19
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +192
   System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +314
   System.Web.Mvc.Controller.ExecuteCore() +105
   System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +39
   System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +7
   System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__4() +34
   System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +21
   System.Web.Mvc.Async.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) +12
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +59
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +44
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +7
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8679150
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155

1 个答案:

答案 0 :(得分:0)

弄清楚如何做到这一点,我觉得自己像个白痴,我想我可以在From Member上运行lambda,然后我可以做我需要的结果。

opt => opt.ResolveUsing().FromMember(x => x.ScheduledFees.First()