Skip to main content

Designing API services

👉 Choose Api > Services in the main menu.

wyStack comes with a lot of default Services to handle API requests, but you can add your own Services to respond to certain events using handlers.

Responding to handlers in the API, implementing the RootApiService​

wyStack services and handlers are available using Dependency Injection via the contructor of your Service.

The most important services that are provided are:

  • IHandlerService: respond to global handlers, use this service to implement: authentication, authorization, user settings, etc.
  • IEntityHandlerService<T>: respond to entity (CRUD) handlers/operations.
  • IFilterService<T>: respond to entity read operations, filter out entities, for example for authorization purposes.
  • I...Repository<T>: gain access to a Repository/Database/Data Store.
  • IUserService: provides information about the current user that is logged on to the API.
  • ITenantService: provides information about the tenant that the current user is logged on to.
  • IBlobService: provides access to the blob storage.

Full list of provided services:

  • IAuthorizationService
  • IComputeHandlerService
  • IConfigurationService
  • IEmailNotificationService
  • IEncryptionService
  • IEntityHandlerService
  • IFilterService
  • IHandlerService
  • IJobService
  • ILiquidRenderingService
  • IMailService
  • IReportRenderingService
  • IRequestService
  • IRootComputeLinkRegistrationService
  • IRootLinkRegistrationService
  • ISocialService
  • ISyncService
  • ITenantService
  • IThumbService
  • ITokenService
  • IUserService

We created a standard RootApiService that you can use to respond to certain wyStack handlers and events, or you can register and call other Services that intervene on these events and handlers.

Make sure that you register handlers in the Register() method of your Service, not in the constructor.

Example​

Note that __app_namespace__ is replaced with the real namespace of your app.

using System;
using System.Collections.Generic;
using System.Linq;
using __app_namespace__.Library.Models;

namespace __app_namespace__.Library
{
public class RootApiService(
SynchronizationService synchronizationService,
IEntityHandlerService<BaseEntity> baseEntityHandler,
IHandlerService handlerService,
IAuthorizationService authService,
IUserService us,
SecurityService securityService) : IRootApiService
{
void IRootApiService.Register()
{
synchronizationService.Register();

handlerService.OnPreAuthenticate((ICredentials credentials) =>
{
return securityService.PerformPreAuthentication(credentials);
});

handlerService.OnAuthenticate((ICredentials credentials) =>
{
return securityService.PerformAuthentication(credentials);
});

authService.OnRequest((FeatureKeyType featureKeyType, Type type, string operationName) =>
{
return true; // Accept any request
});

baseEntityHandler.BeforeEntityCreate((BaseEntity baseEntity) =>
{
if (baseEntity is CoreEntity coreEntity)
{
// Add some custom logic, for example filling defaults
var user = securityService.GetOrCreateUser(us.GetCurrentUserName());
coreEntity.CreatedBy = user.Name;
coreEntity.Created = DateTimeOffset.UtcNow;
coreEntity.ModifiedBy = user.Name;
coreEntity.Modified = DateTimeOffset.UtcNow;
}

return new BeforeEntityHandlerResult();
});

baseEntityHandler.BeforeEntityUpdate((BaseEntity baseEntity) =>
{
if (baseEntity is CoreEntity coreEntity)
{
// Add some custom logic, for example filling defaults
var user = securityService.GetOrCreateUser(us.GetCurrentUserName());
coreEntity.ModifiedBy = user.Name;
coreEntity.Modified = DateTimeOffset.UtcNow;
}

return new BeforeEntityHandlerResult();
});

handlerService.OnRequestUserSettings(() =>
{
var user = securityService.GetOrCreateUser(us.GetCurrentUserName());

var userSettings = new List<IUserSetting>();
return userSettings;
});
}
}
}

Storing your API code​

wyStack gives you 2 ways to store your code:

  • In model: use the integrated source code editor of wyStack. At the moment IntelliSense is not available for C#.
  • On disk: use your favourite IDE, for example Visual Studio or Visual Studio Code.