Skip to main content

API code samples

Accessing data stores

You can easily access all datastores using dependency injection:

using System;
using System.Collections.Generic;
using System.Linq;

namespace __app_namespace__.Library
{
public class RootApiService(IDefaultRepository repository) : IRootApiService
{
void IRootApiService.Register()
{
}
}
}

Using IFilterService

Filter data on API (server) side, for example based on user permissions/role.

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

namespace __app_namespace__.Library
{
public class RootApiService(IFilterService<User> userFilter) : IRootApiService
{
void IRootApiService.Register()
{
userFilter.OnFilter((query) =>
{
return query.Where(x => x.Active == true);
});
}
}
}

Using IEntityHandlerService

Respond to entity events of a certain type.

You can use for example: IEntityHandlerService<BaseEntity> baseEntityHandler (respond to all entities) but also IEntityHandlerService<User> userHandler (respond to User entities only).

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

namespace __app_namespace__.Library
{
public class RootApiService(IEntityHandlerService<BaseEntity> baseEntityHandler) : IRootApiService
{
void IRootApiService.Register()
{
baseEntityHandler.BeforeEntityCreate((BaseEntity baseEntity) =>
{
var coreEntity = baseEntity as CoreEntity;
if (coreEntity != null)
{
// Add some custom logic, for example filling defaults
coreEntity.CreatedBy = "hello";
coreEntity.Created = DateTimeOffset.UtcNow;
coreEntity.ModifiedBy = "hello";
coreEntity.Modified = DateTimeOffset.UtcNow;
}

return new BeforeEntityHandlerResult();
});

baseEntityHandler.BeforeEntityUpdate((BaseEntity baseEntity) =>
{
var coreEntity = baseEntity as CoreEntity;
if (coreEntity != null)
{
// Add some custom logic, for example filling defaults
coreEntity.ModifiedBy = "hello";
coreEntity.Modified = DateTimeOffset.UtcNow;
}

return new BeforeEntityHandlerResult();
});
}
}
}

Using IHandlerService

Use the IHandlerService to respond to global events, not related to specific entities.

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

namespace __app_namespace__.Library
{
public class RootApiService(IHandlerService handlerService) : IRootApiService
{
void IRootApiService.Register()
{
handlerService.OnPreAuthenticate((ICredentials credentials) =>
{
});

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

// respond to user settings request
handlerService.OnRequestUserSettings(() =>
{
var user = _securityService.GetOrCreateUser(_userService.GetCurrentUserName());

var userSettings = new List<IUserSetting>();

// disable menu item users for current user
userSettings.Add(new UserSetting()
{
Key = "FeatureToggle_" + AppConstants.FeatureKeys.MenuItem_Users,
Value = "false"
});

userSettings.Add(new UserSetting()
{
Key = "SomeSetting",
Value = "123"
});

return userSettings;
});

// respond to global operation
handlerService.OnOperation(AppConstants.OperationNames.Test, (object[] args) =>
{
// handle operation here
return new OperationHandlerResult()
{
ClientInstructions = new List<IClientInstruction>() {
new MessageClientInstruction() {
Message = "OK. 👍",
MessageType = "SuccessNotification"
}
}
};
});
}
}
}

Using other Services

Check also the following services that are available using DI:

Designing API services