Search code examples
dynamicreflectionravendbinvokeout

Dynamically invoking RavenDB Statistics method regardless of query source


RavenDB provides 2 APIs for querying data, IDocumentQuery<T> for advanced lucene query and IRavenQueryable<T> for a strongly typed linq provider model.

They share a method called Statistics(out RavenQueryStatistics stats) that returns information at run time.

The 2 different method signatures are:

  public interface IRavenQueryable<T> ....
  {
    IRavenQueryable<T> Statistics(out RavenQueryStatistics stats);

and

 public interface IDocumentQueryBase<T, out TSelf> 
                          where TSelf : IDocumentQueryBase<T, TSelf>
  {
    TSelf Statistics(out RavenQueryStatistics stats);

I'm starting off with

    protected override dynamic SetupQuery(IDocumentSession session)
    {
        return session.Advanced.LuceneQuery<Foo>(new FooIndex().IndexName)
               .WhereEquals("Bar", "Baz")
               ;            
    }

The intent is that this method is able to be replaced by other classes.

I am attempting to consume this as such:

using (var session = Store.OpenSession())
{
    RavenQueryStatistics stats=null;
    var dynQuery=    SetupQuery(session);
    var dynQuery2 = dynQuery.Statistics(out stats);
    //Results in cannot cast void to object

    //next i tried using reflection
    var refQuery=    SetupQuery(session);
    MethodInfo methodInfo = refQuery.GetType()
            .GetMethod("Statistics", BindingFlags.Public, null, 
            new[] {typeof (RavenQueryStatistics).MakeByRefType()}, null);


    var refQuery2= methodInfo.Invoke(query, new[] {stats});
    //results in NullReferenceException

Solution

  • You are looking at the interface definitions, but as it turns out, the dynamic calls end up calling AbstractDocumentQuery.Statistics, which returns void. The method that returns an instance is actually implemented as explicit interface method, so the dynamic call can't call it.