2011-02-25 34 views
0

我想在solrnet中實現一個自定義的IReadOnlyMappingManager,以允許我們使用我們自己的Attribute類型來裝飾表示我們的solr索引記錄的文檔的屬性。由於我只需要更換GetFields和GetUniqueKey方法的執行,目前執行情況如下:在solrnet中實現一個自定義的IReadOnlyMappingManager

public class CustomMappingManager : AttributesMappingManager 
{   
    public new ICollection<KeyValuePair<PropertyInfo, string>> GetFields(Type type) 
    { 
     IEnumerable<KeyValuePair<PropertyInfo, IndexFieldAttribute[]>> mappedProperties = this.GetPropertiesWithAttribute<IndexFieldAttribute>(type); 

     IEnumerable<KeyValuePair<PropertyInfo, string>> fields = from mapping in mappedProperties 
                   select new KeyValuePair<PropertyInfo, string>(mapping.Key, mapping.Value[0].FieldName ?? mapping.Key.Name); 

     return new List<KeyValuePair<PropertyInfo, string>>(fields); 
    } 

    public new KeyValuePair<PropertyInfo, string> GetUniqueKey(Type type) 
    { 
     KeyValuePair<PropertyInfo, string> uniqueKey; 

     IEnumerable<KeyValuePair<PropertyInfo, IndexUniqueKeyAttribute[]>> mappedProperties = this.GetPropertiesWithAttribute<IndexUniqueKeyAttribute>(type); 

     IEnumerable<KeyValuePair<PropertyInfo, string>> fields = from mapping in mappedProperties 
                   select new KeyValuePair<PropertyInfo, string>(mapping.Key, mapping.Value[0].FieldName ?? mapping.Key.Name); 

     uniqueKey = fields.FirstOrDefault(); 

     return uniqueKey; 
    } 
} 

這種類型已經成功連接好使用structuremap和mappingManager在我ISolrOperations的具體實例是一個實例這個CustomMappingManager類型。

我已經將堆棧跟蹤直接下載到Solrnet實現中的Viistors中進行實際工作;這些具有CustomMappingManager實例。不幸的是,這種類型的GetFields和GetUniqueKey方法永遠不會被調用,我的文檔總是空的。

任何想法非常歡迎。

+0

出於好奇,爲什麼要使用自己的屬性?他們有沒有額外的信息? – 2011-02-25 12:33:09

+0

我認爲其目的是消除對SolrNet類型的依賴;它不賦予任何額外的功能。我從一位同事幾個月前做過的一些研究代碼中挑選出來,並且花了幾天的時間,現在我不相信有必要進行隔離。 – Jason 2011-02-25 14:47:45

回答

0

我解決了這個問題。這個問題的方法是錯誤的。以下是CustomMappingManager實現的等效工作代碼部分:

public class CustomMappingManager : IReadOnlyMappingManager 
{ 

public ICollection<SolrFieldModel> GetFields(Type type) 
    { 
     IEnumerable<KeyValuePair<PropertyInfo, IndexFieldAttribute[]>> mappedProperties = this.GetPropertiesWithAttribute<IndexFieldAttribute>(type); 

     IEnumerable<SolrFieldModel> fields = from mapping in mappedProperties 
              select new SolrFieldModel() 
              { 
               Property = mapping.Key, 
               FieldName = mapping.Value[0].FieldName ?? mapping.Key.Name 
              }; 

     return new List<SolrFieldModel>(fields); 
    } 

public SolrFieldModel GetUniqueKey(Type type) 
    { 
     SolrFieldModel uniqueKey; 

     IEnumerable<KeyValuePair<PropertyInfo, IndexUniqueKeyAttribute[]>> mappedProperties = this.GetPropertiesWithAttribute<IndexUniqueKeyAttribute>(type); 

     IEnumerable<SolrFieldModel> fields = from mapping in mappedProperties 
              select new SolrFieldModel() 
              { 
               Property = mapping.Key, 
               FieldName = mapping.Value[0].FieldName ?? mapping.Key.Name 
              }; 

     uniqueKey = fields.FirstOrDefault(); 

     if (uniqueKey == null) 
     { 
      throw new Exception("Index document has no unique key attribute"); 
     } 

     return uniqueKey; 
    } 
}