-
Notifications
You must be signed in to change notification settings - Fork 5
/
Invariants_Suspend.cs
46 lines (38 loc) · 1.2 KB
/
Invariants_Suspend.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
// This is public domain Metalama sample code.
using Metalama.Patterns.Contracts;
namespace Doc.Invariants_Suspend
{
public partial class Invoice
{
public decimal Amount { get; set; }
[Range( 0, 100 )]
public int DiscountPercent { get; set; }
[Range( 0, 100 )]
public decimal DiscountAmount { get; set; }
public virtual decimal DiscountedAmount => (this.Amount * (100 - this.Amount) / 100m) - this.DiscountAmount;
[Invariant]
private void CheckDiscounts()
{
if ( this.DiscountedAmount < 0 )
{
throw new PostconditionViolationException( "The discounted amount cannot be negative." );
}
}
[SuspendInvariants]
public void UpdateDiscounts1( int percent, decimal amount )
{
this.DiscountAmount = amount;
this.DiscountPercent = percent;
}
public void UpdateDiscounts2( int percent, decimal amount )
{
#if METALAMA
using ( this.SuspendInvariants() )
{
this.DiscountAmount = amount;
this.DiscountPercent = percent;
}
#endif
}
}
}