A purchase order has an order ID, a customer ID, total price and line items. Line items can be a product or membership type.
Business rules:
- If the purchase order contains a membership, it has to be activated in the customer account immediately.
- If the purchase order contains Comprehensive First Aid Training video then Basic First Aid training video is added to the purchase order
public abstract class Product
{
}
public class BookClubMembership : Product
{
public bool IsActivated { get; set; }
}
Using the strategy design pattern, i define the first rule as follows
public class BookClubMembershipRule : IPurchaseOrderRule
{
readonly ICustomerActivationService _customerActivationService;
public BookClubMembershipRule(ICustomerActivationService customerActivationService)
{
_customerActivationService = customerActivationService;
}
public void Execute(PurchaseOrder order)
{
foreach(var product in order.Items) {
if (product is BookClubMembership)
{
_customerActivationService.ActivateBookMembership(order.CustomerId, product as BookClubMembership);
break;
}
}
}
}
public class PurchaseOrderProcessor
{
public IList<IPurchaseOrderRule> Rules { get; }
public PurchaseOrderProcessor(List<IPurchaseOrderRule> rules)
{
Rules = rules;
}
public void ProcessOrder(PurchaseOrder order)
{
foreach (var rule in Rules)
rule.Execute(order);
}
I dont feel comfortable using C# is
and as
above. Is there a better way for me to program these rules. I was thinking of Visitor pattern but given the 2nd rule. I will need to operate on the order and the item as well.