Cratis Chronicle .NET namespace resolution
The Cratis Chronicle .NET client uses IEventStoreNamespaceResolver to select a namespace when you call GetEventStore without an explicit namespace. The returned event store stays bound to that namespace. Configure a resolver when a worker service or console application needs to select tenant data from its current execution context.
IEventStoreNamespaceResolver
Section titled “IEventStoreNamespaceResolver”using Cratis.Chronicle;
public class NamespacesDotNetClientSampleResolver : IEventStoreNamespaceResolver{ public EventStoreNamespaceName Resolve() => EventStoreNamespaceName.Default;}import io.cratis.chronicle.EventStoreNamespaceNameimport io.cratis.chronicle.namespaces.IEventStoreNamespaceResolver
class NamespacesDotNetClientSampleResolver : IEventStoreNamespaceResolver { override fun resolve(): String = EventStoreNamespaceName.default.value}import io.cratis.chronicle.namespaces.IEventStoreNamespaceResolver;
class NamespacesDotNetClientSampleResolver implements IEventStoreNamespaceResolver { @Override public String resolve() { return "Default"; }}Elixir does not support this workflow yet.TypeScript does not support this workflow yet.Resolve() returns an EventStoreNamespaceName for the current context. Passing a namespace explicitly to GetEventStore bypasses the resolver for that call.
Built-in resolvers
Section titled “Built-in resolvers”DefaultEventStoreNamespaceResolver
Section titled “DefaultEventStoreNamespaceResolver”DefaultEventStoreNamespaceResolver always returns EventStoreNamespaceName.Default. Direct clients and non-web hosts use it when no resolver is supplied. Choose it when the application uses a single namespace.
ClaimsBasedNamespaceResolver
Section titled “ClaimsBasedNamespaceResolver”ClaimsBasedNamespaceResolver reads a tenant claim from an authenticated principal. By default, it reads ClaimsPrincipal.Current and looks for tenant_id; the example shows how to choose a different claim type:
using Cratis.Chronicle;
public static class NamespacesDotNetClientClaimsBased{ public static ChronicleClient CreateWithDefaultClaimType(ChronicleOptions options) => // Uses the default claim type "tenant_id" new(options, namespaceResolver: new ClaimsBasedNamespaceResolver("tenant_id"));
public static ChronicleClient CreateWithCustomClaimType(ChronicleOptions options) => // Uses a custom claim type new(options, namespaceResolver: new ClaimsBasedNamespaceResolver("custom_tenant_claim"));}import io.cratis.chronicle.EventStoreNamespaceNameimport io.cratis.chronicle.namespaces.IEventStoreNamespaceResolver
/** * Takes the namespace from a claim on the currently authenticated principal, falling back to the * default namespace for anonymous requests or work happening outside a request. The Spring Boot * integration ships this exact strategy as * io.cratis.chronicle.spring.namespaces.AuthenticationNamespaceResolver; this version takes the * current claims as a parameter so it has no framework dependency. */class ClaimsBasedNamespaceResolver( private val claim: String, private val currentClaims: () -> Map<String, String>?) : IEventStoreNamespaceResolver { override fun resolve(): String = currentClaims()?.get(claim) ?: EventStoreNamespaceName.default.value}
fun createWithDefaultClaimType(currentClaims: () -> Map<String, String>?): IEventStoreNamespaceResolver = ClaimsBasedNamespaceResolver("tenant_id", currentClaims)
fun createWithCustomClaimType(currentClaims: () -> Map<String, String>?): IEventStoreNamespaceResolver = ClaimsBasedNamespaceResolver("custom_tenant_claim", currentClaims)import io.cratis.chronicle.namespaces.IEventStoreNamespaceResolver;
import java.util.Map;import java.util.function.Supplier;
// Takes the namespace from a claim on the currently authenticated principal, falling back to the// default namespace for anonymous requests or work happening outside a request. The Spring Boot// integration ships this exact strategy as// io.cratis.chronicle.spring.namespaces.AuthenticationNamespaceResolver; this version takes the// current claims as a parameter so it has no framework dependency.class ClaimsBasedNamespaceResolver implements IEventStoreNamespaceResolver { private final String claim; private final Supplier<Map<String, String>> currentClaims;
public ClaimsBasedNamespaceResolver(String claim, Supplier<Map<String, String>> currentClaims) { this.claim = claim; this.currentClaims = currentClaims; }
@Override public String resolve() { Map<String, String> claims = currentClaims.get(); String value = claims == null ? null : claims.get(claim); return value == null ? "Default" : value; }
public static IEventStoreNamespaceResolver createWithDefaultClaimType(Supplier<Map<String, String>> currentClaims) { return new ClaimsBasedNamespaceResolver("tenant_id", currentClaims); }
public static IEventStoreNamespaceResolver createWithCustomClaimType(Supplier<Map<String, String>> currentClaims) { return new ClaimsBasedNamespaceResolver("custom_tenant_claim", currentClaims); }}Elixir does not support this workflow yet.TypeScript does not support this workflow yet.The resolver also accepts a principal-accessor callback when the principal comes from another context. If the principal is absent or unauthenticated, or the claim is missing or empty, it returns the default namespace. If your application requires a named tenant, reject that missing context before accessing the event store rather than treating the fallback as authorization.
Passing a resolver to ChronicleClient
Section titled “Passing a resolver to ChronicleClient”For a console application or another directly constructed client, pass the resolver through the namespaceResolver constructor parameter. This example explicitly selects the same default resolver used when you omit the parameter:
using Cratis.Chronicle;
public static class NamespacesDotNetClientDefaultResolver{ public static ChronicleClient Create(ChronicleOptions options) { var resolver = new DefaultEventStoreNamespaceResolver(); return new ChronicleClient(options, namespaceResolver: resolver); }}import io.cratis.chronicle.ChronicleClientimport io.cratis.chronicle.IEventStoreimport io.cratis.chronicle.namespaces.DefaultEventStoreNamespaceResolver
fun getDefaultEventStore(client: ChronicleClient, name: String): IEventStore = client.getEventStore(name, DefaultEventStoreNamespaceResolver.resolve())import io.cratis.chronicle.IEventStore;import io.cratis.chronicle.java.BlockingChronicleClient;import io.cratis.chronicle.namespaces.DefaultEventStoreNamespaceResolver;
class NamespacesDotNetClientDefaultResolver { IEventStore create(BlockingChronicleClient client, String name) { String namespace = DefaultEventStoreNamespaceResolver.INSTANCE.resolve(); return client.getEventStore(name, namespace).unwrap(); }}Elixir does not support this workflow yet.TypeScript does not support this workflow yet.Configuring in a hosted application
Section titled “Configuring in a hosted application”For a worker service using IHostApplicationBuilder, either set ChronicleClientOptions.EventStoreNamespaceResolverType or supply an instance through IChronicleBuilder. The instance takes priority over the configured type:
using Microsoft.Extensions.Hosting;
public static class NamespacesDotNetClientHostedAppConfiguration{ public static void ConfigureViaOptions(IHostApplicationBuilder builder) => // Via options (type is resolved from DI) builder.AddCratisChronicle(options => { options.EventStore = "my-store"; options.EventStoreNamespaceResolverType = typeof(TenantNamespaceResolver); });
public static void ConfigureViaBuilder(IHostApplicationBuilder builder, ITenantContext tenantContext) => // Via builder (instance is used directly) builder.AddCratisChronicle( configureOptions: options => options.EventStore = "my-store", configure: b => b.WithNamespaceResolver(new TenantNamespaceResolver(tenantContext)));}import io.cratis.chronicle.ChronicleClientimport io.cratis.chronicle.IEventStoreimport io.cratis.chronicle.namespaces.IEventStoreNamespaceResolver
/** * Kotlin has no separate hosted-app builder to configure a resolver on structurally - a worker * service resolves the namespace and asks the client for that event store exactly like a console * application would. */fun currentEventStore(client: ChronicleClient, name: String, resolver: IEventStoreNamespaceResolver): IEventStore = client.getEventStore(name, resolver.resolve())import io.cratis.chronicle.IEventStore;import io.cratis.chronicle.java.BlockingChronicleClient;import io.cratis.chronicle.namespaces.IEventStoreNamespaceResolver;
// Java has no separate hosted-app builder to configure a resolver on structurally - a worker// service resolves the namespace and asks the client for that event store exactly like a console// application would.class NamespacesDotNetClientHostedAppConfiguration { IEventStore currentEventStore(BlockingChronicleClient client, String name, IEventStoreNamespaceResolver resolver) { return client.getEventStore(name, resolver.resolve()).unwrap(); }}Elixir does not support this workflow yet.TypeScript does not support this workflow yet.Chronicle creates type-based resolvers with dependency injection (DI) and keeps the resolver as a singleton. It resolves the namespace when the scoped IEventStore is first requested. Set the tenant context before resolving that service, and use a new scope for another tenant.
Custom resolvers
Section titled “Custom resolvers”If your application uses Cratis Arc tenancy, use its existing tenant-to-namespace mapping. See Arc tenancy.
For another tenant context, implement a resolver. This example returns ITenantContext.CurrentTenantId directly, making that value the Chronicle namespace:
using Cratis.Chronicle;
public interface ITenantContext{ EventStoreNamespaceName CurrentTenantId { get; }}
public class TenantNamespaceResolver : IEventStoreNamespaceResolver{ readonly ITenantContext _tenantContext;
public TenantNamespaceResolver(ITenantContext tenantContext) { _tenantContext = tenantContext; }
public EventStoreNamespaceName Resolve() => _tenantContext.CurrentTenantId;}import io.cratis.chronicle.namespaces.IEventStoreNamespaceResolver
interface ITenantContext { val currentTenantId: String}
class TenantNamespaceResolver(private val tenantContext: ITenantContext) : IEventStoreNamespaceResolver { override fun resolve(): String = tenantContext.currentTenantId}import io.cratis.chronicle.namespaces.IEventStoreNamespaceResolver;
interface ITenantContext { String getCurrentTenantId();}
class TenantNamespaceResolver implements IEventStoreNamespaceResolver { private final ITenantContext tenantContext;
public TenantNamespaceResolver(ITenantContext tenantContext) { this.tenantContext = tenantContext; }
@Override public String resolve() { return tenantContext.getCurrentTenantId(); }}Elixir does not support this workflow yet.TypeScript does not support this workflow yet.Pass the resolver to a directly constructed client. Its tenant context must expose the current execution’s tenant without sharing a mutable tenant id across concurrent operations:
using Cratis.Chronicle;
public static class NamespacesDotNetClientTenantResolverUsage{ public static ChronicleClient Create(ChronicleOptions options, ITenantContext tenantContext) => new(options, namespaceResolver: new TenantNamespaceResolver(tenantContext));}import io.cratis.chronicle.ChronicleClientimport io.cratis.chronicle.IEventStore
fun getTenantEventStore(client: ChronicleClient, name: String, tenantContext: ITenantContext): IEventStore = client.getEventStore(name, TenantNamespaceResolver(tenantContext).resolve())import io.cratis.chronicle.IEventStore;import io.cratis.chronicle.java.BlockingChronicleClient;
class NamespacesDotNetClientTenantResolverUsage { IEventStore create(BlockingChronicleClient client, String name, ITenantContext tenantContext) { String namespace = new TenantNamespaceResolver(tenantContext).resolve(); return client.getEventStore(name, namespace).unwrap(); }}Elixir does not support this workflow yet.TypeScript does not support this workflow yet.Best practices
Section titled “Best practices”Keep tenant selection stable for the lifetime of an event store instance. Changing the resolver’s context does not retarget an event store you already obtained; call GetEventStore again for the next tenant, or create a new DI scope in a hosted application.
Keep resolution quick and avoid blocking I/O in Resolve(). Decide explicitly whether a missing tenant may use the default namespace. Before relying on the setup, check that two tenant contexts select different namespaces and that missing context follows your chosen policy.
For request-based tenant selection and its authorization boundary, continue with ASP.NET Core namespace resolution.