Scribbly.Stencil.Generator 0.1.3

Suggested Alternatives

Scribbly.Stencil 0.1.4

Additional Details

Scribbly.Stencil.Generator is now part of Scribbly.Stencil and should be removed from your project starting with version 0.1.4

dotnet add package Scribbly.Stencil.Generator --version 0.1.3
                    
NuGet\Install-Package Scribbly.Stencil.Generator -Version 0.1.3
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Scribbly.Stencil.Generator" Version="0.1.3" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Scribbly.Stencil.Generator" Version="0.1.3" />
                    
Directory.Packages.props
<PackageReference Include="Scribbly.Stencil.Generator" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Scribbly.Stencil.Generator --version 0.1.3
                    
#r "nuget: Scribbly.Stencil.Generator, 0.1.3"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Scribbly.Stencil.Generator@0.1.3
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Scribbly.Stencil.Generator&version=0.1.3
                    
Install as a Cake Addin
#tool nuget:?package=Scribbly.Stencil.Generator&version=0.1.3
                    
Install as a Cake Tool

scribbly_banner.png

GitHub GitHub all releases Nuget GitHub issues GitHub Repo stars GitHub forks GitHub last commit (branch)

Scribbly Stencil

A framework for organizing and generating minimal API endpoints.

Static Badge Static Badge Static Badge

hero_vid.gif

Table of Contents

  1. 🎁 Packages
  2. 🎯 Endpoints
  3. 🛒 Groups
  4. 🥣 Cookbook

Example

Below is a brief snip of code to get you started before reading more.

  1. Add a reference to the Scribbly.Stencil package and annotate a static method on a partial class.
[EndpointGroup("/lunch")]
public partial class LunchGroup
{
    // Annotate any method signature with the VerbEndpoint attribute
    [GetEndpoint("/{id}", "Get Lunch", "Fetches the current lunch for the provided restaurant ID.")]
    private static IResult GetLunchMenu(HttpContext context, string id)
    {
        return Results.Ok(id);
    }
    
    [PostEndpoint("/{id}", "Get Lunch", "Fetches the current lunch for the provided restaurant ID.")]
    private static IResult PostLunchMenu(HttpContext context, string id, LunchItem request)
    {
        return Results.Ok(request);
    }
}

Scribbly.Stencil will generate a few things for you behind the Scenes

  1. An HTTP Endpoint Registration Method
public static IEndpointRouteBuilder MapLunchGroupGetLunchMenu(this IEndpointRouteBuilder builder)
{
    builder.MapPost("/{id}", PostLunchMenu);
    return builder;
}
  1. And an Endpoint Registry for All Endpoints Mapping endpoints to Groups
public static IEndpointRouteBuilder MapScribblyEndpoints(this IEndpointRouteBuilder builder)
{
    builder.MapLunchGroupGetLunchMenu();
    builder.MapLunchGroupPostLunchMenu();
    return builder;
} 

  1. Simply Map Your Application
app.MapStencilApp();

Optionally Add a Group or Route Prefix

app.MapStencilApp("/api");

🎯 Endpoints

With Scribbly.Stencil endpoints are declared as a static method and automatically mapped to an HTTP request. The generator will accept several type configurations however the enclosing class MUST be a public partial class. The method signature MAY be private or public, static or an instance member..

public partial class BreakfastEndpoints
{
    [PutEndpoint("/breakfast/{id}", "Edit Breakfast", "Edits a Breakfast Item")]
    private object PutBreakfastMenu(HttpContext context, [FromRoute] string id)
    {
        return new { id = id };
    }
}

Any method signature supported by a Minimal API Endpoint is fine and support by Scribbly.Stencil the rest o f

public partial class BreakfastEndpoints
{
    [PutEndpoint("/breakfast/{id}", "Edit Breakfast", "Edits a Breakfast Item")]
    private static Task<IResult> PutBreakfastMenu([FromQuery] string id)
    {
    }
    
    [PutEndpoint("/breakfast/{id}", "Edit Breakfast", "Edits a Breakfast Item")]
    private static Task<string> PutBreakfastMenu([FromServices] IService service)
    {
    }
}

The code generator will kick into action when it finds a method annotated with any of the EndpointAttributes

Static Badge

Static Badge

Static Badge

Static Badge

once our APIs are stable support for more HTTP verbs will be added.

Types can declare multiple endpoints

public partial class BreakfastEndpoints
{
    [GetEndpoint("/breakfast/{id}", "Edit Breakfast", "Edits a Breakfast Item")]
    private static object GetBreakfastMenu(HttpContext context, [FromRoute] string id)
    {
    }
    
    [PutEndpoint("/breakfast/{id}", "Edit Breakfast", "Edits a Breakfast Item")]
    private static object PutBreakfastMenu(HttpContext context, [FromRoute] string id)
    {
    }
}

Endpoints can also be grouped together with a parent IEndpointRouteBuilder see groups below for details


[EndpointGroup("/breakfast", "Manage Breakfast Menu")]
public partial class BreakfastGroup
{
}

