Last active
October 25, 2015 12:03
-
-
Save labilbe/8653bb53383478870546 to your computer and use it in GitHub Desktop.
WebAppHost sample for ServiceStack
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>(); } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class GuepardDbConnectionFactory : OrmLiteConnectionFactory, IGuepardDbConnectionFactory | |
{ | |
public GuepardDbConnectionFactory(string connectionString) | |
: base(connectionString, SqlServerDialect.Provider) | |
{ | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface IGuepardDbConnectionFactory : IDbConnectionFactory | |
{ | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface ILoggingDbConnectionFactory : IDbConnectionFactory | |
{ | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface ISupplierService : IDisposable | |
{ | |
IList<Supplier> Get(AllSuppliers request); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class LoggingDbConnectionFactory : OrmLiteConnectionFactory, ILoggingDbConnectionFactory | |
{ | |
public LoggingDbConnectionFactory(string connectionString) | |
: base(connectionString, SqlServerDialect.Provider) | |
{ | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public abstract class ValidationServiceBase<T, TU> : DbServiceBase<TU> where T : IReturn where TU : IDbConnectionFactory | |
{ | |
public IValidator<T> Validator { get; set; } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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