Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Validate A Class
Message
De
05/04/2010 10:11:54
 
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Divers
Thread ID:
01458686
Message ID:
01458723
Vues:
45
I could have sworn that I've posted about this before here, but I've searched and I can't find the thread. So, I guess I'll post it again. My approach is a hybrid, the Rules class is standalone and not visual, but each visual component on a Form (TextBox, etc.) implements methods that check the rule that it's bound to and show non-compliance visually (by turning the control Red). Also, a caveat here ... I used DataSets for DataBinding, you're apparently not, so yours will look a lot different than what I show below.

What I've done in the past, is to have the base classes automatically check for Rules compliance (typically in the control's OnValidating method) and set the color of the control accordingly. So, for example, the MyTextBox class would have these two methods, called from OnValidating:
public virtual void CheckRule()
{
	try
	{
		if (this.Parent is IMyContainer && this.oBinding != null)
		{
			DataTable table = (DataTable)this.oBinding.DataSource;
			string field = this.oBinding.BindingMemberInfo.BindingField;
			int row = this.BindingContext[table].Position;

			if (((IMyContainer)this.Parent).CheckRule(table, field, row) > 0)
				this.IsRed = true;
			else
				this.IsRed = false;
		}
		else
			this.IsRed = false;
	} 
	catch (Exception)
	{
		this.IsRed = false;
	}
}
public virtual void CheckColors()
{
	if (this.IsRed)
	{
		if (this.Enabled == true)
		{
			this.BackColor = System.Drawing.Color.Red;
			this.ForeColor = System.Drawing.Color.White;
		}
		else
		{
			this.BackColor = System.Drawing.SystemColors.Control;
			this.ForeColor = System.Drawing.Color.Red;
		}
	}
	else
	{
		if (this.Enabled == true)
		{
			this.BackColor = System.Drawing.SystemColors.Window;
			this.ForeColor = System.Drawing.SystemColors.WindowText;
		}
		else
		{
			this.BackColor = System.Drawing.SystemColors.Control;
			this.ForeColor = System.Drawing.SystemColors.WindowText;
		}
	}
}
The CheckRule() method in each Parent container basically passes the call up through each Parent (which can be things like Panel, GroupBox, Usercontrol, etc.) until it gets to the top (the Form). The base class for the Form doesn't do anything, that's up to the individual sub-classed Forms. We have a pretty complicated Rules class which contains a RulesCollection that's basically populated from the database from the Form. But, here's an example of how a Form might handle the CheckRule method, using the oRules collection. The RulesCollection.CheckRule() method does all the work.:
public override int CheckRule(DataTable data, string field, int row)
{
	return this.oRules.CheckRule(data, field, row);
}
You can also loop through every Rule comparing against the DataSet.
protected virtual int CheckRules(DataSet ds, MyRule rule)
{
	int num = 0;

	if (rule.Table.DataSet.Equals(ds) && ds.HasChanges())
	{
		for (int it = 0; it < ds.Tables.Count; it++)
			if (rule.Table.Equals(ds.Tables[it]))
				for (int ir = 0; ir < ds.Tables[it].Rows.Count; ir++)
					if (ds.Tables[it].Rows[ir].RowState != DataRowState.Unchanged)
						num += this.CheckRule(ds.Tables[it], rule.Field, ir);
	}

	return num;
}
The above method would be called in your Form's Save method, like this:
if (this.oData.HasChanges())
{
	for (int i = 0; i < this.oRules.Count; i++)
		num += this.CheckRules(this.oData, this.oRules[i]);
}
Anyway, that's enough for now. Enough for you to get the picture I hope.
Bonnie Berent DeWitt
NET/C# MVP since 2003

http://geek-goddess-bonnie.blogspot.com
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform