Table of Contents

Entity view Retrieving

This event rule is triggered when an entity view is retrieved and the entity view is bound to an entity.
You can use this event to modify the filtering of the entity before the entity view is retrieved.

If needed, you can inject other services or repositories into the event rule constructor to build custom filter logic.

Note

This event rule is not cancelable.
This event rule is only triggered when retrieving entity views, not when retrieving entities directly. If you want to filter entities when they are retrieved directly, you can use Global Filter.

Arguments

You can find the details of the interface on this page.

Name Type Description
EntityViewName string The name of the entity view being retrieved.
Context IBusinessRuleContext Represents a key/value pair dictionary that is used to store custom data. You can find the details of the interface on this page.
AppendFilter(Expression<Func<TEntity, bool>> predicate) void Appends a filter predicate to the existing filter expression for the retrieval operation.

Example

Note

This example is available in TechnicalDemos neos cluster.

The entity Invoice inherits from Document.

Invoice properties:

Name Type Required
DueDate Date Yes
Enabled Boolean Yes
InvoiceNumber String Yes
TotalAmount Decimal No

Document properties:

Name Type Required
AuthorId String No
CreatedAt DateTime No
Id AutoIncrementedInteger Yes
Status Enum (DocumentStatus) Yes
Title String Yes

The document entity has an EntityViewRetrieving event rule that filters documents based on their status:

/// <summary>
/// Represents EntityViewRetrieving event rule.
/// </summary>
public class EntityViewRetrieving : IEntityViewRetrievingRule<Document>
{
    private readonly INeosTenantInfoAccessor _userInfoAccessor;
    private readonly INeosLogger<IEntityViewRetrievingRule<Document>> _logger;

    /// <summary>
    /// Initializes a new instance of the <see cref="EntityViewRetrieving"/> class.
    /// </summary>
    /// <param name="logger">Logger.</param>
    public EntityViewRetrieving(
        INeosTenantInfoAccessor userInfoAccessor,
        INeosLogger<IEntityViewRetrievingRule<Document>> logger)
    {
        _userInfoAccessor = userInfoAccessor;
        _logger = logger;
    }

    /// <inheritdoc/>
    public async Task OnEntityViewRetrievingAsync(IEntityViewRetrievingRuleArguments<Document> args, CancellationToken cancellationToken)
    {
        if (_userInfoAccessor.NeosTenantInfo != null &&
            _userInfoAccessor.NeosTenantInfo.AdditionalProperties.TryGetValue("CanAccessDocumentStatusDraft", out string? value) &&
            bool.TryParse(value, out bool canAccessDocumentStatusDraft) &&
            !canAccessDocumentStatusDraft)
        {
            args.AppendFilter(doc => doc.Status != DocumentStatus.Draft);
            _logger.LogInformation("Appended filter to exclude documents with Draft status for tenant {TenantId}.", _userInfoAccessor.NeosTenantInfo.Identifier);
        }

        await Task.CompletedTask;
    }
}

This event rule checks if the current tenant has the permission to access documents with the "Draft" status. If not, it appends a filter to exclude such documents from the retrieved entity view. The permission is determined by checking a custom property CanAccessDocumentStatusDraft in the tenant's additional properties.

The Invoice entity has an EntityViewRetrieving event rule that filters only activated invoices for a specific entity view:

/// <summary>
/// Represents EntityViewRetrieving event rule.
/// </summary>
public class EntityViewRetrieving : IEntityViewRetrievingRule<Invoice>
{
    private readonly INeosLogger<IEntityViewRetrievingRule<Invoice>> _logger;

    /// <summary>
    /// Initializes a new instance of the <see cref="EntityViewRetrieving"/> class.
    /// </summary>
    /// <param name="neosTenantInfoAccessor">Neos tenant info accessor.</param>
    /// <param name="logger">Logger.</param>
    public EntityViewRetrieving(
        INeosLogger<IEntityViewRetrievingRule<Invoice>> logger)
    {
        _logger = logger;
    }

    /// <inheritdoc/>
    public async Task OnEntityViewRetrievingAsync(IEntityViewRetrievingRuleArguments<Invoice> args, CancellationToken cancellationToken)
    {
        if (args.EntityViewName == "ActiveInvoiceView")
        {
            args.AppendFilter(invoice => invoice.Enabled);
            _logger.LogInformation("Appended filter to exclude disabled invoices");
        }

        await Task.CompletedTask;
    }
}

In all entity view retrievals of Invoice, the user sees only invoices that do not have a draft status (if the additional property CanAccessDocumentStatusDraft is set to false for the current tenant).

When the entity view is ActiveInvoiceView, only enabled invoices are retrieved.