-
Notifications
You must be signed in to change notification settings - Fork 0
/
Order.cs
55 lines (47 loc) · 2.04 KB
/
Order.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SpecificationPattern.Specifications;
namespace SpecificationPattern
{
/// <summary>
/// Order Business Entity
/// </summary>
public class Order
{
public PaymentMode ModeOfPayment { get; set; }
public virtual Address ShippingAddress { get; set; }
public int SKUs { get; set; }
public int Discount;
public bool AllowDiscount;
public decimal OrderAmount { get; set; }
public bool IsTaxApplicable { get; set; }
/// <summary>
/// Checks the order for whether discount is applicable or not for this order.
/// This method is written for explanation purpose. This method is example of how, business rules are lost inside the application
/// with if-else constructs and also repeated all across application.
/// </summary>
/// <returns>success,if discount is applicable for this order otherwise,false.</returns>
public bool HasDiscountWithoutSpecification()
{
if (SKUs >= 10000 && ModeOfPayment == PaymentMode.CashOnDelivery && ShippingAddress.Country == "USA" && OrderAmount >= 50000 && IsTaxApplicable == false)
{
AllowDiscount = true;
}
return AllowDiscount;
}
/// <summary>
/// Checks the order for whether discount is applicable or not for this order.
/// This method demonstrates how specification pattern is used inside actual code.
/// Compare implementation of this method with that of "HasDiscountWithoutSpecifiaction".
/// Using Specification pattern, business rules are concentrated at one place and it is easy to manage them.
/// </summary>
/// <returns>success,if discount is applicable for this order otherwise,false.</returns>
public bool HasDiscountWithSpecification()
{
var spec = new DiscountSpecification();
return spec.IsSatisfiedBy(this);
}
}
}