how can I run checks before deleting. IsDeletable method

how can I run checks before deleting. IsDeletable method

Old forum URL: forums.lhotka.net/forums/t/7436.aspx


McKay posted on Monday, August 10, 2009

Hi

My senario is this. I have book shop shop. My database contains customers that owe money. I don’t want to delete a customer from the database if they owe money or if they have a book on order. The calculation to determine if money is owed etc is complex so I only wish to run these calculations on delete.
Does the CSLA business object have an approach that I can use?
My idea would be to create a static method on the client business object that runs my required checks and returns a message when the checks fail. From my windows delete button I would call the static method and only continue to delete if the message is empty, otherwise I would display the message.
example:
//Business objects static method
public static bool IsDeletable(int contactId, out string message)
{
message = string.Empty;

// Run owes money check
...
if (failed)
{
message = "Unable to delete as this customer owes you money!";
return false;
}
// Run other check
...
if (failed)
{
message = "Unable to delete as this customer has books on order";
return false;
}
Code
return true;
}

//Code in window
void DeleteButtonPressed(...)
{
int contactId = ..get the contactId;
string message;
if (ContactBo.IsDeletable(contactId, out message))
ContactBo.Delete(contactId);
else
MessageBox.Show(message, "Unable to delete");
}

Is there a better way to do this?

Vinodonly replied on Tuesday, August 11, 2009

You can overwrite DataPortal_Update method and provide your own implementation and can perform all the check there..

In case of invalid operation (for eg. trying to del customer which owes then you can throw exception).. something like this

foreach (MyObj child in DeletedList)

if (!child.DelAllowed)
throw new exception("Del not allowed");
else
child.Child_DeleteSelf();


JonnyBee replied on Tuesday, August 11, 2009

Hi,

Typicially this is a combination of "roles based" authorization and "state" your business objects.

I would look probably overload CanExecuteMethod and if roles based authorization is required that can easily be applied.

Example
    // add roles based authorixzation rules
    protected override void AddAuthorizationRules()
    {
      AuthorizationRules.AllowExecute(Delete, "ProjectManager");
      AuthorizationRules.DenyExecute("Delete", "Guest");
    }

    public override bool CanExecuteMethod(string methodName)
    {
        if (!base.CanExecuteMethod(methodName)) return false;

        if (mehodName == "Delete") {
          using (BypassPropertyChecks()) {
          // Execute command to check if customer can be delete
                 if (CustomerOwesMoneyCommand.Execute(new SingleCriteria<Customer, int>(this.CustomerId) return false;

                 if (CustomerHasUnprocessedOrderCommand.Execute(new SingleCriteria<Customer, int>(this.CustomerId) return false;
           }
        }
        return true;
    }

    public void DeleteCustomer(int id)
    {
          // Throws execption if not allowed to delete customer
          CanExecuteCommand("Delete", true)

          //Execute delete
          DataPortal.Delete(new SingleCriteria<Customer, int>(id));
    }


/jonnybee

Copyright (c) Marimer LLC