comparison HowTo/Aspects/ClearCacheAspect.cs @ 0:f990fcb411a9

Копия текущей версии из github
author cin
date Thu, 27 Mar 2014 21:46:09 +0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:f990fcb411a9
1 using System;
2
3 using NUnit.Framework;
4
5 using BLToolkit.Aspects;
6 using BLToolkit.Reflection;
7
8 namespace HowTo.Aspects
9 {
10 [TestFixture]
11 public class ClearCacheAspect
12 {
13 public /*[a]*/abstract/*[/a]*/ class TestClass
14 {
15 public static int Value;
16
17 // This is a method we will cache. Cached return value depends on input parameters.
18 // We will change the 'Value' field outside of the class and see how it affects the result.
19 //
20 [/*[a]*/Cache/*[/a]*/(MaxCacheTime=500, IsWeak=false)]
21 public /*[a]*/virtual/*[/a]*/ int CachedMethod(int p1, int p2)
22 {
23 return Value;
24 }
25
26 // This method clears the CachedMethod cache.
27 //
28 [/*[a]*/ClearCache/*[/a]*/(/*[a]*/"CachedMethod"/*[/a]*/)]
29 public abstract void ClearCache();
30
31 // The CachedMethod is specified by name and parameters.
32 // Also you can use declaring method type.
33 //
34 [/*[a]*/ClearCache/*[/a]*/(/*[a]*/"CachedMethod"/*[/a]*/, /*[a]*/typeof(int), typeof(int)/*[/a]*/)]
35 public abstract void ClearCache2();
36
37 // This method clears all caches for provided type.
38 //
39 [/*[a]*/ClearCache/*[/a]*/(/*[a]*/typeof(TestClass)/*[/a]*/)]
40 public abstract void ClearAll();
41
42 // This method clears all caches for current type.
43 //
44 [/*[a]*/ClearCache/*[/a]*/]
45 public abstract void ClearAll2();
46
47 public static TestClass CreateInstance()
48 {
49 // Use TypeAccessor to create an instance of an abstract class.
50 //
51 return /*[a]*/TypeAccessor/*[/a]*/<TestClass>.CreateInstance();
52 }
53 }
54
55 [Test]
56 public void Test()
57 {
58 TestClass tc = TypeAccessor<TestClass>.CreateInstance();
59
60 TestClass.Value = 1;
61
62 int value1 = tc.CachedMethod(1, 2);
63
64 TestClass.Value = 2;
65
66 int value2 = tc.CachedMethod(1, 2);
67
68 // The cached values are equal.
69 //
70 Assert.AreEqual(value1, value2);
71
72 tc.ClearCache();
73
74 TestClass.Value = 3;
75
76 // Previous and returned values are not equal.
77 //
78 Assert.AreNotEqual(value1, tc.CachedMethod(1, 2));
79
80 tc.ClearCache2();
81 }
82 }
83 }