目前,我正在使用 OrmLite 进行数据库操作。我也计划使用 Dapper ORM,但谁能指出我如何将 DapperORM 集成到 ServiceStack 中。我是否需要使用 Dapper 和插件在容器中实现 IDbConnection 和 IDbConnectionFactory 接口(interface)。
public override void Configure(Container container) {
container.Register<IDbConnectionFactory>(
c => new OrmLiteConnectionFactory(ConfigurationManager.ConnectionStrings["default"].ConnectionString,
SqlServerDialect.Provider));
container.Register<IDbConnection>(c => c.Resolve<IDbConnectionFactory>().OpenDbConnection()).ReusedWithin(ReuseScope.Request);
}
最佳答案
Dapper 的工作方式与 OrmLite 类似,因为它们都是底层 ADO.NET System.Data.*
上的扩展方法。 IDbConnection界面。这意味着您可以在 上同时使用它们。 IDb连接从 OrmLite 的 IDbConnectionFactory 中检索。
OrmLite 在 ServiceStack.OrmLite 中包含最新版本的 Dapper下ServiceStack.OrmLite.Dapper
命名空间。
在 ServiceStack v3.9.40 中,Dapper 的这个嵌入式版本将其 API 更改为包含“Dapper”后缀,以避免与同名的 OrmLite 方法发生冲突。你 不要必须将 Request-Scoped IDbConnection 注册为 ServiceStack 的默认值 Service类已经为您从 IDbConnectionFactory 检索它。
鉴于此,以下是在 ServiceStack 服务中访问 Dapper ORM API 的方法:
public class MyService : Service
{
public object Any(Request request)
{
base.Db.QueryDapper(...);
base.Db.QueryMultipleDapper(...);
base.Db.MultiMapDapper(...);
base.Db.ExecuteDapper(...);
}
}
关于servicestack - 如何在 ServiceStack 中使用 Dapper,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15487000/