Related List Component Custom Business Rule
Inherit from the class RelatedListComponentCustomBusinessRule
and override the desired business rule methods in order to implement a concrete rule for a Related List Component.
Validation methods
ApplyBusinessRulesForComponentOnSaveAsync
: Use this method to validate and/or modify data that requires simultaneously inspecting all new, modified, or removed rows from a Related List component.ApplyEntityBusinessRulesOnSaveAsync
: Use this method to validate and/or modify the fields of each new or modified row in a Related List component.
Dependency Injection
MIB supports dependency injection in concrete validators. Click here for more information.
Example
In this hypothetical validation, we override the validation methods ApplyBusinessRulesForComponentOnSaveAsync
and ApplyEntityBusinessRulesOnSaveAsync
. We use the inherited method AddNotification
to add notifications that describe issues with the persistence data.
In a hypothetical case where we need to validate and/or modify the entire scope of the list, we should override the method ApplyBusinessRulesForComponentOnSaveAsync
to gain access to the contents of the list: AddedEntities
, NewEntities
, UpdatedEntities
and RemovedRelations
.
In a hypothetical case where we need to to validate and/or modify only the persistence entities, we should override the method ApplyEntityBusinessRulesOnSaveAsync
to gain access to the PersistenceEntityData
.
Key points
- Notifications will be aggregated and displayed to the user.
- Dictionary keys can be used in the messages.
public class SampleRelatedListBusinessRule : RelatedListComponentCustomBusinessRule
{
public override Task ApplyBusinessRulesForComponentOnSaveAsync(List<int> addedEntities, List<PersistenceEntityData> newEntities, Dictionary<int, PersistenceEntityData> updatedEntities, List<int> removedRelations, RelatedListComponentContext context, CancellationToken cancellationToken)
{
if (updatedEntities.Any())
{
foreach (var item in updatedEntities)
{
foreach (var field in item.Value.Fields)
{
if (field.Key.Equals("NAME", StringComparison.InvariantCultureIgnoreCase))
{
item.Value.Fields["NAME"] = "Modified by business rule";
}
}
}
}
return Task.CompletedTask;
}
public override Task ApplyEntityBusinessRulesOnSaveAsync(PersistenceEntityData entity, RelatedListComponentContext context, CancellationToken cancellationToken)
{
if (entity.Fields.TryGetValue("SOURCE", out var source))
{
if (source != null && source.ToString() == "1")
{
AddNotification(new("My rules do not allow this source"));
}
}
return Task.CompletedTask;
}
}