Actions classes

This information is still applicable to v6

This page provides some common scenarios to use the PersistentObjectActions class.

Soft Delete

By default, Vidyano will tell Entity Framework to delete an entity from the target context when the end user uses the Delete action. We can override this behavior so that the entity is not actually deleted but only marked as deleted (e.g. disabled, not active, obsolete, …).

protected override void MarkForDeletion(Customer entity)
{
    //Context.DeleteObject(entity);
    entity.IsActive = false;
}

Cascade Delete

Another common scenario is to delete related entities when a master entity is deleted (in most cases this can also be done on the database using cascade delete on the foreign key).

protected override void MarkForDeletion(Customer entity)
{
    Context.DeleteObject(entity);
    foreach (var address in entity.Addresses)
        Context.DeleteObject(address);
}

Auditing

Setting audit attributes can be easily implemented by using the UpdateEntity method to set the attributes.

protected override void UpdateEntity(PersistentObject obj, Customer entity)
{
    if (obj.IsNew)
    {
        obj[nameof(Customer.CreatedOn)] = Manager.Current.Now;
        obj[nameof(Customer.CreatedBy)] = Manager.Current.User.Id;
    }
    obj[nameof(Customer.ModifiedOn)] = Manager.Current.Now;
    obj[nameof(Customer.ModifiedBy)] = Manager.Current.User.Id;

    base.UpdateEntity(obj, entity);
}

External services

If we need to inform another external service about new or updated entities we can use the following code.

protected override void PopulateAfterPersist(PersistentObject obj, Customer entity)
{
    base.PopulateAfterPersist(obj, entity);

    var wasNew = obj.IsNew; // The entity will have the correct DB primary key at this moment
    ExternalService.PostCustomer(wasNew, entity);
}

Last updated

Was this helpful?