LiteOrm.DependencyInjection
8.1.10
dotnet add package LiteOrm.DependencyInjection --version 8.1.10
NuGet\Install-Package LiteOrm.DependencyInjection -Version 8.1.10
<PackageReference Include="LiteOrm.DependencyInjection" Version="8.1.10" />
<PackageVersion Include="LiteOrm.DependencyInjection" Version="8.1.10" />
<PackageReference Include="LiteOrm.DependencyInjection" />
paket add LiteOrm.DependencyInjection --version 8.1.10
#r "nuget: LiteOrm.DependencyInjection, 8.1.10"
#:package LiteOrm.DependencyInjection@8.1.10
#addin nuget:?package=LiteOrm.DependencyInjection&version=8.1.10
#tool nuget:?package=LiteOrm.DependencyInjection&version=8.1.10
LiteOrm.DependencyInjection
📖 English Version
LiteOrm.DependencyInjection is the DI integration layer of LiteOrm. It switches the host container to Autofac, scans [AutoRegister] types, applies Castle DynamicProxy AOP interception, and explicitly registers the core services (data source provider, SQL builders, DAO context pool, session manager, entity services and DAOs) — so RegisterLiteOrm() brings up the whole LiteOrm stack automatically.
When to Use
- You want to register LiteOrm via
RegisterLiteOrm()and resolve services from the DI container. - You need AOP features (
[Transaction],[ServicePermission],[ServiceLog],[Log]) on your service methods. - You want automatic
[AutoRegister]scanning with Autofac lifetimes and interceptors. - You need automatic database schema synchronization at startup (
SyncTable).
Requirements
- .NET 8.0+ or .NET Standard 2.0 (.NET Framework 4.6.1+ compatible)
- Dependencies:
Autofac,Castle.Core,Castle.Core.AsyncInterceptor,LiteOrm,LiteOrm.Common
Installation
dotnet add package LiteOrm.DependencyInjection
LiteOrm.DependencyInjection transitively references LiteOrm and LiteOrm.Common, so you do not need to reference them separately.
Quick Start
Configure your data source in appsettings.json:
{
"LiteOrm": {
"Default": "main",
"DataSources": [
{
"Name": "main",
"ConnectionString": "Server=localhost;Database=MyDb;User Id=root;Password=123456;",
"Provider": "MySqlConnector.MySqlConnection, MySqlConnector"
}
]
}
}
Register LiteOrm in Program.cs (must be called on builder.Host, because it replaces the underlying DI container with Autofac):
using LiteOrm.DependencyInjection;
// Console app
var host = Host.CreateDefaultBuilder(args)
.RegisterLiteOrm()
.Build();
// ASP.NET Core app
var builder = WebApplication.CreateBuilder(args);
builder.Host.RegisterLiteOrm();
With options:
builder.Host.RegisterLiteOrm(options =>
{
options.Assemblies = new[] { typeof(MyService).Assembly };
options.RegisterSqlBuilder("main", new MySqlBuilder());
});
That is it — [AutoRegister] services, entity services (IEntityService<T>), view services (IEntityViewService<T>) and DAOs (IObjectDAO<T>, IObjectViewDAO<T>, IDataViewDAO<T>) are resolved from the container automatically.
Highlights
- Autofac container:
RegisterLiteOrm()switches the host to Autofac viaAutofacServiceProviderFactory; existingservices.AddXxx()registrations keep working through the MS DI bridge. - Deterministic core registration:
RegisterCoreServices()explicitly registersDataSourceProvider,SqlBuilderFactory,DAOContextPoolFactory,SessionManager, generic entity services and DAOs — no[AutoRegister]scanning for core types. - Auto-registration:
RegisterAutoService()scans assemblies and registers[AutoRegister]types with configurable lifetimes, keys, auto-activation,[InterceptAttribute]AOP wiring, and automaticServiceInvokeInterceptorfor types (or interfaces) carrying[Service]. - Scope tracking: scope tracking is always enabled;
RegisterScope()keepsSessionManager.Currentpointing at the correct lifetime scope across async contexts. - Schema sync:
LiteOrmCoreInitializer(aIHostedService) auto-creates tables/columns/indexes for entities on startup. - Service generation proxies:
AddServiceGenerator<T>()creates Castle DynamicProxy interface proxies that resolve their return values from the DI container.
❌AOT Support
LiteOrm.DependencyInjectiondepends on the Autofac container and dynamic proxies, which are not compatible with AOT.
🔄 Migrating to 8.1.1 (from 8.0.20 or earlier)
Versions 8.1.0 and 8.1.1 introduce several breaking changes. If you are upgrading from 8.0.20 or earlier, follow these steps:
1. Add the new LiteOrm.DependencyInjection package
RegisterLiteOrm() moved out of the LiteOrm core package into the new LiteOrm.DependencyInjection package, and the namespace changed from LiteOrm to LiteOrm.DependencyInjection:
<PackageReference Include="LiteOrm.DependencyInjection" Version="8.1.1" />
// Old (8.0.20 or earlier)
using LiteOrm;
// New (8.1.1)
using LiteOrm.DependencyInjection;
The RegisterLiteOrm() method signature is unchanged, so your call sites do not need modification.
2. Update BulkProvider usage (if you have custom providers)
BulkProviderFactory, BulkProviderAttribute and the [AutoRegister(Key = ...)] convention were removed. Assign your IBulkProvider directly to the SqlBuilder.BulkProvider property. GetSqlBuilder(typeof(MySqlConnection)) returns MySqlBuilder.Instance, so set it directly:
// Old: look up by connection type via the factory (removed)
var provider = services.GetRequiredService<BulkProviderFactory>().GetProvider(dbConnection.GetType());
// New: set it directly on the SqlBuilder
MySqlBuilder.Instance.BulkProvider = new MySqlBulkCopyProvider();
Upgrading to v8.1.1 (from v8.1.0 or lower)
If you are already on v8.1.0 and upgrading to v8.1.1, the changes are:
DAO constructors now require a
SessionManager.DAOBase,ObjectDAO<T>,ObjectViewDAO<T>,DataDAO<T>andDataViewDAO<T>take aSessionManagerparameter and no longer depend on the staticSessionManager.Current. Under DI (RegisterLiteOrm()/AddLiteOrm()) the container resolves it automatically — no code change. When constructing DAOs manually, pass the session:// Old (v8.1.0 and lower) var dao = new ObjectDAO<User>(); // New (v8.1.1) var dao = new ObjectDAO<User>(sessionManager);Custom DAOs deriving from a DAO base class must forward it in their constructor:
public MyDAO(SessionManager sessionManager) : base(sessionManager) { }.LiteOrmOptions.RegisterScopehas been removed. Scope tracking is always enabled automatically — remove anyoptions.RegisterScope = ...assignment.AddLiteOrm()bindsSessionManager.Currentautomatically per scope, so no middleware or manualSessionManager.SetCurrent(...)is needed.
3. FAQ
Q1: IEntityService<T> cannot be resolved from DI after upgrading?
Make sure the host uses RegisterLiteOrm() (from LiteOrm.DependencyInjection). Core types (EntityService<T>, ObjectDAO<T>, ...) are no longer registered via [AutoRegister] scanning; they are registered explicitly by RegisterCoreServices().
Q2: My custom services are still resolved by an interface without specifying ServiceTypes?
Yes. [AutoRegister]'s Policy defaults to RegisterPolicy.All, which registers both the implementation type itself and its non-System.* interfaces. Interface-injected custom services need no explicit Policy. Use RegisterPolicy.Interface for interface-only or Self for self-only registration.
Q3: Do my existing MS DI IServiceCollection registrations still work?
Yes. RegisterLiteOrm() bridges MS DI through AutofacServiceProviderFactory; existing services.AddXxx() registrations remain valid.
See the full 8.1 Upgrade Guide for details.
📖 中文版本
LiteOrm.DependencyInjection 是 LiteOrm 的 DI 集成层。它将宿主容器切换为 Autofac,扫描 [AutoRegister] 类型,应用 Castle DynamicProxy AOP 拦截,并显式注册核心服务(数据源提供程序、SQL 方言构建器、DAO 连接池工厂、会话管理器、实体服务与 DAO)——调用 RegisterLiteOrm() 即可一键拉起完整的 LiteOrm 技术栈。
适用场景
- 需要通过
RegisterLiteOrm()注册 LiteOrm,并从 DI 容器解析服务。 - 需要服务方法上的 AOP 能力(
[Transaction]、[ServicePermission]、[ServiceLog]、[Log])。 - 需要基于 Autofac 生命周期与拦截器的
[AutoRegister]自动扫描注册。 - 需要启动时自动同步数据库表结构(
SyncTable)。
环境要求
- .NET 8.0+ 或 .NET Standard 2.0(兼容 .NET Framework 4.6.1+)
- 依赖库:
Autofac、Castle.Core、Castle.Core.AsyncInterceptor、LiteOrm、LiteOrm.Common
安装
dotnet add package LiteOrm.DependencyInjection
LiteOrm.DependencyInjection 传递引用 LiteOrm 与 LiteOrm.Common,无需重复声明。
快速入门
在 appsettings.json 中配置数据源:
{
"LiteOrm": {
"Default": "main",
"DataSources": [
{
"Name": "main",
"ConnectionString": "Server=localhost;Database=MyDb;User Id=root;Password=123456;",
"Provider": "MySqlConnector.MySqlConnection, MySqlConnector"
}
]
}
}
在 Program.cs 中注册 LiteOrm(必须调用在 builder.Host 上,因为它需要替换底层 DI 容器为 Autofac):
using LiteOrm.DependencyInjection;
// 控制台应用
var host = Host.CreateDefaultBuilder(args)
.RegisterLiteOrm()
.Build();
// ASP.NET Core 应用
var builder = WebApplication.CreateBuilder(args);
builder.Host.RegisterLiteOrm();
带选项注册:
builder.Host.RegisterLiteOrm(options =>
{
options.Assemblies = new[] { typeof(MyService).Assembly };
options.RegisterSqlBuilder("main", new MySqlBuilder());
});
完成——[AutoRegister] 服务、实体服务(IEntityService<T>)、视图服务(IEntityViewService<T>)与 DAO(IObjectDAO<T>、IObjectViewDAO<T>、IDataViewDAO<T>)均可从容器自动解析。
主要特性
- Autofac 容器:
RegisterLiteOrm()通过AutofacServiceProviderFactory切换为 Autofac;已有的services.AddXxx()注册经 MS DI 桥接后仍然有效。 - 确定性核心注册:
RegisterCoreServices()显式注册DataSourceProvider、SqlBuilderFactory、DAOContextPoolFactory、SessionManager、泛型实体服务与 DAO,核心类型不再依赖[AutoRegister]扫描。 - 自动注册:
RegisterAutoService()扫描程序集,按可配置的生命周期、Key、自动激活、[InterceptAttribute]AOP 装配,以及带[Service]特性的类型(自动应用ServiceInvokeInterceptor)注册[AutoRegister]类型。 - 作用域跟踪:作用域跟踪,自动保证异步上下文中
SessionManager.Current始终指向正确的生命周期作用域。 - 表结构同步:
LiteOrmCoreInitializer(IHostedService)在启动时自动为实体创建表 / 列 / 索引。 - 服务生成代理:
AddServiceGenerator<T>()创建 Castle DynamicProxy 接口代理,返回值自动从 DI 容器解析。
❌AOT 支持
LiteOrm.DependencyInjection依赖于Autofac容器、动态代理等特性,不支持 AOT。
🔄 迁移到 8.1.1(从 8.0.20 及以下版本)
8.1.0 / 8.1.1 引入了若干破坏性变更。如果你正从 8.0.20 及以下版本升级,请按以下步骤操作:
1. 引用新的 LiteOrm.DependencyInjection 包
RegisterLiteOrm() 从 LiteOrm 核心包移至新增的 LiteOrm.DependencyInjection 包,命名空间由 LiteOrm 改为 LiteOrm.DependencyInjection:
<PackageReference Include="LiteOrm.DependencyInjection" Version="8.1.1" />
// 旧(8.0.20 及以下版本)
using LiteOrm;
// 新(8.1.1)
using LiteOrm.DependencyInjection;
RegisterLiteOrm() 方法签名不变,调用方式无需改动。
2. 更新 BulkProvider 使用方式(如有自定义实现)
BulkProviderFactory、BulkProviderAttribute 与 [AutoRegister(Key = ...)] 标记方式均已移除。将 IBulkProvider 直接设置到 SqlBuilder.BulkProvider 属性。GetSqlBuilder(typeof(MySqlConnection)) 返回的就是 MySqlBuilder.Instance,直接对其设置即可:
// 旧:通过工厂按连接类型查找(已移除)
var provider = services.GetRequiredService<BulkProviderFactory>().GetProvider(dbConnection.GetType());
// 新:直接设置到 SqlBuilder.BulkProvider
MySqlBuilder.Instance.BulkProvider = new MySqlBulkCopyProvider();
升级到 8.1.1(从 8.1.0 及更低版本)
如果你已处于 v8.1.0,升级到 v8.1.1 的变更如下:
DAO 构造函数现在需要
SessionManager。DAOBase、ObjectDAO<T>、ObjectViewDAO<T>、DataDAO<T>、DataViewDAO<T>构造函数接收SessionManager参数,不再依赖静态SessionManager.Current。使用RegisterLiteOrm()/AddLiteOrm()时由 DI 容器自动解析,无需改动;手动构造 DAO 时需传入会话:// 旧(v8.1.0 及更低) var dao = new ObjectDAO<User>(); // 新(v8.1.1) var dao = new ObjectDAO<User>(sessionManager);自定义 DAO 若继承自 DAO 基类,构造函数需传入并转发:
public MyDAO(SessionManager sessionManager) : base(sessionManager) { }。LiteOrmOptions.RegisterScope选项已移除。 作用域跟踪始终自动启用,请删除options.RegisterScope = ...赋值。AddLiteOrm()自动按作用域绑定SessionManager.Current,无需中间件或手动SessionManager.SetCurrent(...)。
3. 常见问题
Q1:升级后 IEntityService<T> 无法从 DI 解析?
确认宿主使用了 RegisterLiteOrm()(来自 LiteOrm.DependencyInjection)。核心类型(EntityService<T>、ObjectDAO<T> 等)不再通过 [AutoRegister] 扫描注册,而是由 RegisterCoreServices() 显式注册。
Q2:我的业务服务未显式指定 ServiceTypes,还能通过接口解析吗?
可以。[AutoRegister] 的 Policy 默认值为 RegisterPolicy.All,会同时注册实现类型自身及其非 System.* 命名空间接口,依赖接口注入的服务无需显式声明 Policy。需要仅注册接口时用 RegisterPolicy.Interface,仅注册自身时用 Self。
Q3:原来用 MS DI 的 IServiceCollection 注册的服务还能用吗?
可以。RegisterLiteOrm() 内部使用 AutofacServiceProviderFactory 桥接 MS DI,已有的 services.AddXxx() 注册仍然有效。
详细说明见完整 8.1 升级指南。
📚 相关资源 / Resources
- LiteOrm 主仓库 / Main Repository
- 配置与注册文档 / Configuration & Registration Docs
- 8.1 升级指南 / 8.1 Upgrade Guide
- Demo 项目 / Demo Project
📄 License
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 is compatible. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- Autofac.Extensions.DependencyInjection (>= 10.0.0)
- Autofac.Extras.DynamicProxy (>= 7.1.0)
- Castle.Core (>= 5.1.1)
- Castle.Core.AsyncInterceptor (>= 2.1.0)
- LiteOrm (>= 8.1.10)
- LiteOrm.Common (>= 8.1.10)
- Microsoft.Extensions.DependencyInjection (>= 2.2.0)
- Microsoft.Extensions.Hosting (>= 2.2.0)
- Microsoft.Extensions.Hosting.Abstractions (>= 2.2.0)
- Microsoft.Extensions.Logging.Abstractions (>= 2.2.0)
-
.NETStandard 2.1
- Autofac.Extensions.DependencyInjection (>= 10.0.0)
- Autofac.Extras.DynamicProxy (>= 7.1.0)
- Castle.Core (>= 5.1.1)
- Castle.Core.AsyncInterceptor (>= 2.1.0)
- LiteOrm (>= 8.1.10)
- LiteOrm.Common (>= 8.1.10)
- Microsoft.Extensions.DependencyInjection (>= 2.2.0)
- Microsoft.Extensions.Hosting (>= 2.2.0)
- Microsoft.Extensions.Hosting.Abstractions (>= 2.2.0)
- Microsoft.Extensions.Logging.Abstractions (>= 2.2.0)
-
net10.0
- Autofac.Extensions.DependencyInjection (>= 10.0.0)
- Autofac.Extras.DynamicProxy (>= 7.1.0)
- Castle.Core (>= 5.1.1)
- Castle.Core.AsyncInterceptor (>= 2.1.0)
- LiteOrm (>= 8.1.10)
- LiteOrm.Common (>= 8.1.10)
- Microsoft.Extensions.DependencyInjection (>= 10.0.0)
- Microsoft.Extensions.Hosting (>= 10.0.0)
- Microsoft.Extensions.Hosting.Abstractions (>= 10.0.0)
- Microsoft.Extensions.Logging.Abstractions (>= 10.0.0)
-
net8.0
- Autofac.Extensions.DependencyInjection (>= 10.0.0)
- Autofac.Extras.DynamicProxy (>= 7.1.0)
- Castle.Core (>= 5.1.1)
- Castle.Core.AsyncInterceptor (>= 2.1.0)
- LiteOrm (>= 8.1.10)
- LiteOrm.Common (>= 8.1.10)
- Microsoft.Extensions.DependencyInjection (>= 8.0.0)
- Microsoft.Extensions.Hosting (>= 8.0.0)
- Microsoft.Extensions.Hosting.Abstractions (>= 8.0.0)
- Microsoft.Extensions.Logging.Abstractions (>= 8.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
8.1.10:
- SyncTable = Never 的实体在启动同步时不参与建表,也不预加载表信息。
- 同步 LiteOrm.Common 8.1.10 / LiteOrm 8.1.10。