Configuration & Bootstrapping
RCommon uses a single fluent builder entry point. All configuration happens at application startup before the DI container is built.
The AddRCommon() extension method
AddRCommon() is an extension method on IServiceCollection defined in RCommon.Core. Calling it creates an RCommonBuilder, registers the core framework services, and returns an IRCommonBuilder for further configuration.
using RCommon;
// In Program.cs or a DI registration method
builder.Services.AddRCommon();
Calling AddRCommon() always registers these services regardless of what else is configured:
EventSubscriptionManager(singleton) — tracks event-to-producer subscriptionsIEventBusbacked byInMemoryEventBus(singleton) — in-process publish/subscribeIEventRouterbacked byInMemoryTransactionalEventRouter(scoped) — coordinates domain events with the unit of workCachingOptionsconfigured with caching disabled by default
Builder chain
The return value of AddRCommon() is IRCommonBuilder. Every With* method on the builder returns the same IRCommonBuilder instance so calls can be chained:
builder.Services.AddRCommon()
.WithSequentialGuidGenerator(...)
.WithDateTimeSystem(...)
.WithPersistence<EFCorePerisistenceBuilder>(...)
.WithUnitOfWork<DefaultUnitOfWorkBuilder>(...)
.WithMediator<MediatRBuilder>(...)
.WithEventHandling<InMemoryEventBusBuilder>(...);
Every With* call is independent and opt-in. Omitting a call means those services are not registered.
Core builder methods
WithSequentialGuidGenerator
Registers IGuidGenerator as a SequentialGuidGenerator. Sequential GUIDs are ordered in a way that reduces index fragmentation in SQL databases.
.WithSequentialGuidGenerator(options =>
{
options.DefaultSequentialGuidType = SequentialGuidType.SequentialAsString;
})
SequentialGuidType values:
| Value | Description |
|---|---|
SequentialAsString | Sequential in string representation — compatible with MySQL and PostgreSQL |
SequentialAsBinary | Sequential in binary representation — compatible with Oracle |
SequentialAtEnd | Sequential at the end — compatible with SQL Server |
Only one GUID generator may be configured. Calling WithSequentialGuidGenerator or WithSimpleGuidGenerator a second time throws RCommonBuilderException.
WithSimpleGuidGenerator
Registers IGuidGenerator as a SimpleGuidGenerator that wraps Guid.NewGuid().
.WithSimpleGuidGenerator()
WithDateTimeSystem
Registers ISystemTime as SystemTime. Inject ISystemTime wherever you need the current time so that tests can substitute a known value.
.WithDateTimeSystem(options =>
{
options.Kind = DateTimeKind.Utc;
})
Only one date/time system may be configured per builder instance.
WithCommonFactory
Registers a service type alongside a DI-aware ICommonFactory<TService> that can create instances through the container.
.WithCommonFactory<IMyService, MyServiceImpl>()
Persistence configuration
WithPersistence<T> accepts any type that implements IPersistenceBuilder. The built-in options are EFCorePerisistenceBuilder, DapperPersistenceBuilder, and Linq2DbPersistenceBuilder.
.WithPersistence<EFCorePerisistenceBuilder>(ef =>
{
ef.AddDbContext<AppDbContext>("AppDb", options =>
{
options.UseSqlServer(connectionString);
});
ef.SetDefaultDataStore(ds =>
{
ds.DefaultDataStoreName = "AppDb";
});
})
Multiple DbContexts can be registered with different names. The name is used to resolve the correct context at runtime via IDataStoreFactory. Use SetDefaultDataStore to declare which context a repository uses when no explicit data store name is specified on the repository instance.