Static Command Handlers in Wolverine: The Good Kind of Static

Static Command Handlers in Wolverine: The Good Kind of Static
by Brad Jolicoeur
12/04/2026

I've become wary of static methods in application code for the same reasons many senior engineers are wary of them.

Static state can leak across tests. Static service locators hide dependencies. A static database context can be shared across requests that should be isolated. I've seen code that was convenient to call from anywhere become difficult to change, test, and operate precisely because it depended on process-wide state.

So when I first saw Wolverine use static command handlers, I was skeptical. How could a static method participate in dependency injection, message handling, and transaction management without bringing those familiar problems along?

The distinction is that a static method is not the same thing as static state. In Wolverine, the method is an entry point for application behavior. Wolverine supplies its dependencies and composes the runtime pipeline around it.

For focused commands, that makes the static form more than acceptable. It can express the operation more directly, without creating an object just to hold dependencies until one method is called.

What We Are Really Worried About

The objections to static code are usually objections to hidden dependencies and uncontrolled lifetimes:

  • Mutable static fields are shared across invocations and can introduce concurrency problems.
  • A static service locator lets a method acquire dependencies that aren't visible in its signature.
  • A static database session can outlive the operation it was meant to serve.
  • Global configuration and caches can make tests interfere with one another.

These are real architectural problems. But none is required by a static method. The method belongs to a type rather than an object instance; its parameters and local variables don't become global as a result.

The important questions are still: Where does the state live? Who creates and owns each dependency? What is shared between invocations?

A Product Catalog Command

Suppose a Product Catalog accepts a command to draft a product. Here's a Wolverine handler that stores the draft using a Marten document session:

using System;
using Marten;

public sealed record DraftProduct(Guid ProductId, string Name);
public sealed record Product(Guid Id, string Name);

public static class DraftProductHandler
{
    public static void Handle(
        DraftProduct command,
        IDocumentSession session)
    {
        if (string.IsNullOrWhiteSpace(command.Name))
            throw new ArgumentException("A product name is required.");

        session.Store(new Product(command.ProductId, command.Name.Trim()));
    }
}

Wolverine recognizes the handler by convention. The first parameter is the command; Wolverine supplies the IDocumentSession for the invocation. The handler queues the document for storage in that session. With Marten integration and transactional middleware configured, Wolverine saves the session as part of the handler pipeline.

The session is not a static field. The command is not global state. There is no static service locator in the handler. Each call receives the values it needs as parameters.

This example uses Marten's Store, which inserts or updates a document. A real catalog that must reject duplicate product IDs needs an appropriate insert or concurrency strategy; static syntax doesn't decide that behavior.

The Method Is Not the Whole Runtime

Seeing a static method can make it look as though someone will call it directly and handle everything else by hand. Wolverine does more than that.

Wolverine discovers the handler and generates an invocation chain. That chain resolves the dependencies, invokes the method, and applies the middleware and policies configured for the application. Conceptually, the Product Catalog flow is:

DraftProduct arrives
  -> Wolverine resolves the session for this invocation
  -> configured middleware wraps the handler
  -> DraftProductHandler.Handle(command, session)
  -> transaction middleware saves the session

The exact generated code depends on the handler, registrations, and Wolverine configuration. For example, Marten integration and automatic transaction handling can be configured at application startup:

builder.Services
    .AddMarten(options => options.Connection(connectionString))
    .IntegrateWithWolverine();

builder.Host.UseWolverine(options =>
{
    options.Policies.AutoApplyTransactions();
});

With that setup, the handler queues the write and Wolverine's transaction middleware calls SaveChangesAsync(). The method doesn't need to create its own session or commit it.

The static method is the application entry point, not a replacement for the runtime pipeline. Wolverine composes that pipeline for instance handlers too. Static is a choice about how the application method is invoked, not a shortcut around infrastructure.

Static Does Not Mean Singleton

It's easy to look at the handler and picture a single shared object serving every message. There isn't one: the static handler has no instance.

The session, on the other hand, is an object with a lifetime. Wolverine's integration and dependency injection manage it within the invocation's runtime scope. Passing it to a static method does not change it into a static or process-wide session. Its mutable unit of work remains associated with the scope that created it.

That distinction also exposes what static doesn't guarantee. A static handler can still call a global service locator, write to a mutable static field, or receive an incorrectly registered singleton. A static method can also mutate its arguments and perform I/O, as this one does. None of that makes it automatically pure, thread-safe, or easy to test.

The same lifetime discipline applies with an instance handler. Constructor injection doesn't protect an application from a mutable singleton registered with the wrong lifetime. Review the registrations and the code that uses them, not just the handler's shape.

What Does Static Actually Buy Us?

The advantage is clearest when the handler represents one focused operation.

First, the method shows its dependencies at the point of use. A reader sees both DraftProduct and IDocumentSession in the signature rather than tracing a field back to a constructor. When a handler has several methods, method injection can make it clear which dependencies each operation actually uses.

Second, there is no handler object to construct just to hold those dependencies. For a small command handler, a constructor and private fields can add ceremony without adding a meaningful abstraction. A static handler also can't accidentally retain per-invocation values in instance fields because it has no instance fields.

