This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
Apstory.Scaffold is a code generation tool that follows a database-first approach for MSSQL databases. It generates CRUD stored procedures and corresponding C# code using convention-over-configuration methodology. The repository contains three main components:
- Apstory.Scaffold.App - .NET 8.0 CLI tool (distributed as a dotnet global tool)
- Apstory.Scaffold.VisualStudio - Visual Studio extension
- Apstory-Scaffold-VSCode - VSCode extension (TypeScript)
The .NET solution is organized into three layers:
- App/ - Contains the CLI application and IDE extensions
Apstory.Scaffold.App/- Main CLI tool (dotnet tool)Apstory.Scaffold.VisualStudio/- Visual Studio extensionApstory-Scaffold-VSCode/- VSCode extension
- Domain/ - Business logic and code generation
Apstory.Scaffold.Domain/Parser/- SQL and TypeScript parsersApstory.Scaffold.Domain/Scaffold/- Code generation scaffoldsApstory.Scaffold.Domain/Service/- Core servicesApstory.Scaffold.Domain/Util/- Utility classes
- Model/ - Data models and configuration classes
Apstory.Scaffold.Model/Config/- Configuration modelsApstory.Scaffold.Model/Sql/- SQL model representationsApstory.Scaffold.Model/Typescript/- TypeScript model representations
- The tool searches for a
.sqlprojfile and extracts the project namespace - Parsers (
SqlTableParser,SqlProcedureParser) analyze SQL table definitions and stored procedures - Scaffold classes generate code in a layered architecture:
- SQL Procedures:
DelHrd,DelSft,GetByTableNameIds,GetById,GetByIds,GetByIdsPaging,InsUpd - Models:
ProjectName.Model/Gen/TableName.Gen.cs - Repository Layer:
ProjectName.Dal.Dapper/Gen/TableNameRepository.Gen.csand interfaces - Service Layer:
ProjectName.Domain/Gen/TableNameService.Gen.cswith foreign key extensions - DI Registration: ServiceCollectionExtension classes for repository and service registration
- SQL Procedures:
The CLI uses a worker pattern (BackgroundService) for different operations:
SqlScaffoldWatcherWorker- File system watcher mode (default)SqlScaffoldRegenerationWorker- Regeneration mode (-regen)SqlScaffoldDeleteWorker- Deletion mode (-delete)SqlUpdateWorker- SQL push mode (-sqlpush)TypescriptSearchPageWorker- Angular search page generation (-ngsearchpage)SqlLiteWorker- SQLite repository generation (-tsdalfolder)
# Build the solution
dotnet build Apstory.Scaffold.sln
# Build specific project
dotnet build App/Apstory.Scaffold.App/Apstory.Scaffold.App.csproj
# Pack and install globally (for testing)
cd App/Apstory.Scaffold.App
dotnet pack
dotnet tool update Apstory.Scaffold.App --globalcd App/Apstory-Scaffold-VSCode
# Install dependencies
npm install
# Type checking
npm run check-types
# Lint
npm run lint
# Compile (development)
npm run compile
# Watch mode (for development)
npm run watch
# Package for distribution
npm run packagePress F5 in VSCode to launch the extension in debug mode.
# Install VSCE globally (if not already installed)
npm install -g @vscode/vsce
# Create .vsix file
cd App/Apstory-Scaffold-VSCode
vsce packThe generated .vsix file can be installed by dragging it into VSCode's Extensions tab.
dotnet tool update Apstory.Scaffold.App --globalBy default, running Apstory.Scaffold.App searches for a .sqlproj file and enters watch mode:
Apstory.Scaffold.App-sqlproject <path>- Override SQL project path-namespace <name>- Override namespace-regen [schema|table|procedure]- Regenerate code (e.g.,dbo,dbo.Customer,dbo;dbo.Orders)-delete [schema|table|procedure]- Delete generated code-sqlpush [table|procedure]- Push SQL changes to database (requires-sqldestination)-sqldestination <connectionstring>- Database connection string for push operations-variant <type>- Generation variants (e.g.,mergefor custom ID inserts)-tsmodel <path>- TypeScript model path for SQLite generation-ngsearchpage- Generate Angular search page-tsdalfolder- Generate TypeScript DAL service-help- Show all available commands
- Generated stored procedures follow naming pattern:
zgen_[TableName]_[Operation] - Only one of these switches can be used at a time:
-regen,-delete,-sqldestination,-ngsearchpage,-tsdalfolder
Stored procedures can specify custom return types using the @ReturnType annotation at the beginning of the procedure:
-- @ReturnType: CustomerSummary
CREATE PROCEDURE [dbo].[zgen_Customer_GetSummary]
@CustomerId UNIQUEIDENTIFIER
AS
BEGIN
SELECT * FROM CustomerSummary WHERE CustomerId = @CustomerId
ENDBehavior:
- When a custom return type is specified, the generated methods will always return a list of that type (
List<CustomReturnType>) - Without the annotation, the default behavior uses the table name derived from the procedure name
- The custom return type must be a valid model in the target namespace
- This works across all generated layers: Repository, Repository Interface, Service, Service Interface, and Foreign Service layers
Example:
-- @ReturnType: OrderDetails
CREATE PROCEDURE [dbo].[zgen_Order_GetDetailsByCustomer]
@CustomerId UNIQUEIDENTIFIER
AS
BEGIN
-- Complex query returning OrderDetails model instead of Order
ENDThis generates methods like:
- Repository:
Task<List<OrderDetails>> GetOrderDetailsByCustomer(Guid customerId) - Service:
Task<List<OrderDetails>> GetOrderDetailsByCustomer(Guid customerId)
- All generated files include
.Gen.cssuffix to distinguish them from custom code - Generated code should not be manually edited as it will be overwritten
- The tool searches for a
.sqlprojfile, preferring paths containing/DB/or\DB\when multiple projects exist
The tool uses CSharpConfig model which is derived from:
- The
.sqlprojfile's<RootNamespace>element (with.DBremoved) - Or the
-namespaceoverride parameter
The VSCode extension provides context menu commands for:
extension.generateSQLiteRepo- Generate SQLite Repository (TypeScript files)apstoryScaffold.runCodeScaffold- Run Code Scaffold (SQL files)apstoryScaffold.pushSqlChanges- Push SQL Changes (SQL files)apstoryScaffold.configure- Configure settingsapstoryScaffold.delete- Delete generated code (SQL files)
Commands are registered in extension.ts and organized into:
Configuration is managed via config-util.ts.
- .NET 8.0
- Dapper (data access)
- Microsoft.Data.SqlClient (SQL Server connectivity)
- Microsoft.Extensions.Hosting (worker pattern)
- Microsoft.CodeAnalysis.CSharp (code generation)
- TypeScript 5.7+
- VSCode Engine 1.97.0+
- esbuild (bundling)
- ESLint (linting)