Error in CommonRules - Utilities Class

Error in CommonRules - Utilities Class

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


thaehn posted on Thursday, April 16, 2009

The business rules are not being enforced in my business object.  Here is an example of the property declaration:

private static PropertyInfo<string> ShortNameProperty = RegisterProperty(new PropertyInfo<string>("relLoc"));

public string ShortName

{

   get { return GetProperty<string>(ShortNameProperty); }

   set { SetProperty<string>(ShortNameProperty, value); }

}

And here is the rule declaration:

ValidationRules.AddRule(Csla.Validation.CommonRules.StringRequired, new Csla.Validation.RuleArgs(ShortNameProperty));

ValidationRules.AddRule(Csla.Validation.CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs(ShortNameProperty, 4));

When CommonRules tries to evaluate this rule it calls a Utility class called CallByName which executes this code which results in null:

PropertyInfo p = target.GetType().GetProperty(methodName);

where target is my business object and methodName = "relLoc".

The next line creates a null exception and exits the the whole process:

return p.GetValue(target, args);

The rest of the processing does't happen and the object remains valid and allows Save to be called.

Any idea of what I did wrong?

Todd

sergeyb replied on Thursday, April 16, 2009

Your property is not declared properly.  Parameter for new ProeprtyInfo should be actual property name as in

private static PropertyInfo<string> ShortNameProperty = RegisterProperty(new PropertyInfo<string>("ShortName"));

public string ShortName

{

get { return GetProperty<string>(ShortNameProperty); }

set { SetProperty<string>(ShortNameProperty, value); }

}

 

 

 

Sergey Barskiy

Principal Consultant

office: 678.405.0687 | mobile: 404.388.1899

cid:_2_0648EA840648E85C001BBCB886257279
Microsoft Worldwide Partner of the Year | Custom Development Solutions, Technical Innovation

 

From: thaehn [mailto:cslanet@lhotka.net]
Sent: Thursday, April 16, 2009 1:47 PM
To: Sergey Barskiy
Subject: [CSLA .NET] Error in CommonRules - Utilities Class

 

The business rules are not being enforced in my business object.  Here is an example of the property declaration:

private static PropertyInfo<string> ShortNameProperty = RegisterProperty(new PropertyInfo<string>("relLoc"));

public string ShortName

{

get { return GetProperty<string>(ShortNameProperty); }

set { SetProperty<string>(ShortNameProperty, value); }

}

And here is the rule declaration:

ValidationRules.AddRule(Csla.Validation.CommonRules.StringRequired, new Csla.Validation.RuleArgs(ShortNameProperty));

ValidationRules.AddRule(Csla.Validation.CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs(ShortNameProperty, 4));

When CommonRules tries to evaluate this rule it calls a Utility class called CallByName which executes this code which results in null:

PropertyInfo p = target.GetType().GetProperty(methodName);

where target is my business object and methodName = "relLoc".

The next line creates a null exception and exits the the whole process:

return p.GetValue(target, args);

The rest of the processing does't happen and the object remains valid and allows Save to be called.

Any idea of what I did wrong?

Todd



nermin replied on Thursday, April 16, 2009

Or (in case that you are on 3.6.2+) you could use lambda expression like

private static PropertyInfo ShortNameProperty = RegisterProperty(p->p.ShortName);

and that lambda expression gets the correct property name into registration.



________________________________

From: Sergey Barskiy [mailto:cslanet@lhotka.net]
Sent: Thu 4/16/2009 2:14 PM
To: Nermin Dibek
Subject: RE: [CSLA .NET] Error in CommonRules - Utilities Class



Your property is not declared properly. Parameter for new ProeprtyInfo should be actual property name as in

private static PropertyInfo ShortNameProperty = RegisterProperty(new PropertyInfo("ShortName"));

public string ShortName

{

get { return GetProperty(ShortNameProperty); }

set { SetProperty(ShortNameProperty, value); }

}







Sergey Barskiy

Principal Consultant

office: 678.405.0687 | mobile: 404.388.1899

cid:_2_0648EA840648E85C001BBCB886257279
Microsoft Worldwide Partner of the Year | Custom Development Solutions, Technical Innovation



From: thaehn [mailto:cslanet@lhotka.net]
Sent: Thursday, April 16, 2009 1:47 PM
To: Sergey Barskiy
Subject: [CSLA .NET] Error in CommonRules - Utilities Class



The business rules are not being enforced in my business object. Here is an example of the property declaration:

private static PropertyInfo ShortNameProperty = RegisterProperty(new PropertyInfo("relLoc"));

public string ShortName

{

get { return GetProperty(ShortNameProperty); }

set { SetProperty(ShortNameProperty, value); }

}

And here is the rule declaration:

ValidationRules.AddRule(Csla.Validation.CommonRules.StringRequired, new Csla.Validation.RuleArgs(ShortNameProperty));

ValidationRules.AddRule(Csla.Validation.CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs(ShortNameProperty, 4));

When CommonRules tries to evaluate this rule it calls a Utility class called CallByName which executes this code which results in null:

PropertyInfo p = target.GetType().GetProperty(methodName);

where target is my business object and methodName = "relLoc".

The next line creates a null exception and exits the the whole process:

return p.GetValue(target, args);

The rest of the processing does't happen and the object remains valid and allows Save to be called.

Any idea of what I did wrong?

Todd







thaehn replied on Thursday, April 16, 2009

Sergey,

The name of the column in the database is "relLoc", but the property name is "ShortName".  Do they have to be the same?  Do I need to change "ShortName" to "relLoc"?

Todd

ajj3085 replied on Friday, April 17, 2009

What you name your properties on your class has nothing to do with your database. You take care of that mapping in the DataPortal_XyZ methods when you load and save data.

Name the property by what your users will reconize.

ajj3085 replied on Thursday, April 16, 2009

I don't think it's a bug; I think it's a requirement that the string name you pass to RegisterProperty matches the actual name of the property on your class. So change the literal to ShortName and it will work.

Copyright (c) Marimer LLC