简体   繁体   English

在Solrnet中实现自定义IReadOnlyMappingManager

[英]Implementing a custom IReadOnlyMappingManager in solrnet

I am trying to implement a custom IReadOnlyMappingManager in solrnet to allow us to use our own Attribute types for decorating the properties of the documents that represent our solr index records. 我正在尝试在solrnet中实现自定义IReadOnlyMappingManager,以允许我们使用自己的Attribute类型来修饰表示solr索引记录的文档的属性。 As I only need to replace the implementation of the GetFields and GetUniqueKey methods, the current implementation is as follows: 因为我只需要替换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;
    }
}

This type has been successfully wired up using structuremap and the mappingManager in my concrete instance of ISolrOperations is an instance of this CustomMappingManager type. 在我的ISolrOperations具体实例中,已经使用structuremap成功连接了该类型,并且mappingManager是此CustomMappingManager类型的实例。

I have followed the stack trace right down to the Viistors in the solrnet implementation that do the real work; 我一直遵循堆栈跟踪,一直到进行实际工作的solrnet实现中的Viistors。 these have the CustomMappingManager instance as intended. 这些具有所需的CustomMappingManager实例。 Unfortunately, the GetFields and GetUniqueKey methods on this type never get called and my documents are always empty. 不幸的是,这种类型的GetFields和GetUniqueKey方法永远不会被调用,而且我的文档总是空的。

Any ideas very welcome. 任何想法都非常欢迎。

I have resolved this. 我已经解决了。 The approach in the question was wrong way to go about it. 问题中的方法是错误的解决方法。 Here is the equivalent portion of the working code for the CustomMappingManager implementation: 这是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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM