2014-12-02 61 views
1

所以我有一個移動服務運行良好,並希望添加DTOs的符號。 我使用AutoMapper在我的模型和DTO模型之間進行映射,但是數據庫的複雜性和結果我迫使我使用自定義解析器,而這又會引發空異常。自動映射器自定義解析器拋出空異常

具體 - 映射器初始化

Mapper.Initialize(cfg => 
     { 
      cfg.CreateMap<Contact, MobileContact>() 
       .ForMember(mobcont => mobcont.Favorite, map => map.ResolveUsing<ContactResolver>());//.UseValue(true));// 
      cfg.CreateMap<MobileContact, Contact>(); 
     }); 

而且我自定義的解析器是

public class ContactResolver : ValueResolver<Contact, bool> 
{ 
    protected override bool ResolveCore(Contact a) 
    { 
     return true; 
    } 
} 

編輯:最喜歡的當然是

的布爾如果我不使用自定義解析器和使用.UseValue(true)它工作得很好

儘管如上圖所示用自定義解析器一個共同的GET請求拋出以下異常:

Exception=System.NullReferenceException: Object reference not set to an instance of an object. 
    at AutoMapper.QueryableExtensions.Extensions.ResolveExpression(PropertyMap propertyMap, Type currentType, Expression instanceParameter) 
    at AutoMapper.QueryableExtensions.Extensions.CreateMemberBindings(IMappingEngine mappingEngine, TypePair typePair, TypeMap typeMap, Expression instanceParameter, IDictionary`2 typePairCount) 
    at AutoMapper.QueryableExtensions.Extensions.CreateMapExpression(IMappingEngine mappingEngine, TypePair typePair, Expression instanceParameter, IDictionary`2 typePairCount) 
    at AutoMapper.QueryableExtensions.Extensions.CreateMapExpression(IMappingEngine mappingEngine, TypePair typePair, IDictionary`2 typePairCount) 
    at AutoMapper.QueryableExtensions.Extensions.<>c__DisplayClass1`2.<CreateMapExpression>b__0(TypePair tp) 
    at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory) 
    at AutoMapper.Internal.DictionaryFactoryOverride.ConcurrentDictionaryImpl`2.GetOrAdd(TKey key, Func`2 valueFactory) 
    at AutoMapper.QueryableExtensions.Extensions.CreateMapExpression[TSource,TDestination](IMappingEngine mappingEngine) 
    at AutoMapper.QueryableExtensions.ProjectionExpression`1.To[TResult]() 
    at Microsoft.WindowsAzure.Mobile.Service.MappedEntityDomainManager`2.Query() 
    at azmobtestService.Models.MobileContactDomainManager.Query() in c:\Users\n.atlas\Source\Repos\JPhoneBook\Service\azmobtestService\Models\MobileContactDomainManager.cs:line 104 
    at Microsoft.WindowsAzure.Mobile.Service.TableController`1.Query(), Id=75849f58-ccb0-4a6b-8a77-491f13fcb717, Category='App.Controllers.Tables' 

回答

0

如果你正在做的是在任何情況下解決的喜愛,真正的(這是不正確的,但是這是代碼你提供的,所以我用它去),那麼你的映射器可能只是.ForMember(mobcont => mobcont.Favorite, expression => expression.MapFrom(source => true));

如果您正在尋找正常的值解析器,我會用ITypeConverter是這樣的:

public class ContactToMobileContactTypeConverter : ITypeConverter<Contact, MobileContact> 
{ 
    public MobileContact Convert(ResolutionContext context) 
    { 
     var contact = (Contact)context.SourceValue; 

     var mobileContact = new MobileContact(); 
     if(contact != null) { 
      //database query 
      //assign values 
     } 
     return mobileContact; 
    } 
} 
+0

我用AutoMapper來映射een我的模型和DTOs模型,但數據庫的複雜性和我想要的結果迫使我使用自定義解析器,這反過來拋出空異常。 Ofcourse我不想把它設置爲true! 我想創建一個數據庫請求並搜索Favorites表中的一行並將其設置爲該值。正如您所看到的,此搜索基於用戶身份驗證(每個用戶每次聯繫我擁有多行最愛)。 – Nikatlas 2014-12-02 13:19:20

+0

我編輯了一些應該可以工作的代碼,但我相信你必須做相當多的工作才能讓它按照你想要的方式工作。 – 2014-12-02 13:30:40

+0

將配置更改爲: cfg.CreateMap ()。ConvertUsing(new ContactToMobileContactTypeConverter()); cfg.CreateMap (); 並添加了您提供的代碼。 調試顯示轉換方法從未執行! 新的日誌是: LINQ to Entities不支持指定的類型成員'Id'。只支持初始化器,實體成員和實體導航屬性。更多更多 – Nikatlas 2014-12-02 13:48:08