RVN001 - Index Map or Reduce assigned outside constructor
-
RVN001 reports assignments to an index's
MaporReduceproperty inside a method instead of directly in the constructor. -
Default severity: Info
Automated code fix: No -
In this article:
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>, orAbstractGenericIndexCreationTask<TReduceResult>.AbstractMultiMapIndexCreationTask,AbstractMultiMapTimeSeriesIndexCreationTask, orAbstractMultiMapCountersIndexCreationTask, 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
MaporReduce. - Assignments through another object, such as
other.Maporother.Reduce. - Calls to
AddMaporAddMapForAllinside methods. - Single-map time-series and counters indexes based on
AbstractTimeSeriesIndexCreationTask<...>orAbstractCountersIndexCreationTask<...>.
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
MaporReduceon an index class,
even when they are unrelated to the index definition.
Related C# analyzers
-
RVN004
Fires when no constructor setsMap, directly or through a method it calls.
RVN001 and RVN004 can both be reported whenMapis assigned only in a method that no constructor calls. -
RVN005
Fires when no constructor registers a map throughAddMaporAddMapForAll, directly or through a method it calls.