Table of Contents

Creating an entity view event rule

This article describes how to create an event that will fill a property created on the entity view. This new property will return the number of products in a category.

Creating the entity view property

Before creating the event, you will create the new property that will be filled when the entity view is loaded. Add the following unbound property on the CategoryView entity view with the Add a scalar property action:

NumberOfProducts:

Name = NumberOfProducts
Caption = Number of products
Type = Integer
Server-side read only = true
Client-side read only = true

Open the CategoryUI, click on the Properties tab and click on the Add from entity view button. Select the NumberOfProducts property and click on the OK button. This will add to the UI view a new property based on the property you previously created on the CategoryView entity view.

Next, update the UI template of CategoryUI to display your new property by adding the following line next to the other <form-field> tags :

<form-field property-name="NumberOfProducts" />

Creating the entity view event rule metadata

Add an event rule with the following properties on the CategoryView entity view:

Event = Retrieved

Save the rule. This generates a new class in the application project :

Catalog
└─── businessAssembly
     └─── Application
        └─── EventRules
            └─── CategoryView
                │    Retrieved.cs

Open it in your visual studio and update it :

private readonly IProductViewRepository _productViewRepository;

/// <summary>
/// Initializes a new instance of the <see cref="Retrieved"/> class.
/// </summary>
/// <param name="productViewRepository">Product view repository.</param>
public Retrieved(IProductViewRepository productViewRepository)
{
    _productViewRepository = productViewRepository;
}

/// <inheritdoc/>
public async Task OnRetrievedAsync(IRetrievedRuleArguments<ICategoryView> args)
{
    foreach (ICategoryView category in args.Items)
    {
        IReadOnlyList<IProductView> products = await _productViewRepository.GetListAsync(q => q.Where(p => p.CategoryID == category.CategoryID));
        category.NumberOfProducts = products.Count;
    }
}

Generate the application so that the event rule is compiled and taken into account in the application.

Testing the event rule

To test your event rule in the user interface, open the application at the https://localhost/neos/Northwind address and go to the category edit page. The event rule should fire when you open the UI or when you refresh your data. You now see the number of products each category has.