[GroupMember<BreakfastGroup>]
public partial class BreakfastEndpoints
{
    [GetEndpoint("/{id}", "Edit Breakfast", "Edits a Breakfast Item")]
    private static object GetBreakfastMenu(HttpContext context, [FromRoute] string id)
    {
    }
    
    [PutEndpoint("/{id}", "Edit Breakfast", "Edits a Breakfast Item")]
    private static object PutBreakfastMenu(HttpContext context, [FromRoute] string id)
    {
    }
}

Group membership is Type Safe as the Generic Parameter can ONLY accept a Scribbly.Stencil.IGroup interface. Membership can be added to endpoints at either the method declaration or the class declartion.


[EndpointGroup("/breakfast", "Manage Breakfast Menu")]
public partial class BreakfastGroup
{
}

[EndpointGroup("/menu", "Manage Breakfast Menu")]
public partial class MenuGroup
{
}

[GroupMember<BreakfastGroup>]
public partial class BreakfastEndpoints
{
    [GetEndpoint("/{id}", "Edit Breakfast", "Edits a Breakfast Item")]
    private static object GetBreakfastMenu(HttpContext context, [FromRoute] string id)
    {
    }
    
    [PutEndpoint("/{id}", "Edit Breakfast", "Edits a Breakfast Item")]
    [GroupMember<MenuGroup>]    
    private static object PutBreakfastMenu(HttpContext context, [FromRoute] string id)
    {
    }
}

in the example above the PutBreakfastMenu would be added the MenuGroup with a route /menu NOT /breakfast

Configuration

Stencil allows you to easily plug into the configuration for each endpoint generated by your Methods. Simply add the Configure attribute to the method declaration or class declaration and magic 🐣

endpoint_configure.gif

Your type will now implement a custom interface used by the code generator. This tells Scribbly.Stencil to invoke your configure method allowing you to modify and append the builder. This interface forces the compiler to guid you.

public partial class BreakfastEndpoints
{
    [PutEndpoint("/{id}", "Edit Breakfast", "Edits a Breakfast Item")]
    [Configure]
    private static object PutBreakfastMenu(HttpContext context, [FromRoute] string id)
    {
        return new { id = id };
    }

    /// <inheritdoc />
    public void ConfigurePutBreakfastMenu(IEndpointConventionBuilder putBreakfastMenuBuilder)
    {
        putBreakfastMenuBuilder.ProducesProblem(404);
    }
}

🛒 Groups

Endpoint groups can be created to map several endpoints with common routing and configuration. Scribbly.Stencil will build this routing tree for you behind the scenes. Just create a Group and attached members.

[EndpointGroup("/lunch", "Manage Lunch Menu")]
public partial class LunchGroup
{
}

This will create a routing group. Groups can even be members of other groups. Use the GroupMember<T> to target a group. Your new group will be added to the group specified in the Generic Param.

[EndpointGroup("/menu", "Menu")]
public partial class MenuGroup
{
}

[GroupMember<MenuGroup>]
[EndpointGroup("/lunch", "Manage Lunch Menu")]
public partial class LunchGroup
{
}

Of course you'll need to add endpoints to groups as well yielding a routable endpoint /menu/lunch/{id}

public partial class LunchEndpoints
{
    [GetEndpoint("lunch/{id}")]
    [GroupMember<LunchGroup>]
    private static IResult GetLunchMenu(HttpContext context, [FromRoute] string id)
    {
        return Results.Ok(id);
    }
}

Using Scribbly.Stencil you can also contain endpoints inside of groups. Each endpoint will inherit the behavior of the encapsulating class.

Since the DinnerEndpoints in the following example and annotated with the EndpointGroup and the GroupMember<MenuGroup> the endpoints will yield routes /menu/dinner/{id}

[EndpointGroup("/dinner", "Dinner time endpoints wrapped in a Group.")]
[GroupMember<MenuGroup>]
public partial class DinnerEndpoints
{
    [GetEndpoint("/{id}", "Gets Dinner", "Queries a new Dinner Item")]
    private IResult GetDinnerMenu(HttpContext context, [FromRoute] string id)
    {
        return Results.Ok(id);
    }
    
    [PostEndpoint("/{id}", "Create Dinner", "Creates a new Dinner Item")]
    private object PostDinnerMenu(HttpContext context, [FromRoute] string id)
    {
        return Results.Ok(id);
    }
    
    [PutEndpoint("/{id}", "Edit Dinner", "Edits a Dinner Item")]
    private object PutDinnerMenu(HttpContext context, [FromRoute] string id)
    {
        return new { id = id };
    }
}

Configuration

While this is great you may need to plug into the route building and override or append configuration to groups.

Simply add the Configure attribute to the class declaration and magic 🐣

group_configure.gif

Your type will now implement a custom interface used by the code generator. This tells Scribbly.Stencil to invoke your configure method allowing you to modify and append the builder.

[Configure]
[GroupMember<ApplicationRoot>]
[EndpointGroup("/lunch", "Manage Lunch Menu")]
public partial class LunchGroup
{
    /// <inheritdoc />
    public void Configure(IEndpointConventionBuilder lunchGroupBuilder)
    {
        applicationRootBuilder.AddEndpointFilter<MyFilter>();
        // .....
    }
}
There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.