Third, the method can be called directly. A test can pass a command and a suitable session or test double to Handle without constructing a handler. That doesn't remove the need to test persistence, transaction behavior, or Wolverine's generated chain through integration tests.

These are practical benefits, not magic properties. An instance handler can also use method injection and expose the same dependencies directly. Either style can be tested well or poorly. Static is useful because it makes the absence of handler instance state explicit and removes an object that this particular operation doesn't need.

Avoiding a handler allocation may also have a small performance benefit, but I wouldn't choose the design on that basis without measuring the application. Clarity and state boundaries are the stronger argument.

Compare the Equivalent Instance Handler

The same Product Catalog operation can be written as an instance handler with constructor injection:

public sealed class DraftProductHandler(IDocumentSession session)
{
    public void Handle(DraftProduct command)
    {
        if (string.IsNullOrWhiteSpace(command.Name))
            throw new ArgumentException("A product name is required.");

        session.Store(new Product(command.ProductId, command.Name.Trim()));
    }
}

The business behavior and Marten operation are the same. Wolverine still composes the invocation and configured middleware. The difference is where the dependency is expressed: the constructor stores it on the handler instance; the static version takes it on the method.

The instance form is not a singleton by default. Wolverine scopes an instance handler to a message and disposes it afterward. The session's own lifetime still depends on its registration and the integration.

For this one operation, I prefer the static version: the class doesn't need its own state, and the complete dependency list is right on Handle. But that preference isn't a claim that the instance version is unsafe.

When an Instance Is the Better Fit

An instance handler can be a better expression of the design when several operations share a meaningful set of collaborators or when a constructor describes a coherent component boundary. It can also be clearer when a long method signature obscures the operation rather than explaining it.

But those situations are also a reason to reconsider the implementation before choosing an instance. A long dependency list or several operations sharing collaborators may indicate that the handler has too many responsibilities, or that related behavior belongs in a cohesive domain service. Moving parameters into a constructor can make a signature shorter without resolving the underlying complexity.

Instance fields can hold state relevant to the handler's message-scoped work, but they are not durable workflow state. State that must survive across messages may belong in an event-sourced aggregate or a persisted saga, depending on whether it represents a domain entity or a longer-running process.

These are questions to investigate, not automatic reasons to refactor. If the handler has a cohesive purpose and an instance makes that purpose clearer, Wolverine supports it. Conversely, if the constructor exists only to pass dependencies to one operation, a static method can make that simpler design visible.

Transactions and Delivery Still Need Design

Static handlers don't bypass the operational concerns of a message-driven application. In the Product Catalog, concurrent writes, retries, duplicate commands, and delivery to other services all need deliberate handling.

Wolverine's configured transaction, retry, and outbox policies apply around the handler; they are not consequences of the static keyword. Retries must be safe for the operation being repeated, and an application may need an idempotency strategy for duplicate commands.

A configured durable outbox can coordinate supported outgoing messages with the database transaction. It doesn't make an arbitrary HTTP call or email atomic with that transaction. These boundaries matter regardless of whether the handler method is static or instance-based.

A Practical Review Checklist

Before approving either handler style, I would ask:

  1. Are dependencies visible? Check method parameters, constructor parameters, and any hidden service location.
  2. Where does mutable state live? Look for static fields and shared services, not just the handler declaration.
  3. Are lifetimes correct? A database session should not be retained beyond its intended scope.
  4. What does the configured pipeline do? Verify transaction, retry, and delivery policies rather than assuming them.
  5. Are tests at the right level? Test the operation's behavior and exercise the real Wolverine and persistence integration where those boundaries matter.

For a static handler, add one more question: Is the lack of an instance helping the design? If the handler feels unwieldy, first examine its responsibilities and state boundaries; then decide whether refactoring or an instance would make the design clearer.

Static Without Global State

The bad experiences behind skepticism about static methods are valid. Shared mutable state, hidden dependencies, and uncontrolled lifetimes can make systems difficult to test and operate.

Wolverine's static handler form doesn't require those practices. The static method is an entry point with dependencies passed in for the invocation; Wolverine provides the generated runtime composition and configured middleware around it.

For a focused command like drafting a product, that gives me an explicit operation boundary without manufacturing a handler object. When an instance provides a clearer component boundary, Wolverine supports that too.

The decision isn't whether static is always good or always bad. It's whether the chosen form makes state, dependencies, and their lifetimes easier to understand.

Further Reading


Brad Jolicoeur
About Brad Jolicoeur

Principal Architect with 20+ years building and transforming engineering organizations. Wharton Executive CTO Program graduate. Writing about architecture, distributed systems, production AI, and engineering leadership.

Get in touch →   More articles →

You May Also Like


Event Sourcing in Practice: Marten and Wolverine Working Together

martin-wolverine-eventsource.png
Brad Jolicoeur - 06/14/2026
Read

Fast Code, Stuck Value

feature-dollar-days.png
Brad Jolicoeur - 05/26/2026
Read

Coding Was Never the Bottleneck

abolish-local-optima.png
Brad Jolicoeur - 05/25/2026
Read