How to register a ISolrFieldSerializer in Windsor

2019-09-01 03:57发布

问题:

I am trying to get an enum to serialize to it's int value when posting to Solr.

So I have implemented a ISolrFieldSerializer to do this, As suggested here. But I can seem to register it within the Windsor container in a way that it then gets used by SolrNet

Here is what I have:

This works fine apart from the serializer does not get used, although it appears in the containers components list. Any ideas?

    container.Register(Component.For<ISolrFieldSerializer>().ImplementedBy<SolrEnumSerializer>());
    Startup.Init<SearchBox>("http://10.10.10.10:0000/solr/boxes");

    container.Register(Component.For<ISolrOperations<SearchBox>>()
                          .UsingFactoryMethod(k => ServiceLocator.Current.GetInstance<ISolrOperations<SearchBox>>()));

回答1:

I sorted this by removing the default implementation and replace it with a custom one:

Startup.Container.Remove<ISolrFieldSerializer>();

var fieldSerializer = new CustomSerializer();
Startup.Container.Register<ISolrFieldSerializer>(c => fieldSerializer);

Custom Sertializer:

public class CustomSerializer : ISolrFieldSerializer 
    {
        private readonly AggregateFieldSerializer _serializer;

        public CustomSerializer()
        {
            _serializer = new AggregateFieldSerializer(new ISolrFieldSerializer[]
                                                          {
                                                              new MyCustom1Serializer(),
                                                              new MyCustom2Serializer(),
                                                              new CollectionFieldSerializer(this),
                                                              new GenericDictionaryFieldSerializer(this),
                                                              new NullableFieldSerializer(new BoolFieldSerializer()),
                                                              new NullableFieldSerializer(new DateTimeFieldSerializer()),
                                                              //new MoneyFieldSerializer(),
                                                              new FormattableFieldSerializer(),
                                                              new TypeConvertingFieldSerializer(),


                                                          });
        }

        public  bool CanHandleType(Type t)
        {
            return _serializer.CanHandleType(t);
        }

        public IEnumerable<PropertyNode> Serialize(object obj)
        {
            return _serializer.Serialize(obj);
        }
    }