Skip to content

Generating Handler Interfaces

In Serene, every request handler class has a matching interface that is written by hand:

public interface IRoleSaveHandler : ISaveHandlerAsync<MyRow> { }

public class RoleSaveHandler(IRequestContext context)
    : SaveRequestHandlerAsync<MyRow>(context), IRoleSaveHandler
{
    // ...
}

In StartSharp you don't have to write the interface. Place the GenerateInterface attribute on the handler class, and the Serenity.Pro.Coder InterfaceSourceGenerator (a Roslyn source generator) creates the interface for you:

[GenerateInterface]
public class RoleSaveHandler(IRequestContext context) :
    SaveRequestHandlerAsync<MyRow>(context),
    IRoleSaveHandler
{
    // ...
}

[GenerateInterface] is a Serenity.Pro.Coder feature, so it is only available in StartSharp (or projects referencing Serenity.Pro.Coder).

Works for All Request Handler Types

This is not specific to save handlers. The generator recognizes every request handler base class and picks the matching base interface automatically:

Handler base class Generated interface
SaveRequestHandlerAsync<TRow> ISaveHandlerAsync<TRow>
ListRequestHandlerAsync<TRow> IListHandlerAsync<TRow>
RetrieveRequestHandlerAsync<TRow> IRetrieveHandlerAsync<TRow>
DeleteRequestHandlerAsync<TRow> IDeleteHandlerAsync<TRow>
UndeleteRequestHandlerAsync<TRow> IUndeleteHandlerAsync<TRow>

So a RoleListHandler, RoleDeleteHandler, or RoleUndeleteHandler marked with [GenerateInterface] gets its IRoleListHandler, IRoleDeleteHandler, IRoleUndeleteHandler, etc. generated the same way.

The generator also recognizes the synchronous base classes (SaveRequestHandler<TRow>, ListRequestHandler<TRow>, ...) and generates the corresponding ISaveHandler<TRow>, IListHandler<TRow>, ... interfaces, so both modes are supported. New code should use the async variants.

Naming and Namespace

The generated interface is named I + class name (e.g. IRoleSaveHandler).

It is placed in the same namespace as the handler class, but with a trailing .RequestHandlers suffix removed. So a handler in MyProject.Administration.RequestHandlers gets its interface generated into MyProject.Administration:

// generated by InterfaceSourceGenerator
namespace MyProject.Administration;

public interface IRoleSaveHandler : ISaveHandlerAsync<MyRow> { }

Nullable Context

The generated interface follows the nullable context of the project, which the generator takes from the Nullable MSBuild property, so a handler with nullable annotations, e.g. a string? parameter, generates a matching interface (string? stays string?) instead of one without a nullable context. Without it, a nullable annotation in a project that enables nullable reference types caused CS8669 warnings in the generated code.

Interface Members

Public members of the class become interface members. Mark a member with NonInterfaceMember to exclude it from the generated interface.

For a class that is not a request handler (e.g. an action handler deriving from BaseRequestHandler), the generated interface derives from IRequestHandler and includes the public methods:

[GenerateInterface]
public class TestActionHandler : ITestActionHandler
{
    public TestResponse TestAction(TestRequest request) { ... }
}

// generated:
public interface ITestActionHandler : IRequestHandler
{
    TestResponse TestAction(TestRequest request);
}

Interfaces Without a Request Handler

Because the generated interface derives from IRequestHandler, AddServiceHandlers registers the class in every application that references the assembly, see Auto-Registration of Request Handlers. That is not wanted for a handler that needs the services of a feature which only some applications add: in the others it fails at startup in development, as its services are missing.

Set RequestHandler = false for a class that only the Add method of its own feature should register:

// AddEmailQueue registers it, not AddServiceHandlers
[GenerateInterface(RequestHandler = false)]
public class EmailQueueHandler(/* the services of the email queue */) : IEmailQueueHandler
{
    // ...
}

// generated:
public interface IEmailQueueHandler
{
    // the public methods of the class
}
  • The property defaults to true, so the other interfaces are generated as before.
  • The interface has the public members of the class and no base interface, so it is not an IRequestHandler, and AddServiceHandlers skips the class.
  • The class should not implement a request handler interface of its own either, or AddServiceHandlers registers it through that interface.

IEmailQueueHandler, IBackgroundJobHandler, IDbSettingsHandler and IExceptionLogHandler of Serenity.Pro.Extensions are generated this way, as the Add methods of their features register them.

See Also