10

Currently, I am using OrmLite for DB operations. I am also planning to use Dapper ORM, but can anyone point me how to integrate DapperORM in ServiceStack. Do I need to implement both IDbConnection and IDbConnectionFactory interfaces with Dapper and plugin into the container.

  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);

      }
Sunny
  • 4,765
  • 5
  • 37
  • 72

2 Answers2

10

Dapper works like OrmLite in that they're both extension methods over the underlying ADO.NET System.Data.* IDbConnection interface. This means you can use both of them together on the IDbConnection retrieved from OrmLite's IDbConnectionFactory.

OrmLite includes a recent version of Dapper in ServiceStack.OrmLite under the ServiceStack.OrmLite.Dapper namespace.


In v3.9.40 of ServiceStack, this embedded version of Dapper had their APIs changed to include a 'Dapper' suffix to avoid clashing with OrmLite methods of the same name. You don't have to register a Request-Scoped IDbConnection as ServiceStack's default Service class already retrieves it from the IDbConnectionFactory for you.

Given that, here's how you can access Dapper ORM APIs inside a ServiceStack service:

public class MyService : Service
{
    public object Any(Request request)
    {
        base.Db.QueryDapper(...);
        base.Db.QueryMultipleDapper(...);
        base.Db.MultiMapDapper(...);
        base.Db.ExecuteDapper(...);
    }
}
mythz
  • 141,670
  • 29
  • 246
  • 390
  • Thank you. And will it have any issues referencing razor library, my application is just API with rest services. – Sunny Mar 19 '13 at 01:10
  • No, it's just a library which doesn't do anything if you don't enable the RazorFeature plugin. – mythz Mar 19 '13 at 02:20
  • as v3.9.44 is missing from ServiceStack.Razor NuGet package, where is now ? – Anton Hasan May 16 '13 at 15:04
  • This answer appears to no longer be correct. The link to ServiceStack.Razor.Dapper results in a 404. I am unable, at present, to find a way to use "built-in" Dapper. Is Dapper no longer a part of ServiceStack? – Ram Argid May 26 '13 at 17:05
5

I don't know about others, but due to the information regarding Dapper within the Razor namespace this answer confused me quite a bit.

In case others have a similar experience, I've attempted to answer this same question in a basic way.

Community
  • 1
  • 1
Ram Argid
  • 253
  • 3
  • 8