2010-07-23 93 views
0

問候大師,Automapper:你如何映射一個有條件的類型?

所以基本上我有一個地圖,我設置爲TripFeeSchedule使用,旅行費用表是IEnumerable類型。我試圖將其映射出來,所以我不必擁有5個左右的ValueResolver,並且我被卡住了。

我基本上想要做的是從單一費用計劃中映射一種可選費用。因此,舉例來說,如果類型是滯納金,我想將特定類型與價格和費用進行匹配並正確顯示。由於這些費用並不總是存在,我正試圖儘可能簡化。

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())); 

ScheduledFees死在我身上,說Object Not Refrenced。

我有地圖和它似乎是工作(調試器顯示,其被調用)

[NullReferenceException異常:對象引用不設置爲一個對象的一個​​實例] AutoMapper.Mappers.PropertyMapMappingStrategy.MapPropertyValue( ResolutionContext上下文,IMappingEngineRunner映射器,對象mappedObject,屬性映射屬性映射)393 AutoMapper.Mappers.PropertyMapMappingStrategy.Map(ResolutionContext上下文,IMappingEngineRunner映射器)309 AutoMapper.Mappers.TypeMapMapper.Map(ResolutionContext上下文,IMappingEngineRunner映射器)221 AutoMapper .MappingEngine.AutoMapper.IMappingEngineRunner.Map(ResolutionContext上下文) 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 

回答

0

想出如何做到這一點,我覺得自己像一個白癡,我想我可以只運行在拉姆達從會員,然後我可以做什麼,我需要得到的結果回來。

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