Skip to content

ASP.NET dialect

@pbuilder/sdk/aspnet edits the .cs files of an ASP.NET Core application. Its handle has every op of @pbuilder/sdk/csharp plus addService, which registers a service in the dependency injection container. All ops chain on one handle and the file is written once; see Dialects for how a handle works.

Program.cs (before)
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.Run();
factory.ts
import * as aspnet from "@pbuilder/sdk/aspnet";
export const run = async () => {
await aspnet
.find("Program.cs")
.addService("Scoped", "IOrders", "SqlOrders", {
serviceFrom: "Shop.Application.Ports",
implementationFrom: "Shop.Infrastructure",
});
};
Program.cs (after)
using Shop.Application.Ports;
using Shop.Infrastructure;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddScoped<IOrders, SqlOrders>();
var app = builder.Build();
app.Run();

The handle carries every C# dialect op with the same rules: addUsing, addBaseType, addMember and addConstructorParameter. find(path) accepts the same .cs files, whatever they hold.

addService(lifetime, service, implementation, { serviceFrom?, implementationFrom?, method? })

Section titled “addService(lifetime, service, implementation, { serviceFrom?, implementationFrom?, method? })”

Writes <receiver>.Add<lifetime><service, implementation>();. lifetime is "Singleton", "Scoped" or "Transient", and service and implementation are each one C# type. The op edits one of two places.

Without method, it edits the top-level statements of Program.cs. The one local variable initialized by WebApplication.CreateBuilder, CreateSlimBuilder or CreateEmptyBuilder is the builder, and the receiver is <builder>.Services. The registration goes on the line after the last statement before the one that calls <builder>.Build(), as in the example above.

With method, it edits the one static method of that name whose first parameter is this IServiceCollection, such as the AddInfrastructure method each layer of a solution exposes. The receiver is that parameter. The registration goes before the method’s last statement when that is a return, and after it otherwise.

DependencyInjection.cs (before)
namespace Shop.Infrastructure;
public static class DependencyInjection
{
public static IServiceCollection AddInfrastructure(this IServiceCollection services)
{
return services;
}
}
factory.ts
await aspnet
.find("DependencyInjection.cs")
.addService("Scoped", "IOrders", "SqlOrders", { method: "AddInfrastructure" });
DependencyInjection.cs (after)
namespace Shop.Infrastructure;
public static class DependencyInjection
{
public static IServiceCollection AddInfrastructure(this IServiceCollection services)
{
services.AddScoped<IOrders, SqlOrders>();
return services;
}
}

serviceFrom and implementationFrom add the using of each type with the rules of the C# dialect’s from.

A registration of the same lifetime, service and implementation already in those statements leaves the file as it is, so a second run changes nothing:

Program.cs (unchanged)
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddScoped<IOrders, SqlOrders>();
var app = builder.Build();
factory.ts
await aspnet.find("Program.cs").addService("Scoped", "IOrders", "SqlOrders");

addService rejects with the dialect operation failed: prefix, naming addService, and leaves the file as it is, for:

  • a lifetime other than Singleton, Scoped or Transient, such as "Pooled";
  • a registration of the same service and implementation with another lifetime. On the Program.cs just above, addService("Singleton", "IOrders", "SqlOrders") rejects;
  • without method, top-level statements with no builder, or more than one, or no statement after the builder that calls Build(). A Program.cs that holds only Console.WriteLine("hi"); rejects;
  • with method, no such static extension method or more than one, or one with an expression body.

The op finds the place it edits when it runs, so find opens any .cs file. A file without that place fails only when addService runs.

As in the C# dialect, .modify(fn) hands fn the live CsharpDocument. Query its text with withTree and replace text. From @pbuilder/sdk 0.24.0, @pbuilder/sdk/aspnet re-exports withTree(text, fn) and isValidCsharp(text) from the C# dialect; for anything else, parse with astLibrary (web-tree-sitter) and the exported language. See Custom edits with .modify() for a worked example. Use it for registrations addService does not write, such as AddDbContext<ShopDb>(...) or a registration with a factory lambda.