ServiceStack ORMLite is great, I've typically steered clear of the ORM mentality preferring to build databases as it makes sense to build databases instead of a 1:1 class model. That said, there are a couple of things that I seem to be running into difficulty around, I'm certain it's simply my ignorance shining through.
First:
Is there a way to manage multiple result sets using ORMLite? I know that one can use the QueryMultiple method using Dapper, but for whatever reason I'm having a bear of a time figuring out how to use the built-in Dapper implementation of ServiceStack.
Second:
Is there a way using ORMLite to return output parameters within a stored procedure call?
Ideally, I'd like to steer clear of MARS and output parameters and ideally I'd like to live in an ideal world :)
I'm using .NET framework 4.5, SQL Server 2008 R2 and ServiceStack 3.9.46.
Multiple Active Result Sets (MARS) is a feature that allows the execution of multiple batches on a single connection. In previous versions, only one batch could be executed at a time against a single connection. Executing multiple batches with MARS does not imply simultaneous execution of operations.
A batch or stored procedure which starts a manual or implicit transaction when MARS is enabled must complete the transaction before the batch exits. If it does not, SQL Server rolls back all changes made by the transaction when the batch finishes.
Multiple Active Result Sets (MARS) is a feature that works with SQL Server to allow the execution of multiple batches on a single connection. When MARS is enabled for use with SQL Server, each command object used adds a session to the connection.
Using Multiple Active Result Sets (MARS), you can maintain multiple pending requests on a single SQL Server connection. This allows you to issue multiple SQL statements or batch statements against a single connection, which eliminates the overhead of opening and closing the connection for each.
It turns out that this is really quite simple (provided you know the magic to make it happen).
Based on the documentation and a seemingly misleading post indicating that Dapper is "included" in razor I assumed that when it was implied that Dapper was "built-in" that it was essentially a part of the included libraries.
Laugh if you will, but for those of us that aren't enlightened, I'm going to outline how to make the Dapper extensions show up. So here's the magic.
Using the Package Manager console execute the following:
Install-Package ServiceStack
Install-Package Dapper
Add the following using statements (C#) to your Service:
using ServiceStack.OrmLite;
using Dapper;
Now, when you leverage the Db object all the OrmLite AND Dapper methods will be there.
To get an output parameter it is now as simple as:
var p = new DynamicParameters();
p.Add("@param1", request.stuff1);
p.Add("@param2", request.stuff2);
p.Add("@param3", dbType: DbType.Int32, direction: ParameterDirection.Output);
Db.Execute("schema.sp_stored_proc_name", p, commandType: CommandType.StoredProcedure);
response.outputStuff = p.Get<int>("@param3");
In order to manage MARS (assume you have a SP that returns two result sets AND an output param):
p.Add("@param1", request.stuff1);
p.Add("@param2", request.stuff2);
p.Add("@param3", dbType: DbType.Int32, direction: ParameterDirection.Output);
var mars = Db.QueryMultiple("schema.sp_stored_proc_name", p, commandType: CommandType.StoredProcedure);
//firstSet contains the first result set
var firstSet = mars.Read().ToList();
//secondSet contains the second result set
var secondSet = mars.Read().ToList();
response.outputStuff = p.Get<int>("param3");
It's beautifully simple, once you know the magic :)
Here's a much more complicated example.
Hopefully this helps someone else out and saves them a bit of time.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With