Skip to content

Instantly share code, notes, and snippets.

@gistlyn
Last active June 30, 2021 07:12
Show Gist options
  • Save gistlyn/f10257ccf051763da336dba4618fb456 to your computer and use it in GitHub Desktop.
Save gistlyn/f10257ccf051763da336dba4618fb456 to your computer and use it in GitHub Desktop.
db-connection
public class AppHost : AppHostBase
{
public AppHost() : base("Web",typeof(MyServices).Assembly){}
public override void Configure(Container container)
{
var connectionString =
"Server=127.0.0.1;Port=5432;" +
"Database=myDataBase;User Id=myUsername;" +
"Password=myPassword;";
container.Register<IDbConnectionFactory>(
new OrmLiteConnectionFactory(
connectionString,
PostgreSqlDialect.Provider));
using var db = container
.Resolve<IDbConnectionFactory>().Open();
if (db.CreateTableIfNotExists<MyTable>())
{
db.Insert(new MyTable {
Name = "Seed Data for new MyTable"
});
}
}
}
public class MyServices : Service
{
public object Any(Hello request)
{
// Db property opens a new connection per request
var user = Db.Single<User>(x => x.Id == request.Id);
return new HelloResponse {
Result = $"Hello, {user.Name}!"
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment