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 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 node and click on the Add from entity view button. Select the NumberOfProducts property and click on the Add 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 of type Retrieved on the CategoryView entity view.
Save the rule. This generates a new class in the application project :
Catalog
└─── businessAssembly
└─── Application
└─── EventRules
└─── CategoryView
│ Retrieved.cs
You will need the product repository in order to retrieve products from a category. For this, it is necessary to add a dependency to the event rule. Click on the dedicated button available at the top right of the editor code. Click on the Add button and fill properties as follows :
Dependency type = Entity view repository
Entity view name = ProductView
The dependencies defined in this array are injected into the constructor of the C# class associated with the event rule. You can save the rule and open it in your visual studio. Update the Retrieved.cs file with the following code to implement the logic of your event rule:
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, CancellationToken cancellationToken)
{
foreach (ICategoryView category in args.Items)
{
IReadOnlyList<IProductView> products = await _productViewRepository.GetListAsync(q => q.Where(p => p.CategoryID == category.CategoryID), cancellationToken);
category.NumberOfProducts = products.Count;
}
}
Note
It's important to note that the OnRetrievedAsync method is asynchronous and the last argument is a cancellationToken. The cancellationToken parameter is also passed to the GetListAsync method to allow the cancellation of the request.
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.