2.1 and shared rules.

2.1 and shared rules.

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


skagen00 posted on Tuesday, September 12, 2006

I have a rule here:

private bool IsValidEmailAddress(object target, Csla.Validation.RuleArgs e)

{

   if (!Regex.IsMatch(_address, "\\w+([-+.']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*"))

   {

      e.Description = Resources.InvalidEmailFormat;

      return false;

   }

   return true;

}

Nothing too flashy. I've got to admit I'm not entirely sure I understand, after reading the release notes, whether I need to give additional attention to this rule when moving to 2.1.

It appears that everything immediately works fine after moving to 2.1, but I have seen notes discussing using static rule methods for type rules, and my rule is still an instance method.

I'm going to dig into some of the old posts but if someone is able to articulate the needed modifications when moving to 2.1, I'd appreciate it!

 

ajj3085 replied on Tuesday, September 12, 2006

Well, here's the thing to watch for.  Are you adding that rule with AddRule, or AddInstanceRule?  If you're still using the former, you'll have problems.   The FIRST object will be the one checked, even if its the second object which had its email changed.

You can keep this as an instance method, but you'll need to change the AddRule to AddInstanceRule.  Alternately (and what I'd recommend) is simply to make the rule method static.  You can also use the Generic AddRule method so that you'll get your type instead of the generic object passed as target.

HTH
Andy

skagen00 replied on Tuesday, September 12, 2006

OK, I think I understand what you're saying. That's a tricky one. It's a good thing I'm in a process that has me visiting all of my classes anyways.

Taking what you said, let me throw out what I've changed it to - is this in the right spirit?

        /// <summary>
        /// Method to add custom validation rules that are not generated.
        /// </summary>
        private void AddCustomRules()
        {
            // Email address needs to be the appropriate format.
            ValidationRules.AddRule<EmailAddress>(IsValidEmailAddress, "Address");
        }

        /// <summary>
        /// Validation rule to test the format of the e-mail address.
        /// </summary>
        private static bool IsValidEmailAddress(EmailAddress target, Csla.Validation.RuleArgs e)
        {
            if (!Regex.IsMatch(target._address, "\\w+([-+.']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*"))
            {
                e.Description = Resources.InvalidEmailFormat;
                return false;
            }
            return true;
        }

ajj3085 replied on Tuesday, September 12, 2006

Looks fine to me.. although I'm not sure why the private method at add rules.  Is there a reason you can't override AddBusinessRules (or AddInstanceBusinessRules)?

Otherwise, looks ok to me.

skagen00 replied on Tuesday, September 12, 2006

I'm using partial classes with the C# templates - so AddBusinessRules() is declared in the generated code, and then uses a delegate and that's where my method comes into play - adding business rules that aren't supported by the generated code from the templates at this point.

Thanks for the input!

ajj3085 replied on Tuesday, September 12, 2006

Makes prefect sense to me.

What do you generate your code from?  Some kind of class diagram?

skagen00 replied on Tuesday, September 12, 2006

I generate it from the C# CodeSmith templates with some modifications.

I use the XML source option, which isn't the primary option, but it allows me to be a little bit more robust on what I can do with it, including such things as "property include files" to include "base class properties" for multiple concrete classes instead of duplicating them, provide means to add XML attributes for summary and remarks comments so my gen code is documented, etc.  I just think the XML route is long term a little more powerful.

I had dreams of it generating the stored procedures for me too but I've set it aside for now. I did not take the CSLA Gen approach at the time because it seemed like the Codesmith C# templates were more widely adopted and also because I don't think there were C# 2.0 templates available for CSLA Gen at the time. 

RockfordLhotka replied on Tuesday, September 12, 2006

Yes, your second method should work fine. The first method will, in the next release (beta) of 2.1, throw an exception at AddRule() - specifically to avoid the subtle bug you'd otherwise have introduced into your code.

ajj3085 replied on Tuesday, September 12, 2006

Opps... I forgot you were going to add that feature.

ajj3085 replied on Tuesday, September 12, 2006

I haven't looked at CS yet, maybe I should.

I have a custom app that generates procs. The catch is they are VERY basic.. which for me is fine.  I don't really want procs to be dependant on each other and wanted all the logic in the BOs.

Basically it uses Ado.net to get the table schema, then generates some Xml to represent it.  It then runs the Xml through a few Xslts to generate the create proc script.  Works very well.  I spend 99% of my time now in the BO design and UI layers.  The db is pretty simple since all I do is create my tables, then unlease my generator (which also creates classes in my DAL for the selected tables).

Copyright (c) Marimer LLC