Organizing configuration

Keep Program.cs small as your admin panel grows

Organizing resource registration

If you have used ASP.NET Core Minimal APIs, you know that it can clutter up your Program.cs very quickly with a lot of code and turn it into an unmaintainable mess.

Sadly, StellarAdmin suffers from the same problem. Once you have more than a few resources, configuring them all in Program.cs makes the file hard to read.

Luckily for us, people have solved this problem in Minimal APIs using various approaches, and we can use those same approaches to make our StellarAdmin configuration more readable and maintainable.

The approach we recommend is to move each resource's configuration into an extension method in its own file in a separate directory.

For example, we can define our Guide resource in an extension method in /Admin/GuidesAdmin.cs.

// Admin/GuidesAdmin.cs
using StellarAdmin;
using StellarAdmin.EntityFrameworkCore;

public static class GuidesAdmin
{
    public static StellarAdminBuilder AddGuides(this StellarAdminBuilder admin) =>
        admin.AddEfCoreResource<Guide>(resource =>
        {
            // ... some config omitted for brevity

            resource.SidebarItem(item => item.Group = "Operations");
            resource.Index(index => index.Columns(columns => columns.Add(guide => guide.LastName)));
        });
}

Other resource definitions can be moved out into their own files in a similar manner. We can then add an AddResources() call in Admin/AdminResources.cs that ties all the resource registration methods together.

// Admin/AdminResources.cs
public static class AdminResources
{
    public static StellarAdminBuilder AddResources(this StellarAdminBuilder admin) =>
        admin.AddGuides().AddTrips().AddBookings();
}

Which leaves just the single call to AddResources() inside the configuration callback of AddStellarAdmin().

// Program.cs
builder.Services.AddStellarAdmin(admin =>
{
    admin.RequireAuthorization(policy => policy.RequireRole("Staff"));
    admin.ConfigureBranding(branding => branding.Title = "Voyager Travel");
    admin.AddResources();
});

Sharing field definitions

Another common area where you can share configuration is the field definitions for a resource's create and edit forms. These are often the same and since the field builders for both take a parameter of type ResourceFieldsBuilder<TResource>, these can also be extracted to a single shared method as per the example below.

public static class GuidesAdmin
{
    public static StellarAdminBuilder AddGuides(this StellarAdminBuilder admin) =>
        admin.AddEfCoreResource<Guide>(resource =>
        {
            // ... some config omitted for brevity

            resource.AllowCreate(create => create.Fields(GuideFields));
            resource.AllowEdit(edit => edit.Fields(GuideFields));
        });
    
    static void GuideFields(ResourceFieldsBuilder<Guide> fields)
    {
        fields.Add(guide => guide.FirstName);
        fields.Add(guide => guide.LastName);
        fields.Add(guide => guide.Email);
    }
}

On this page