Skip to content

Application

The Application module provides the tools you need to define and execute application use cases with CQRS and pipeline behaviors.

Use this section when you need to:

  • create a command;
  • create a query;
  • create a handler;
  • validate commands or queries;
  • add logging around application requests;
  • monitor execution performance;
  • add idempotency to commands;
  • configure concurrency retries;
  • enable query processing features such as caching;
  • handle exceptions consistently.

Application features are composed through XenoAppBuilder. Pass pipeline plugins to addPipeline() to configure the corresponding cross-cutting behavior.

For example, add performance tracking to the command and query pipelines:

import { XenoAppBuilder, withPerformance } from '@xeno-js/core'
const app = await new XenoAppBuilder()
.addPipeline(withPerformance())
.build()

Configure a plugin through its setup callback when you need to override its defaults:

import { XenoAppBuilder, withPerformance } from '@xeno-js/core'
const app = await new XenoAppBuilder()
.addPipeline(withPerformance((options) => {
options.thresholdMs = 500
options.intentThresholdMs = {
CREATE_USER_HANDLER: { thresholdMs: 300 },
}
}))
.build()

Call build() after configuring the required plugins. The exact options depend on the plugin; each feature page documents its configuration and prerequisites.

To create a command, see:

Create a Command

The guide covers:

  • defining the command;
  • defining its input;
  • adding validation;
  • sending the command;
  • connecting it to a handler.

To create a query, see:

Create a Query

The guide covers:

  • defining the query;
  • defining its parameters;
  • adding validation;
  • executing the query;
  • connecting it to a handler.

To implement the application logic for a command or query, see:

Create a Handler

The guide covers:

  • extending BaseHandler;
  • implementing the handler method;
  • receiving dependencies;
  • accessing the current user context when required;
  • registering the handler;
  • connecting the handler to its command or query.

Pipeline behaviors let you add cross-cutting behavior to commands and queries without putting that behavior directly into every handler.

Choose the feature you need:

Task Documentation
Handle application exceptions Exception Pipeline
Add request logging Logging Pipeline
Monitor execution time Performance Pipeline
Validate commands and queries Validation Pipeline
Prevent duplicate command processing Idempotency Pipeline
Retry concurrency conflicts Concurrency Pipeline
Enable query caching Caching Pipeline

If you need to validate a command or query before its handler runs:

Enable Validation

The validation guide explains:

  • how to configure validation in XenoAppBuilder;
  • how to use Zod schemas;
  • where to define schemas;
  • how to configure custom validation;
  • what happens when validation fails.

For the command and query definitions used by the examples, see:

To configure logging around command and query execution:

Configure Logging

To monitor command and query execution time:

Configure Performance Tracking

To prevent duplicate processing of commands:

Configure Idempotency

To configure concurrency retries for commands:

Configure Concurrency

To enable query caching:

Enable Caching

To configure or troubleshoot exception handling for application requests:

Configure Exception Handling

Use the CQRS section when you are creating application requests and their handlers.

Task Documentation
Understand the CQRS workflow CQRS Overview
Create a command Create a Command
Create a query Create a Query
Create a handler Create a Handler

A typical application use case follows this path:

Create Command or Query
│
▼
Add input validation if required
│
▼
Create the Handler
│
▼
Register the Handler
│
▼
Configure required pipelines
│
▼
Execute the Command or Query

For example, when creating a new command:

Create Command
│
├── Add validation
│ └── Validation Pipeline
│
└── Create Handler
└── Register Handler

The individual guides contain the implementation details required for each step.

Use the task that matches what you are trying to do:

If a command or query does not execute as expected, check:

  1. the required pipeline plugin is passed to XenoAppBuilder.addPipeline();
  2. the plugin’s required configuration is provided;
  3. the command or query is connected to a handler;
  4. the handler is registered in the application;
  5. the request is executed through the configured mediator;
  6. any feature-specific prerequisites documented in the relevant pipeline guide are satisfied.

If the problem is specific to a feature, start from its dedicated guide rather than troubleshooting the entire Application module.


Xeno.JS is an MIT-licensed open source project. It can grow thanks to the support of these awesome people. If you’d like to join them, please read more at support section