Last active
June 30, 2021 07:12
-
-
Save gistlyn/f10257ccf051763da336dba4618fb456 to your computer and use it in GitHub Desktop.
db-connection
This file contains hidden or 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 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" | |
}); | |
} | |
} | |
} |
This file contains hidden or 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 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