good day, i am an absolute beginner trying to figure out CSLA. i am struggling to grasp how to check business rules and display errors in my UI.
in my Customer class i have the following:
public class Customer : BusinessBase<Customer>
{
public static PropertyInfo<string> NameProperty = RegisterProperty<string>(c => c.Name);
public string Name
{
get { return GetProperty(NameProperty); }
set { SetProperty(NameProperty, value); }
}
#region business rules
protected override void AddBusinessRules()
{
base.AddBusinessRules();
BusinessRules.AddRule(new Required(NameProperty) { MessageText = "Name Required" });
}
#endregion //business rules
}
i have setup all my properties eg. Name, LastName, etc. and have added the above 'AddBusinessRules' no i'm stumped on where to actually check these rules or break them is the property does not meet the rule and how to display a message on my UI if these rules are broken.
do i require another class to handle it?
any help would be much appreciated.
Look at the sample projects
Net\cs\BusinessRuleDemo
Net\cs\RuleTutorial
Basically:
How you visualize broken rules in the UI will depend on which UI technology you use.
I highly recommend the 3 first Using CSLA 4 ebooks by Rocky.
http://download.lhotka.net/Default.aspx?t=UsingCsla4
http://store.lhotka.net/Default.aspx?tabid=1560&ProductID=22
Hi Jonny.
thanks for your reply.
ok that makes sense, i see that my rule is being checked when creating my customer. nut i see now that my rule is not actually broken when i create a new customer, only after i change the property 'Name' and take out all of its contents so that it is empty is my object not valid, why is that? shouldnt the rullebe broken to begin with seen that there is nothing in the 'Name' property? how would i make sure that the rule is broken to begin with?
im using WinForms so what would be the best way to get a list of all broken rules and display on my UI form? i tried this but i dont seem to get the desired results back.
public List<string> CheckRules()
{
base.CheckObjectRules();
List<string> msgs = new List<string>();
foreach (var rule in this.BrokenRulesCollection)
{
msgs.Add(rule.Description);
}
return msgs;
}
Hi,
Use the builtin ErrorProvider in Windows Forms or the ErrorWarnInfoProvider available on http://cslacontrib.codeplex.com to vusalize the broken rules in your application.
How do you create your object?
Do you call DataPortal.Create() and do you have code in DataPortal_Create method?
In My Customer Class i have the following Method:
public static Customer NewCustomer()
{
return DataPortal.Create<Customer>();
}
which in turn calls a method in my CustomerFactory class:
public Customer Create()
{
var obj = (Customer)MethodCaller.CreateInstance(typeof(Customer));
LoadProperty(obj, Customer.NameProperty, "");
MarkNew(obj);
return obj;
}
is this the incorrect way to do this? also re the broken rules, i would like to display the list of broken rules on a label is that possible. please excuse all the silly questions, I'm really new to CSLA (in fact i only start looking at it about 3 days ago)
In order to check rules you must call CheckRules(obj) in your create method.
Fantastic!!! JonnyBee you are a legend!
thanks so much for your help on this. I'm sure I'll be coming back soon for more help.
Copyright (c) Marimer LLC