sharing about .NET and technology RSS 2.0
 Thursday, March 01, 2007

CSLA.NET framework from Lhotka contains a lot of mechanisms for adding validations and business rules. Through CSLA.NET you can easily provide your own custom rules. Enterprise Library v3.0 now also contains a validation application block (VAB) that can be used through attributes and even from a configuration file.

The two validation mechanisms of validation are complementary. This can be done by adding a custom rule that uses the ValidationFactory of the VAB. This means we have something like:

Copy Code
public class VABRules { public class VABRuleArgs : RuleArgs { private string _ruleset; public string Ruleset { get { return _ruleset; } } public VABRuleArgs(string propertyName) : this(propertyName, null) { } public VABRuleArgs(string propertyName, string ruleset) : base(propertyName) { _ruleset = ruleset; } } public static bool VABValid<T>(object target, RuleArgs e) { Validator<T> validator = ValidationFactory.CreateValidator<T>(((VABRuleArgs)e).Ruleset); if (validator == null) return true; ValidationResults results = validator.Validate(target); if (results == null) return true; foreach (ValidationResult result in results) { if (result.Key == e.PropertyName) { e.Description = result.Message; return false; } } return true; } }

Having the VAB rule we simply need to decorate our properties with the validation attributes of VAB and an override of the AddBusinessRules method is needed to take into account the VAB rules. For example we can define a customer business object as follow:

Copy Code
[Serializable()] public class Customer : Csla.BusinessBase<Customer> { private int _id = 0; private string _firstName = string.Empty; private string _email = string.Empty; private int _rewardPoints; private string _countryCode = string.Empty; [Browsable(false), System.ComponentModel.DataObjectField(true, true)] public int Id { get { CanReadProperty("Id", true); return _id; } } [NotNullValidator(MessageTemplate="First Name may not be empty")] [StringLengthValidator(1, 60, MessageTemplate = "First Name must be between 1 and 60 characters long")] public string FirstName { get { CanReadProperty("FirstName", true); return _firstName; } set { CanWriteProperty("FirstName", true); if (!_firstName.Equals(value)) { _firstName = value; PropertyHasChanged("FirstName"); } } } [RegexValidator(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*")] public string Email { get { CanReadProperty("Email", true); return _email; } set { CanWriteProperty("Email", true); if (!_email.Equals(value)) { _email = value; PropertyHasChanged("Email"); } } } [Int32RangeValidator(0, 1000000, MessageTemplate = "Rewards points cannot exceed 1,000,000")] public int RewardPoints { get { CanReadProperty("RewardPoints", true); return _rewardPoints; } set { CanWriteProperty("RewardPoints", true); if (!_rewardPoints.Equals(value)) { _rewardPoints = value; PropertyHasChanged("RewardPoints"); } } } [NotNullValidator(MessageTemplate = "Country may not be empty")] public string CountryCode { get { CanReadProperty("CountryCode", true); return _countryCode; } set { CanWriteProperty("CountryCode", true); if (!_countryCode.Equals(value)) { _countryCode = value; PropertyHasChanged("CountryCode"); } } } protected override object GetIdValue() { return _id; } protected override void AddBusinessRules() { ValidationRules.AddRule(VABRules.VABValid<Customer>, new VABRules.VABRuleArgs("FirstName")); ValidationRules.AddRule(VABRules.VABValid<Customer>, new VABRules.VABRuleArgs("Email")); ValidationRules.AddRule(VABRules.VABValid<Customer>, new VABRules.VABRuleArgs("RewardPoints")); ValidationRules.AddRule(VABRules.VABValid<Customer>, new VABRules.VABRuleArgs("CountryCode")); } }
Thursday, March 01, 2007 2:39:25 PM (Romance Standard Time, UTC+01:00)  #    Comments [6] -

Thursday, March 22, 2007 4:24:04 PM (Romance Standard Time, UTC+01:00)
Hi,

WOuld you share the soruce code of your Component Dropper ?

thanks
Alex
Thursday, September 06, 2007 2:57:50 PM (Romance Standard Time, UTC+01:00)
Reading the article is interesting, and I agree with the conclusion. Firms of all sizes, no longer expect to be totally dependent on their own in-house research and technical resources to maintain innovative performance in the global business environment. Companies increasingly decide to outsource parts of their technological and underlying knowledge requirements to other firms and organisations. How they articulate these technological requirements, co-ordinate and absorb them with their own in-house knowledge capabilities is of fundamental importance to their competitive success in the knowledge economy.
Tuesday, September 25, 2007 3:51:31 PM (Romance Standard Time, UTC+01:00)
hi ,

this way its not working out with csla .
can u post an working code sample of the same .

thanks
jsy
Thursday, November 15, 2007 3:13:56 PM (Romance Standard Time, UTC+01:00)
Hi,

I have one question regarding the Validation Block.

We are using Subsonic or Mygeneration to generate the Business Objects. Whenever there is changes in data, we are regenerating and replacing the same.
Suppose we use attribute in the above scenario, we have problem in updating the generated classes. Is there any other way around to do the same without using Attribute and that would be integrated with validationsummary of asp.net class.

Regards,
Manish Pansiniya
Tuesday, August 19, 2008 8:15:33 AM (Romance Standard Time, UTC+01:00)
ok, good!
share best site for you:
wow gold http://www.wowgolds.co.uk
广州电脑维修 http://www.boweipc.cn
Tuesday, August 19, 2008 8:16:14 AM (Romance Standard Time, UTC+01:00)
ok, good!
share best site for you:
wow gold http://www.wowgolds.co.uk
广州电脑维修 http://www.boweipc.cn
Name
E-mail
Home page

Comment (HTML not allowed)  

Enter the code shown (prevents robots):

Navigation
Archive
<November 2008>
SunMonTueWedThuFriSat
2627282930311
2345678
9101112131415
16171819202122
23242526272829
30123456
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008
Christoph De Baene
Sign In
Statistics
Total Posts: 151
This Year: 22
This Month: 1
This Week: 0
Comments: 147
All Content © 2008, Christoph De Baene
DasBlog theme 'Business' created by Christoph De Baene (delarou)