Hi Daniel,
You pass around a IDbConnectionFactory (which is itself a singleton) because it itself doesn't hold a db connection open, so it's safe to reference forever. Whenever you need to use it you just open the DB Connection.
One way we can make this more DRY is to have a Lazy property in an IDisposable base class, see the implementation of ServiceStack's New API
Service as an example:
private IDbConnection db;
public virtual IDbConnection Db
{
get { return db ?? (db = TryResolve<IDbConnectionFactory>().Open()); }
}
Then in your Dispose method you can just close it if it's been opened:
public virtual void Dispose()
{
if (db != null)
db.Dispose();
}
Which lets you avoid the using statement, e.g:
public HelloService : Service
{
public object Get(Customer request)
{
return Db.Id<Customer>(request.Id);
}
}
This works as by default ServiceStack will automatically dispose of any IDisposable services just after your service has been executed.
Cheers,