Link to home
Start Free TrialLog in
Avatar of pzozulka
pzozulka

asked on

OOP Design Help

I have a c# code file (.cs) called MyReport.cs. Inside I have two classes, both public, called MyReport, and PositiveDecimalRule. The code is below.

The class name PositiveDecimalRule was a poor choice for a name in this situation because the rules inside are particular to MyReport only.

We now have a need to create a PositiveDecimalRule class that is more appropriate to all other reports in the same project.

Without renaming, is there another way too approach this problem? Also, ValidationRuleBase class is an abstract class, if that helps.

// MyReport.cs
public class MyReport : I1ActiveReport
{
	protected override void DoPrepareReport(Report report, ReportParameter sourceParameter)
	{
		...
		
		ReportParameter costOfFunds = ReportParameter.NewParameter("CostOfFunds", "Cost of Funds", 7, ReportParameterType.Decimal, 1, 1, false);
		costOfFunds.ParameterValues.Rules.Add(new PositiveDecimalRule());
		report.Parameters.Add(costOfFunds);
		
		...
	}
}

public class PositiveDecimalRule : ValidationRuleBase
{
	public override bool IsDataTypeSupported(RuleDataType ruleDataType)
	{
		if (ruleDataType is DecimalCollection)
			return true;
		return false;
	}

	protected override ValidationResultBase RunValidationOnValue(RuleDataType ruleDataType,
																 string friendlyFieldName,
																 RuleSettings settings)
	{
		DecimalCollection list = ruleDataType as DecimalCollection;
		if (list == null || list.Count == 0)
			return null;

		foreach (Decimal value in list)
		{
			string message = string.Empty;

			if (value < 1 && friendlyFieldName == "Monthly Cost Per Account")
			{
				message = "Must be a value greater than zero";
				return new ValidationResult(friendlyFieldName, message, ruleDataType, ValidationResultSeverity.Error);
			}
			else if (value < 0 && friendlyFieldName == "Cost of Funds")
			{
				message = "Must be a value greater than or equal to zero";
				return new ValidationResult(friendlyFieldName, message, ruleDataType, ValidationResultSeverity.Error);
			}
			
		}
		return null;
	}
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of sarabande
sarabande
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial