Register NHibernate services¶
This operation wires the library's modern DI surface into a consuming application's IServiceCollection, so that UnitOfWorkAccessor can later be injected and used to obtain units of work.
| Field | Value |
|---|---|
| Operation ID | OP-NH-01 |
| Trigger | Host calls services.AddNhibernateServices(assemblyMappings, debugOutputSql) or services.AddNhibernateServices<T>(debugOutputSql) at startup |
| Inputs | assemblyMappings (or a marker type T), debugOutputSql |
| Outputs | IUnitOfWorkFactory singleton, UnitOfWorkAccessor scoped service |
| Primary components | IServicesCollectionExtensions.AddNhibernateServices, AddUnitOfWorkFactory |
| Entry point | src/SAIF.NetCore.Nhibernate/SAIF.NetCore.NHibernate/SAIF.NetCore.NHibernate/Extensions/IServicesCollectionExtensions.cs |
Steps¶
AddNhibernateServices<T>(bool debugOutputSql = false)resolves a single mapping assembly viaAssembly.GetAssembly(typeof(T))and forwards to theIEnumerable<Assembly>overload. Call this overload directly instead when more than one mapping assembly must be scanned.- The internal
AddUnitOfWorkFactory(assemblyMappings, debugOutputSql)registersAddSingleton<IUnitOfWorkFactory>(provider => new UnitOfWorkFactory(assemblyMappings, debugOutputSql)). The mapping assemblies anddebugOutputSqlflag are captured in this closure for the lifetime of the singleton (BR-NH-01). AddNhibernateServicesthen registersAddScoped<UnitOfWorkAccessor>().- It returns
servicesto support fluent chaining. - It does not call
AddOptions(), register anyIConfigureOptions<UnitOfWorkOptions>, or registerIUnitOfWorkdirectly — the consumer must register its ownConfigureUnitOfWorkOptionssubclass (see Configure named unit-of-work options). - Calling
AddNhibernateServicestwice adds duplicate registrations; the last registration wins at resolution.*
Process flow¶
The diagram below visualizes the generic-overload assembly-resolution branch and the two DI registrations that follow it.
flowchart TD
Start([Host calls AddNhibernateServices]) --> Resolve{"Generic overload?"}
Resolve -->|Yes| GetAssembly["Assembly.GetAssembly(typeof(T))"]
Resolve -->|No| UseGiven["Use supplied assemblyMappings"]
GetAssembly --> RegisterFactory["AddSingleton IUnitOfWorkFactory"]
UseGiven --> RegisterFactory
RegisterFactory --> RegisterAccessor["AddScoped UnitOfWorkAccessor"]
RegisterAccessor --> Return([Return services])
Failure and edge-case behavior¶
- Calling
AddNhibernateServicesmore than once registers the singleton factory multiple times; the container resolves the last registration.* - No exception is thrown at registration time even if
assemblyMappingsis empty or contains no mapped types — failures surface later, at session-factory build time.
Related documentation¶
Note: Statements marked with
*were inferred from code structure, naming conventions, or contextual evidence where direct confirmation was not available in source artifacts.
Sources¶
src/SAIF.NetCore.Nhibernate/SAIF.NetCore.NHibernate/SAIF.NetCore.NHibernate/Extensions/IServicesCollectionExtensions.cs