Skip to main content

RVN001 - Index Map or Reduce assigned outside constructor

Description​

The RavenDB client builds the index definition from the Map and Reduce expressions when the index is deployed.
If these expressions are assigned in a separate method, that method may not run before the definition is built,
for example if it is called conditionally or not at all.

For single-map document indexes, the client throws InvalidOperationException if Map is still unset when the definition is built. If Reduce is unset, the resulting definition is map-only.

Trigger condition​

RVN001 checks index classes derived from the following bases:

  • AbstractIndexCreationTask<TDocument>, AbstractIndexCreationTask<TDocument, TReduceResult>, or AbstractGenericIndexCreationTask<TReduceResult>.
  • AbstractMultiMapIndexCreationTask, AbstractMultiMapTimeSeriesIndexCreationTask, or AbstractMultiMapCountersIndexCreationTask, including their generic variants.

Abstract index classes are checked too.
Multi-map indexes have no Map property, so only Reduce assignments apply to them.

RVN001 also reports Reduce assignments in JavaScript indexes derived from AbstractJavaScriptIndexCreationTask. Assignments to their Maps property are not checked.

When is RVN001 reported?​

RVN001 is reported when Map or Reduce is assigned inside an ordinary method of a supported index class, including assignments inside lambdas or local functions nested in that method. Both block-bodied and expression-bodied methods are checked.

The assignment must use an unqualified property name (Map or Reduce), this.Map / this.Reduce, or base.Map / base.Reduce.

RVN001 does not trace calls. It is still reported when the method is always called from a constructor, or when it reassigns a property the constructor already set.

When is RVN001 not reported?​

RVN001 is not reported for:

  • Assignments in constructors, including lambdas or local functions nested in them.
  • Assignments in property or indexer accessors, operators, or finalizers.
  • Assignments to local variables or parameters named Map or Reduce.
  • Assignments through another object, such as other.Map or other.Reduce.
  • Calls to AddMap or AddMapForAll inside methods.
  • Single-map time-series and counters indexes based on AbstractTimeSeriesIndexCreationTask<...> or AbstractCountersIndexCreationTask<...>.

How to resolve​

Move the reported Map or Reduce assignment directly into the constructor body.


Before (triggers RVN001)​

public class Orders_ByCompany : AbstractIndexCreationTask<Order>
{
public void Configure()
{
// Triggers RVN001 even though the constructor always calls Configure().
Map = orders => from o in orders
select new { o.Company, o.Employee };
}

public Orders_ByCompany()
{
Configure();
}
}

After (correct)​

public class Orders_ByCompany : AbstractIndexCreationTask<Order>
{
public Orders_ByCompany()
{
Map = orders => from o in orders
select new { o.Company, o.Employee };
}
}

Limitations​

  • A partial class declaration that lists neither a base class nor any interfaces is skipped. Assignments in methods declared in that part are not checked, even when another part declares the index base class.

  • RVN001 can also report assignments to custom fields or properties named Map or Reduce on an index class,
    even when they are unrelated to the index definition.

  • RVN004
    Fires when no constructor sets Map, directly or through a method it calls.
    RVN001 and RVN004 can both be reported when Map is assigned only in a method that no constructor calls.

  • RVN005
    Fires when no constructor registers a map through AddMap or AddMapForAll, directly or through a method it calls.

In this article