SHOW:
|
|
- or go back to the newest paste.
1 | class Program | |
2 | { | |
3 | public static List<int> HashCodes = new List<int>(); | |
4 | ||
5 | static void Main(string[] args) | |
6 | { | |
7 | RunTest<StandardKernel>(() => | |
8 | { | |
9 | var kernel = new Ninject.StandardKernel(); | |
10 | return () => kernel.Get<One>(); | |
11 | }); | |
12 | ||
13 | RunTest<UnityContainer>(() => | |
14 | { | |
15 | var container = new UnityContainer(); | |
16 | return () => container.Resolve<One>(); | |
17 | }); | |
18 | ||
19 | RunTest<DefaultKernel>(() => | |
20 | { | |
21 | var kernel = new DefaultKernel(); | |
22 | kernel.Register(Classes.FromThisAssembly() | |
23 | .Where(t => true) | |
24 | .WithService.DefaultInterfaces() | |
25 | .LifestyleTransient()); | |
26 | ||
27 | return () => kernel.Resolve<One>(); | |
28 | }); | |
29 | ||
30 | RunTest<Container>(() => | |
31 | { | |
32 | var container = new Container(); | |
33 | return () => container.GetInstance<One>(); | |
34 | }); | |
35 | ||
36 | Console.ReadLine(); | |
37 | ||
38 | } | |
39 | ||
40 | ||
41 | static void RunTest<T>(Func<Func<One>> config) | |
42 | { | |
43 | PrintType<T>(); | |
44 | var resolve = config(); | |
45 | ||
46 | HashCodes.Clear(); | |
47 | resolve(); | |
48 | ||
49 | if (ReferenceCount() == 1) | |
50 | { | |
51 | Console.WriteLine("References are same! Running Resolve again"); | |
52 | resolve(); | |
53 | if (ReferenceCount() == 1) | |
54 | Console.WriteLine("The container is faulty using Singleton scope"); | |
55 | else | |
56 | { | |
57 | Console.WriteLine("The container is only true transient per call to Resolve"); | |
58 | } | |
59 | } | |
60 | else | |
61 | Console.WriteLine("References are different"); | |
62 | ||
63 | ||
64 | } | |
65 | ||
66 | private static int ReferenceCount() | |
67 | { | |
68 | return HashCodes.GroupBy(c => c).Count(); | |
69 | } | |
70 | ||
71 | static void PrintType<T>() | |
72 | { | |
73 | Console.WriteLine(string.Format("{0}{1}", Environment.NewLine, typeof(T).FullName)); | |
74 | } | |
75 | } | |
76 | ||
77 | public class One | |
78 | { | |
79 | public One(Three three, Two two) | |
80 | { | |
81 | Program.HashCodes.Add(three.GetHashCode()); | |
82 | } | |
83 | } | |
84 | ||
85 | ||
86 | public class Two | |
87 | { | |
88 | public Two(Three three) | |
89 | { | |
90 | Program.HashCodes.Add(three.GetHashCode()); | |
91 | } | |
92 | } | |
93 | ||
94 | public class Three | |
95 | { | |
96 | ||
97 | } |