Skip to content

Instantly share code, notes, and snippets.

@labilbe
Last active October 25, 2015 12:03
Show Gist options
  • Save labilbe/8653bb53383478870546 to your computer and use it in GitHub Desktop.
Save labilbe/8653bb53383478870546 to your computer and use it in GitHub Desktop.
WebAppHost sample for ServiceStack
public abstract class AppServiceBase : ServiceStack.Service
{
private ISupplierService _supplierService;
internal ISupplierService SupplierService
{
get { return _supplierService ?? (_supplierService = TryResolve<ISupplierService>()); }
set { _supplierService = value; }
}
internal CustomUserSession UserSession
{
get { return base.SessionAs<CustomUserSession>(); }
}
}
public abstract class DbServiceBase<T> : AppServiceBase where T : IDbConnectionFactory
{
private IDbConnection _db;
public override IDbConnection Db
{
get
{
return _db ?? (_db = TryResolve<T>().OpenDbConnection());
}
}
public override void Dispose()
{
if (_db != null)
{
_db.Dispose();
}
base.Dispose();
}
}
public class GuepardDbConnectionFactory : OrmLiteConnectionFactory, IGuepardDbConnectionFactory
{
public GuepardDbConnectionFactory(string connectionString)
: base(connectionString, SqlServerDialect.Provider)
{
}
}
public interface IGuepardDbConnectionFactory : IDbConnectionFactory
{
}
public interface ILoggingDbConnectionFactory : IDbConnectionFactory
{
}
public interface ISupplierService : IDisposable
{
IList<Supplier> Get(AllSuppliers request);
}
public class LoggingDbConnectionFactory : OrmLiteConnectionFactory, ILoggingDbConnectionFactory
{
public LoggingDbConnectionFactory(string connectionString)
: base(connectionString, SqlServerDialect.Provider)
{
}
}
[RequiresGeckoRole(GeckoRoles.OperatorPurchase)]
public class SupplierService : ValidationServiceBase<Supplier, IGuepardDbConnectionFactory>, ISupplierService
{
public IList<Supplier> Get(AllSuppliers request)
{
var ev = Db.From<Supplier>().OrderBy(p => new { p.Name, p.Id });
var suppliers = Db.Select(ev);
return suppliers;
}
}
public abstract class ValidationServiceBase<T, TU> : DbServiceBase<TU> where T : IReturn where TU : IDbConnectionFactory
{
public IValidator<T> Validator { get; set; }
}
public class WebAppHost : AppHostBase, IAppHost
{
public WebAppHost(HttpApplication httpApplication) : base("My Web Service", typeof(SupplierService).Assembly)
{
}
public override void Configure(Container container)
{
// Register connection strings
container.Register<IGuepardDbConnectionFactory>(
new GuepardDbConnectionFactory(ConfigurationManager.ConnectionStrings["guepard"].ConnectionString)
{
ConnectionFilter = x => new ProfiledDbConnection(x, Profiler.Current)
});
container.Register<ILoggingDbConnectionFactory>(
new LoggingDbConnectionFactory(ConfigurationManager.ConnectionStrings["gecko-log"].ConnectionString)
{
ConnectionFilter = x => new ProfiledDbConnection(x, Profiler.Current)
});
container.RegisterAs<SupplierService, ISupplierService>();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment