Blazor 6.0 Call BusinessRule On business object Delete Method #3240
-
Hello All, Sorry all Im not sure how to format it I need some help with this. I will provide some info on what I am trying to do. The application that I am working on tracks equipment. When the admin tries to delete the equipment, I need to check if it has been assigned to anyone. If the equipment is assigned, then the rule should break and not allow the delete. Business Base
Business Rule
|
Beta Was this translation helpful? Give feedback.
Replies: 9 comments 3 replies
-
Well I added this code and it seemed to work. Im not sure if this is the best method to do it though. Since I know the business rules work with properties, I added a MarkForDelete property to my business object. That was the only way I figured out how to pass the boolean value. Can someone let me know this is a proper way of accomplishing my task.
|
Beta Was this translation helpful? Give feedback.
-
There are two ways to implement a delete operation on a root object. One is to use the data portal The other is probably what you want, and works like this:
|
Beta Was this translation helpful? Give feedback.
-
Could we possibly add a gateway rule that will only check for equipment assignment when the object is marked for deletion? |
Beta Was this translation helpful? Give feedback.
-
I think this is probably the right answer? using Csla;
using Csla.Configuration;
using Csla.Core;
using Csla.Rules;
using Microsoft.Extensions.DependencyInjection;
// configuration
var services = new ServiceCollection();
services.AddCsla();
var provider = services.BuildServiceProvider();
// test code
var dp = provider.GetRequiredService<IDataPortal<EquipmentDelete>>();
var equipment = dp.Fetch(42);
try
{
var deleted = await equipment.RemoveEquipment();
if (deleted)
{
Console.WriteLine("Deleted successfully");
}
else
{
Console.WriteLine(equipment.BrokenRulesCollection.GetFirstBrokenRule(EquipmentDelete.IdProperty).Description);
}
}
catch (DataPortalException ex)
{
if (ex.BusinessException != null)
Console.WriteLine($"Error: {ex.BusinessException.ToString()}");
else
Console.WriteLine($"Unexpected error: {ex.ToString()}");
}
catch (Exception ex)
{
Console.WriteLine($"Unexpected error: {ex.ToString()}");
}
// classes follow here
[Serializable]
public class EquipmentDelete : BusinessBase<EquipmentDelete>
{
public static readonly PropertyInfo<int> IdProperty = RegisterProperty<int>(nameof(Id));
public int Id
{
get => GetProperty(IdProperty);
set => SetProperty(IdProperty, value);
}
public async Task<bool> RemoveEquipment()
{
await BusinessRules.CheckRulesAsync();
if (IsValid)
{
MarkDeleted();
await SaveAsync();
return true;
}
else
{
return false;
}
}
protected override void AddBusinessRules()
{
base.AddBusinessRules();
BusinessRules.AddRule(new EquipmentAssignedRuleAsync(IdProperty));
}
[Fetch]
private void Fetch(int id)
{
// get data related to `id`
using (BypassPropertyChecks)
{
Id = id;
}
}
[Insert]
private void Insert()
{
// insert new data related to `id`
}
[Update]
private void Update()
{
// update data related to `id`
}
[DeleteSelf]
private void DeleteSelf()
{
Delete(Id);
}
[Delete]
private void Delete(int id)
{
// delete data related to `id`
}
}
public class EquipmentAssignedRuleAsync : BusinessRuleAsync
{
public EquipmentAssignedRuleAsync(IPropertyInfo primaryProperty)
: base(primaryProperty)
{
InputProperties.Add(PrimaryProperty);
}
protected override async Task ExecuteAsync(IRuleContext context)
{
var id = (int)context.InputPropertyValues[PrimaryProperty];
var dp = context.ApplicationContext.GetRequiredService<IDataPortal<EquipmentAssignedChecker>>();
var checker = await dp.FetchAsync(id);
if (checker.IsAssigned)
context.AddErrorResult("Equipment is assigned to someone");
}
}
[Serializable]
public class EquipmentAssignedChecker : ReadOnlyBase<EquipmentAssignedChecker>
{
public static readonly PropertyInfo<bool> IsAssignedProperty = RegisterProperty<bool>(nameof(IsAssigned));
public bool IsAssigned
{
get => GetProperty(IsAssignedProperty);
set => LoadProperty(IsAssignedProperty, value);
}
[Fetch]
private void Fetch(int id)
{
// find out if `id` equipment is assigned
IsAssigned = false;
}
} |
Beta Was this translation helpful? Give feedback.
-
Hello @rockfordlhotka and @Chicagoan2016, Thank you for the responses and code snipes. I totally forgot about the second method of deleting. I will also look at implementing the option above. I have been struggling to get the broken rules back to the client with my original code that I added for the delete. I will try it out and get back to you. Thank you both. |
Beta Was this translation helpful? Give feedback.
-
As the object is fetched you could load a value |
Beta Was this translation helpful? Give feedback.
-
Hello @rockfordlhotka and @Chicagoan2016, Well Im stilling trying to perform the delete and check my rules. I am trying to perform it via this method. Fetch the object I know you guys provided the other solution but I want to see if I can get it going. Razor Page Code
Im getting the following error below with the following code on my command.
Command Execute
If I comment out the dataPortal create and execute code and if statement I do see my error message. Is there something that I can do to correct the error? If not then I guess I will need to implement the solution above. I wish I could just call my DataAccess layer class directly
|
Beta Was this translation helpful? Give feedback.
-
@jhuerta3 |
Beta Was this translation helpful? Give feedback.
-
Thank you @rockfordlhotka and @Chicagoan2016. Thank you. On Razor Page
|
Beta Was this translation helpful? Give feedback.
I think this is probably the right answer?