Validation and deferred deletion

Validation and deferred deletion

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


j055 posted on Thursday, June 18, 2009

Hi

Is there a way of using Validation rules for objects requiring deferred deletion? I've had some success by creating a Rule which checks IsDeleted before validating, then override the delete method:

 

        public override void Delete()

        {

            ValidationRules.CheckRules();

            base.Delete();

        }

 

The problem is that the Save method doesn't throw an exception if the object is deleted. I know I could override that too. Also the CheckRules method obviously checks all rules which may not be valid but are not relevant to the delete requirement. Perhaps I'm missing something here.

 

Can the framework provide for validation just on deleted objects or do I need to handle this myself? Any help/suggestions much appreciated. Please also point me to any pages in the book which you think might help me with this.

 

Many thanks

Andrew

 

JonnyBee replied on Friday, June 19, 2009

Hi,

Why would you want to run validation rules on deleted objects? Your users cannot edit or change items in the deleted list so that would be a catch 22 if your objects became Invalid due to broken rules in deleted items.

/jonnybee

j055 replied on Friday, June 19, 2009

Perhaps I'm using the wrong terminology.

What I'm trying to do is restrict delete operations when the object contains certain values. E.g. The current user should only be allowed to delete an object they created. My object contains an OwnerId property. The current user identity should only be able to delete if the custom Identity contains the same ID. Also if the object has a status=live setting then the user cannot delete the object. In which case it should throw an exception. At the moment all users of certain roles can delete all objects. Am I right in thinking the framework doesn't provide anything to deal with these scenarios? In which case can you suggest an approach to this?

What about something like a ValidationRules.AddDeleteRule and ValidationRules.CheckDeleteRules()?

Thanks

JoeFallon1 replied on Friday, June 19, 2009

I am not sure you need a framework level solution. Depending on the scope of this issue it is something you can put into a single root BO or in your Base class which inherits from CSLA and from which all your BOs derive.

Just add a property to your BO named CanDelete As Boolean. In that property you evaluate all the conditions of the given BO and return True or False. Then in your UI you simply ask the bO if CanDelete is True and then either show or enable your delete button.

If you need it in all your BOs then move the property into your Base class.

Joe

dmnc replied on Friday, June 19, 2009

How about overriding the CanExecuteMethod() method to determine whether the user is allowed to delete the object?

public override void Delete()
{
   CanExecuteMethod("Delete", true);
   base.Delete();
}

public override bool CanExecuteMethod(string methodName)
{
    if (methodName == "Delete") {                   
        return ReadProperty(OwnerIdProperty) == Csla.ApplicationContext.User.Identity.Name && ReadProperty(StatusProperty) != "live";
    }
          
    return base.CanExecuteMethod(methodName);
}

JonnyBee replied on Saturday, June 20, 2009

Hi,

I agree with dmnc but I would check the base command first before custom checks:

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

   if (methodName == "Delete")
   {
        return ReadProperty(OwnerIdProperty) == Csla.ApplicationContext.User.Identity.Name && ReadProperty(StatusProperty) != "live";
    }
}

I also apply the same rule for any overloads of CanReadProperty and CanWriteProperty.

 /jonnybee

Copyright (c) Marimer LLC