Databases Reference
In-Depth Information
value of the OrderStatus property— ValidateDraftOrder , ValidateSubmittedOrder ,
ValidatePaidOrder , and ValidateFufilledOrder —that are called from a switch statement
by the main Validate method. With this approach, the validation rules for Submitted
orders, which are quite different from the validation rules for Draft orders, are imple-
mented in two different methods and do not conflict with each other.
LISTING 9.1 Partial Order Class with State-Specific Validation Methods
partial class Order : IValidatableObject
{
public IEnumerable<ValidationResult> Validate(ValidationContext context)
{
switch ((DataModel.OrderStatus?)this.OrderStatus)
{
case null:
case DataModel.OrderStatus.Draft:
return this.ValidateDraftOrder(context);
case DataModel.OrderStatus.Submitted:
return this.ValidateSubmittedOrder(context);
case DataModel.OrderStatus.Paid:
return this.ValidatePaidOrder(context);
case DataModel.OrderStatus.Fulfilled:
return this.ValidateFulfilledOrder(context);
default:
Debug.Fail(“Unexpected OrderStatus value”);
return new ValidationResult[0];
}
}
private IEnumerable<ValidationResult> ValidateDraftOrder(ValidationContext c)
{
if (this.Customer == null && string.IsNullOrEmpty(this.CustomerID))
yield return new ValidationResult(
“The Customer is required”, new[] { “Customer” });
}
private IEnumerable<ValidationResult> ValidateSubmittedOrder(ValidationContext c)
{
if (this.OrderDate == null)
yield return new ValidationResult(
“The Order Date is required”, new[] { “OrderDate” });
Search WWH ::




Custom Search