.NET Aspire is a formidable tool to visualize your components and relation between them . Today I will show you how to add a custom visualizer for SqlServer database .
The code for adding a database is pretty simple
var paramPass = builder.AddParameter("password","P@ssw0rd");
var sqlserver = builder.AddSqlServer("sqlserver",paramPass,1433)
.WithDbGate()
;
var db = sqlserver.AddDatabase("NewDB")
;
The community extension,https://github.com/CommunityToolkit/Aspire,has already an extension,WithDBGate,that adds a viewer for the whole SqlServer . But I want something faster,that adds just for the database . So SqlPad,https://getsqlpad.com/,that has also a docker container,will be enough. So this is the code
public static class SqlServerExtensions
{
/// <summary>
///
/// </summary>
/// <param name="db"></param>
/// <param name="sqlserver"></param>
/// <returns></returns>
public static IResourceBuilder<SqlServerDatabaseResource> WithSqlPadViewerForDB(this IResourceBuilder<SqlServerDatabaseResource> db,IResourceBuilder<SqlServerServerResource> sqlserver)
{
var builder = db.ApplicationBuilder;
var sqlpad = builder
.AddContainer("sqlpad","sqlpad/sqlpad:latest")
.WithEndpoint(5600,3000,"http")
.WithEnvironment("SQLPAD_AUTH_DISABLED","true")
.WithEnvironment("SQLPAD_AUTH_DISABLED_DEFAULT_ROLE","Admin")
.WithEnvironment("SQLPAD_ADMIN","admin@sqlpad.com")
.WithEnvironment("SQLPAD_ADMIN_PASSWORD","admin")
.WithEnvironment("SQLPAD_CONNECTIONS__sqlserverdemo__name",sqlserver.Resource.Name)
.WithEnvironment("SQLPAD_CONNECTIONS__sqlserverdemo__driver","sqlserver")
.WithEnvironment("SQLPAD_CONNECTIONS__sqlserverdemo__host",sqlserver.Resource.Name)
.WithEnvironment("SQLPAD_CONNECTIONS__sqlserverdemo__database",db.Resource.Name)
.WithEnvironment("SQLPAD_CONNECTIONS__sqlserverdemo__username","sa")
.WithEnvironment("SQLPAD_CONNECTIONS__sqlserverdemo__password",sqlserver.Resource.PasswordParameter.Value)
.WithEnvironment("SQLPAD_CONNECTIONS__sqlserverdemo1__name","SqlMaster")
.WithEnvironment("SQLPAD_CONNECTIONS__sqlserverdemo1__driver","sqlserver")
.WithEnvironment("SQLPAD_CONNECTIONS__sqlserverdemo1__host",sqlserver.Resource.Name)
.WithEnvironment("SQLPAD_CONNECTIONS__sqlserverdemo1__database","master")
.WithEnvironment("SQLPAD_CONNECTIONS__sqlserverdemo1__username","sa")
.WithEnvironment("SQLPAD_CONNECTIONS__sqlserverdemo1__password",sqlserver.Resource.PasswordParameter.Value)
.WithParentRelationship(db)
.WaitFor(db)
.WaitFor(sqlserver)
;
return db;
}
}
And the code for adding is
var db = sqlserver.AddDatabase("NewDB") .WithSqlPadViewerForDB(sqlserver);
Leave a Reply