RuleHandler Delegate

RuleHandler Delegate

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


decius posted on Friday, October 03, 2008

I've somehow managed to create a rule that contains no arguments 0_o.  thus, in the GetRuleDescriptions() method of ValidationRules.cs, the uri of the rule is an empty string and throws an index out of range exception in the RuleDescription constructor like so:

public RuleDescription(string ruleString)

{

Uri uri = new Uri(ruleString);

_scheme = uri.GetLeftPart(UriPartial.Scheme);

_methodName = uri.Host;

_propertyName = uri.LocalPath.Substring(1);

string args = uri.Query;

_arguments = new Dictionary<string, string>();

string[] argArray = args.Split('&');

foreach (string arg in argArray)

{

string[] argParams = arg.Split('=');

_arguments.Add(

Uri.UnescapeDataString(argParams[0]),

Uri.UnescapeDataString(argParams[1]));  //right here!!

}

}

I think I'm setting up the rule incorrectly to begin with, i don't think i'm using the RuleHandler delegate correctly, though I tried duping the ProjectTracker StartDate EndDate example of a custom rule.  here's my code, any help is greatly appreciated!!!!

private static bool CodeOrText<T>(

T target, Csla.Validation.RuleArgs e) where T : CodesCriteria

{

if (!DoMyValidation()) 

{

e.Description = "My description";

return false;

}

else

{

return true;

}

}

 

JoeFallon1 replied on Friday, October 03, 2008

Please show the code from AddBusinessRules where you add this rule.

 

RockfordLhotka replied on Friday, October 03, 2008

What does your AddRule() code look like?

decius replied on Friday, October 03, 2008

ah sorry!  I meant to include that

ValidationRules.AddRule<CodesCriteria>(CodeOrText<CodesCriteria>, "Code");

ValidationRules.AddDependantProperty("Code", "Text", true);

RockfordLhotka replied on Friday, October 03, 2008

It should be

 

ValidationRules.AddRule<CodesCriteria>(CodeOrText, “Code”);

 

I don’t know if that’ll fix your problem, but I suspect the second generic type is a problem.

 

Rocky

 

decius replied on Friday, October 03, 2008

Thanks for the reply.  No unfortunately that didn't solve the problem.  Sadly, my slim knowledge and understanding of delegates has made this hard for me to overcome.

A little more info that might shine some light on things: GetRuleDescriptions() is getting called from a Custom Control I built.  It is getting called during the CreateChildControls() method of that Custom Control and calls (via reflection) a private static method of the CodesCriteria class like so:

 

private static string[] GetRuleDescriptions()

{

return new CodesCriteria().ValidationRules.GetRuleDescriptions();

}

 

I'm not sure if this order of event would affect things or not.  As far as I can tell, this is irrelevant, as this control works fine with other common rules and such, it's only this attempt at a custom rule that it throws up.

 

EDIT:  Yet another interesting piece of information....  the Target property of RuleMethod.Handler is null even after the rule has been invoked inside RuleMethod.cs.... is that normal?

 

 

decius replied on Monday, October 06, 2008

still stuck on this.  Anyone have any other suggestions?

RockfordLhotka replied on Monday, October 06, 2008

I would break that code up into separate lines so you can establish (with the debugger) exactly what is failing. Make sure it is CSLA not returning the rule, not your code failing to process it properly.

decius replied on Monday, October 06, 2008

Thanks for your reply Rocky.  Yes, I have even tried calling GetRuleDescriptions directly from the UI after the object gets created.  when stepping through the code with the debugger, everything appears to make sense.  However, when Csla tries to read the Query string value of the rule Uri, Uri.Query ends up null for each of these custom rules.  I notice that this exception is not handled here, so I assume that custom rules should still end up with a Uri.Query value, is this correct??  if so what would determine that value?

RockfordLhotka replied on Monday, October 06, 2008

The query part of the rule:// URI comes from the RuleArgs object.

 

If you have a custom RuleArgs subclass you need to override ToString() to provide a meaningful value.

 

Perhaps somehow you don’t have a valid RuleArgs object?

 

Rocky

 

 

From: decius [mailto:cslanet@lhotka.net]
Sent: Monday, October 06, 2008 9:16 AM
To: rocky@lhotka.net
Subject: Re: [CSLA .NET] RE: RuleHandler Delegate

 

Thanks for your reply Rocky.  Yes, I have even tried calling GetRuleDescriptions directly from the UI after the object gets created.  when stepping through the code with the debugger, everything appears to make sense.  However, when Csla tries to read the Query string value of the rule Uri, Uri.Query ends up null for each of these custom rules.  I notice that this exception is not handled here, so I assume that custom rules should still end up with a Uri.Query value, is this correct??  if so what would determine that value?

Copyright (c) Marimer LLC