我正在尝试在solrnet中实现自定义IReadOnlyMappingManager,以允许我们使用自己的Attribute类型来装饰代表我们的solr索引记录的文档的属性。由于我只需要替换GetFields和GetUniqueKey方法的实现,当前的实现如下:
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成功连接,并且我在ISolrOperations的具体实例中的mappingManager是此CustomMappingManager类型的实例。
我已经跟踪堆栈跟踪,直到执行实际工作的solrnet实现中的Viistors;这些具有预期的CustomMappingManager实例。不幸的是,这种类型的GetFields和GetUniqueKey方法永远不会被调用,我的文档总是空的。
任何想法都非常受欢迎。
答案 0 :(得分: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;
}
}