Examples of using custom rules using Generics

Examples of using custom rules using Generics

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


boo posted on Friday, March 02, 2007

I cannot for the life of me figure out how to implement customer rules using generics, for example we have a type that needs to have a MaxLength check on (similiar to string, but not the same) - the only way I can currently get this to work is to use the non-generic way of adding a rule.  I want to use generics to have strongly typed values and don't have to use reflection to pull the property value as I am below:

public static bool CustomTypeMaxLength(object target, RuleArgs e)

{

CustomType.CustomTypeMaxLength r = (CustomType.CustomTypeMaxLength)e;

CustomType propValue = (CustomType)target.GetType().GetProperty(e.PropertyName).GetValue(target, null);

return propValue.SomeValue.Length <= r.MaxLength;

}

Every time I go to use generic class with rule args I can't add the rule without a compile or run-time casting error.  I consider myself pretty good with Generics and am matching the signatures for what is want best I can tell; but there's something I'm missing.  Does anyone have some examples of the customer rule, the customer rule args - one that preferably has to be set when it's added, not a static rule, and how it's added to the business rules collection?

RockfordLhotka replied on Friday, March 02, 2007

I almost hate to ask, but did you look at the CSLA .NET Version 2.1 Handbook? I put examples in there to cover this Big Smile [:D]

The rule method itself must accept type parameters for target and/or e.

private static MyRule<T>(T target, RuleArgs e)
  where T: CustomType

private static MyRule<T,R>(T target, R e)
  where T: CustomType
  where R:CustomTypeMaxLength

Then you use the overloads of AddRule() that accept the same type parameters. The actual type parameter value is specified by your call to AddRule():

ValidationRules.AddRule<CustomType, CustomTypeMaxLength>(...);

 

boo replied on Friday, March 02, 2007

My boss said it's on order.  I don't know if it actually is though...you know how that can go.  Thanks

RockfordLhotka replied on Friday, March 02, 2007

So you know, it is an instant download when you buy it. So if “on order” means it is going through your internal purchasing process that’s cool. But if “on order” means you are waiting for delivery, or waiting for email instructions for a download, then that’s a problem. If that’s the case, to resolve the problem, send an email to admin@lhotka.net with either the store or paypal receipt for the purchase to get a copy emailed to you.

 

Rocky

 

From: boo [mailto:cslanet@lhotka.net]
Sent: Friday, March 02, 2007 11:31 AM
To: rocky@lhotka.net
Subject: Re: [CSLA .NET] Examples of using custom rules using Generics

 

My boss said it's on order.  I don't know if it actually is though...you know how that can go.  Thanks

boo replied on Friday, March 02, 2007

I'm thinking I'll just buy it myself at this point...politics can really delay things unecessarily.

Copyright (c) Marimer LLC