Pete
Pete

Reputation: 2463

Scalable Business Logic Rules

I am looking for a way to create a scalable sales order project where it is easy to append new business rules.

public class OrderLine
{
    public int OrderId { get; set; }
    public int Line { get; set; }
    public string Product { get; set; }
    public string Description { get; set; }
    public decimal Quantity { get; set; }
    public int LeadTimeDays { get; set; }        
    public DateTime ShipDate { get; set; }
}

What are best practices for creating business rules to check that an order line is valid? And, is there an easy way to apply multiple rules without adding a check method for each one?

public static class OrderLineChecks
{
    public static void CheckLeadTime(this OrderLine orderLine)
    {
        if( (orderLine.ShipDate - DateTime.Today).TotalDays < orderLine.LeadTimeDays )
            throw new Exception("Order is within lead time.");
    }

    public static void CheckShipDateError(this OrderLine orderLine)
    {
        if(orderLine.ShipDate < DateTime.Today)
            throw new Exception("Ship date cannot be before today.");
    }
    public static void ShouldBeOrderedInPairs(this OrderLine orderLine)
    {
        if(orderLine.Description.Contains("pair") && (orderLine.Quantity % 2 !=0))
            throw new Exception("Quantities must be even numbers.");
    }
    public static NextFutureRuleHere(...)
    {
    }
}

Thanks for your advice.

Upvotes: 2

Views: 531

Answers (2)

Radu094
Radu094

Reputation: 28414

Depending on your requirements it might be overkill, but have you considered writing up a DSL with Rhino DSL & Boo . Quite easy to expand / maintain a set of business rules even by non-programmers if you code it right.

Upvotes: 0

anAgent
anAgent

Reputation: 2780

Check out Fluent Validation: http://fluentvalidation.codeplex.com/

Following how this framework implements rules should help you work out what I think you're looking for.

Upvotes: 1

Related Questions