chriseldredge / Lucene.Net.Linq

LINQ provider to run native queries on a Lucene.Net index
Other
151 stars 66 forks source link

Allow custom type converter with collections #89

Open khalidsalomao opened 9 years ago

khalidsalomao commented 9 years ago

Only use CollectionReflectionFieldMapper if no custom type converter was provided. Since Generic collections are not implemented, this will allow the user to provide his custom converter.

Example:

class sample
{
    public List<string> List { get; set; }
    public Dictionary<string, string> Dic { get; set; }
}

var map = new ClassMap<sample>(Lucene.Net.Util.Version.LUCENE_30);
map.Property(i => i.List).ConvertWith(new CustomListConverter());
map.Property(i => i.Dic).ConvertWith(new CustomDictionaryConverter());
chriseldredge commented 9 years ago

Some tests would be good here. Does this address #32?

khalidsalomao commented 9 years ago

Yes! This is a partial solution for #32.

By partial I mean that the user must provide the TypeConverter, but we will have some support for collections. It is a start!

This weekend, I will add some tests!

khalidsalomao commented 9 years ago

There is a problem with my approach (as seen in the failed test). I was expecting to apply the type converter to the collection instance, but the mapper apply the type converter to each item of the collection.

CollectionReflectionFieldMapper.CopyFromDocument

@chriseldredge, what do you think about adding another attribute AsPrimitive or IsCollection or StoreAsCollection to manage this behavior?

This option would keep the current behavior as default, but would allow a new way of dealing with collections (treat the whole collection instance). This new type conversion behavior is the partial solution for #32.

The use case of this new behavior is to allow Lucene.Net.Linq to store an object that has such collections. It is not perfect, since it won't be very helpful for indexing but at least such objects can be stored... (It is a partial support)

Next week, I will take a look and try submit a new pull request with the full solution for #32. What do you think?

chriseldredge commented 8 years ago

I think the extension point should be in CollectionReflectionFieldMapper.

The values.ToArray(elementType) is what needs to be extensible. Perhaps having the option (with attributes or fluent syntax) to specify an alternate helper method that creates an appropriate collection type.

The default converter implementation could also be smarter and try to create an appropriate instance for the property type, supporting commonly used collections like T[], IList<T>, ISet<T>, etc.