Mercurial > pub > bltoolkit
comparison HowTo/Data/ComplexMapping.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 using System.Collections.Generic; | |
3 | |
4 using NUnit.Framework; | |
5 | |
6 using BLToolkit.Mapping; | |
7 using BLToolkit.Data; | |
8 using BLToolkit.DataAccess; | |
9 using BLToolkit.Reflection.Extension; | |
10 | |
11 namespace HowTo.Data | |
12 { | |
13 [TestFixture] | |
14 public class ComplexMapping | |
15 { | |
16 const string TestQuery = @" | |
17 -- Parent Data | |
18 SELECT 1 as ParentID | |
19 UNION SELECT 2 as ParentID | |
20 | |
21 -- Child Data | |
22 SELECT 4 ChildID, 1 as ParentID | |
23 UNION SELECT 5 ChildID, 2 as ParentID | |
24 UNION SELECT 6 ChildID, 2 as ParentID | |
25 UNION SELECT 7 ChildID, 1 as ParentID | |
26 | |
27 -- Grandchild Data | |
28 SELECT 1 GrandchildID, 4 as ChildID | |
29 UNION SELECT 2 GrandchildID, 4 as ChildID | |
30 UNION SELECT 3 GrandchildID, 5 as ChildID | |
31 UNION SELECT 4 GrandchildID, 5 as ChildID | |
32 UNION SELECT 5 GrandchildID, 6 as ChildID | |
33 UNION SELECT 6 GrandchildID, 6 as ChildID | |
34 UNION SELECT 7 GrandchildID, 7 as ChildID | |
35 UNION SELECT 8 GrandchildID, 7 as ChildID | |
36 "; | |
37 | |
38 public class Parent | |
39 { | |
40 [MapField("ParentID"), /*[a]*/PrimaryKey/*[/a]*/] | |
41 public int ID; | |
42 | |
43 /*[a]*/[Relation(typeof(Child))]/*[/a]*/ | |
44 public List<Child> Children = new List<Child>(); | |
45 } | |
46 | |
47 [MapField("ParentID", "Parent.ID")] | |
48 public class Child | |
49 { | |
50 [MapField("ChildID"), /*[a]*/PrimaryKey/*[/a]*/] | |
51 public int ID; | |
52 | |
53 /*[a]*/[Relation]/*[/a]*/ | |
54 public Parent Parent = new Parent(); | |
55 | |
56 /*[a]*/[Relation(typeof(Grandchild))]/*[/a]*/ | |
57 public List<Grandchild> Grandchildren = new List<Grandchild>(); | |
58 } | |
59 | |
60 [MapField("ChildID", "Child.ID")] | |
61 public class Grandchild | |
62 { | |
63 [MapField("GrandchildID"), /*[a]*/PrimaryKey/*[/a]*/] | |
64 public int ID; | |
65 | |
66 /*[a]*/[Relation]/*[/a]*/ | |
67 public Child Child = new Child(); | |
68 } | |
69 | |
70 [Test] | |
71 public void Test() | |
72 { | |
73 List<Parent> parents = new List<Parent>(); | |
74 /*[a]*/MapResultSet/*[/a]*/[] sets = new MapResultSet[3]; | |
75 | |
76 sets[0] = new MapResultSet(typeof(Parent), parents); | |
77 sets[1] = new MapResultSet(typeof(Child)); | |
78 sets[2] = new MapResultSet(typeof(Grandchild)); | |
79 | |
80 sets[0].AddRelation(sets[1], "ParentID", "ParentID", "Children"); | |
81 sets[1].AddRelation(sets[0], "ParentID", "ParentID", "Parent"); | |
82 | |
83 sets[1].AddRelation(sets[2], "ChildID", "ChildID", "Grandchildren"); | |
84 sets[2].AddRelation(sets[1], "ChildID", "ChildID", "Child"); | |
85 | |
86 using (DbManager db = new DbManager()) | |
87 { | |
88 db | |
89 .SetCommand (TestQuery) | |
90 ./*[a]*/ExecuteResultSet/*[/a]*/(sets); | |
91 } | |
92 | |
93 Assert.IsNotEmpty(parents); | |
94 | |
95 foreach (Parent parent in parents) | |
96 { | |
97 Assert.IsNotNull(parent); | |
98 Assert.IsNotEmpty(parent.Children); | |
99 | |
100 foreach (Child child in parent.Children) | |
101 { | |
102 Assert.AreEqual(parent, child.Parent); | |
103 Assert.IsNotEmpty(child.Grandchildren); | |
104 | |
105 foreach (Grandchild grandchild in child.Grandchildren) | |
106 { | |
107 Assert.AreEqual(child, grandchild.Child); | |
108 Assert.AreEqual(parent, grandchild.Child.Parent); | |
109 } | |
110 } | |
111 } | |
112 } | |
113 | |
114 [Test] | |
115 public void Test2() | |
116 { | |
117 List<Parent> parents = new List<Parent>(); | |
118 /*[a]*/MapResultSet/*[/a]*/[] sets = new MapResultSet[3]; | |
119 | |
120 sets[0] = new MapResultSet(typeof(Parent), parents); | |
121 sets[1] = new MapResultSet(typeof(Child)); | |
122 sets[2] = new MapResultSet(typeof(Grandchild)); | |
123 | |
124 using (DbManager db = new DbManager()) | |
125 { | |
126 db | |
127 .SetCommand(TestQuery) | |
128 ./*[a]*/ExecuteResultSet/*[/a]*/(sets); | |
129 } | |
130 | |
131 Assert.IsNotEmpty(parents); | |
132 | |
133 foreach (Parent parent in parents) | |
134 { | |
135 Assert.IsNotNull(parent); | |
136 Assert.IsNotEmpty(parent.Children); | |
137 | |
138 foreach (Child child in parent.Children) | |
139 { | |
140 Assert.AreEqual(parent, child.Parent); | |
141 Assert.IsNotEmpty(child.Grandchildren); | |
142 | |
143 foreach (Grandchild grandchild in child.Grandchildren) | |
144 { | |
145 Assert.AreEqual(child, grandchild.Child); | |
146 Assert.AreEqual(parent, grandchild.Child.Parent); | |
147 } | |
148 } | |
149 } | |
150 } | |
151 | |
152 public class ParentEx | |
153 { | |
154 public int ID; | |
155 public List<ChildEx> Children = new List<ChildEx>(); | |
156 } | |
157 | |
158 public class ChildEx | |
159 { | |
160 public int ID; | |
161 public ParentEx Parent = new ParentEx(); | |
162 public List<GrandchildEx> Grandchildren = new List<GrandchildEx>(); | |
163 } | |
164 | |
165 public class GrandchildEx | |
166 { | |
167 public int ID; | |
168 public ChildEx Child = new ChildEx(); | |
169 } | |
170 | |
171 static readonly MappingSchema _mappingSchema = new MappingSchema | |
172 { | |
173 Extensions = TypeExtension.GetExtensions("RelationExtension.xml") | |
174 }; | |
175 | |
176 [Test] | |
177 public void Test3() | |
178 { | |
179 var parents = new List<ParentEx>(); | |
180 var sets = new /*[a]*/MapResultSet/*[/a]*/[3]; | |
181 | |
182 sets[0] = new MapResultSet(typeof(ParentEx), parents); | |
183 sets[1] = new MapResultSet(typeof(ChildEx)); | |
184 sets[2] = new MapResultSet(typeof(GrandchildEx)); | |
185 | |
186 using (var db = new DbManager()) | |
187 { | |
188 db.MappingSchema = _mappingSchema; | |
189 | |
190 db | |
191 .SetCommand(TestQuery) | |
192 ./*[a]*/ExecuteResultSet/*[/a]*/(sets); | |
193 } | |
194 | |
195 Assert.IsNotEmpty(parents); | |
196 | |
197 foreach (ParentEx parent in parents) | |
198 { | |
199 Assert.IsNotNull(parent); | |
200 Assert.IsNotEmpty(parent.Children); | |
201 | |
202 foreach (ChildEx child in parent.Children) | |
203 { | |
204 Assert.AreEqual(parent, child.Parent); | |
205 Assert.IsNotEmpty(child.Grandchildren); | |
206 | |
207 foreach (GrandchildEx grandchild in child.Grandchildren) | |
208 { | |
209 Assert.AreEqual(child, grandchild.Child); | |
210 Assert.AreEqual(parent, grandchild.Child.Parent); | |
211 } | |
212 } | |
213 } | |
214 } | |
215 } | |
216 } |