GitPedia

Darker

The query-side counterpart of Brighter

From BrighterCommand·Updated June 23, 2026·View on GitHub·

The query-side counterpart of [Brighter](https://github.com/BrighterCommand/Paramore.Brighter). The project is written primarily in C#, distributed under the MIT License license, first published in 2016. Key topics include: brighter, darker, dotnet-core, handlers, query.

Latest release: 4.1.1
June 2, 2025View Changelog →

Darker

The query-side counterpart of Brighter.

.NET Core
NuGet

Usage with Microsoft.Extensions.DependencyInjection

In your ConfigureServices method, use AddDarker to add Darker to the container.
DI integration is provided by the Paramore.Darker.Extensions.DependencyInjection package.

csharp
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { // Add Darker and some extensions. services.AddDarker() .AddHandlersFromAssemblies(typeof(GetPeopleQueryHandler).Assembly) .AddJsonQueryLogging() .AddDefaultPolicies(); // Add framework services. services.AddMvc(); }

WARNING if you are using EFCore the DBContext DI Lifetime is scoped, for Darker to play nicely with EFCore and DI the QueryProcessor must also be registration as Scoped

csharp
services.AddDarker(options => { //EFCore by default registers Context as scoped, which forces the QueryProcessorLifetime to also be scoped options.QueryProcessorLifetime = ServiceLifetime.Scoped; })

This example uses the request logging integration provided by Paramore.Darker.QueryLogging
and policy integration provided by Paramore.Darker.Policies.
Have a look at the Startup.ConfigureServices method
in the SampleApi project for more examples on how to use the integrations.

Inject IQueryProcessor and call Execute or ExecuteAsync to dispatch your query to the registered query handler.

csharp
using Paramore.Darker; using Microsoft.AspNetCore.Mvc; using System.Threading; using System.Threading.Tasks; public class FooController : ControllerBase { private readonly IQueryProcessor _queryProcessor; public FooController(IQueryProcessor queryProcessor) { _queryProcessor = queryProcessor; } public async Task<IActionResult> Get(CancellationToken cancellationToken = default(CancellationToken)) { var query = new GetFoo(42); var result = await _queryProcessor.ExecuteAsync(query, cancellationToken); return Ok(result); } }
csharp
using Paramore.Darker; public sealed class GetFoo : IQuery<string> { public int Number { get; } public GetFoo(int number) { Number = number; } }

Implement either QueryHandler<,> or QueryHandlerAsync<,> depending on whether you wish to execute your queries synchronously or asynchronously.
For most control, you can also implement IQueryHandler<,> directly.

csharp
using Paramore.Darker; using Paramore.Darker.Attributes; using Paramore.Darker.Policies; using Paramore.Darker.QueryLogging; using System.Threading; using System.Threading.Tasks; public sealed class GetFooHandler : QueryHandlerAsync<GetFoo, string> { [QueryLogging(1)] [FallbackPolicy(2)] [RetryableQuery(3)] public override async Task<string> ExecuteAsync(GetFoo query, CancellationToken cancellationToken = default(CancellationToken)) { return await FetchFooForNumber(query.Number, cancellationToken); } }

Usage without ASP.NET

Register your queries and handlers with QueryHandlerRegistry and use QueryProcessorBuilder to configure and build a IQueryProcessor.

csharp
var registry = new QueryHandlerRegistry(); registry.Register<GetFoo, string, GetFooHandler>(); IQueryProcessor queryProcessor = QueryProcessorBuilder.With() .Handlers(registry, Activator.CreateInstance, t => {}, Activator.CreateInstance) .InMemoryQueryContextFactory() .Build();

Instead of Activator.CreateInstance, you can pass any factory Func<Type, object> to construct handlers and decorators.

Note: The Paramore.Darker.SimpleInjector and Paramore.Darker.LightInject packages have been removed as of V5. If you use a third-party DI container, use its built-in adapter for Microsoft.Extensions.DependencyInjection and integrate with Darker via the Paramore.Darker.Extensions.DependencyInjection package instead.

Contributors

Showing top 12 contributors by commit count.

View all contributors on GitHub →

This article is auto-generated from BrighterCommand/Darker via the GitHub API.Last fetched: 6/24/2026