comparison HowTo/DataAccess/ExecuteDictionary.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.Common;
7 using BLToolkit.DataAccess;
8 using BLToolkit.Mapping;
9
10 namespace HowTo.DataAccess
11 {
12 [TestFixture]
13 public class ExecuteDictionary1
14 {
15 public class Person
16 {
17 [MapField("PersonID"), /*[a]*/PrimaryKey/*[/a]*/]
18 public int /*[a]*/ID/*[/a]*/;
19
20 public string LastName;
21 public string FirstName;
22 public string MiddleName;
23 }
24
25 public abstract class /*[a]*/PersonAccessor/*[/a]*/ : /*[a]*/DataAccessor/*[/a]*/<Person>
26 {
27 // This method uses Person class primary key information.
28 //
29 [ActionName("SelectAll")]
30 public abstract /*[a]*/Dictionary<int,Person>/*[/a]*/ GetPersonDictionary1();
31
32 // Define index field explicitly. "ID" is a field name of the Person class.
33 //
34 [ActionName("SelectAll")]
35 [/*[a]*/Index("ID")/*[/a]*/]
36 public abstract /*[a]*/Dictionary<int,Person>/*[/a]*/ GetPersonDictionary2();
37
38 // Define index field explicitly. "@PersonID" is a recordset field.
39 // Note that the '@' symbol enforces the library to read index value
40 // from recordset (not from object).
41 //
42 [ActionName("SelectAll")]
43 [/*[a]*/Index("@PersonID")/*[/a]*/]
44 public abstract /*[a]*/Dictionary<int,Person>/*[/a]*/ GetPersonDictionary3();
45
46 // This method reads a dictionary containing scalar values.
47 //
48 [SqlQuery("SELECT PersonID, FirstName FROM Person")]
49 [/*[a]*/Index("PersonID")/*[/a]*/]
50 [/*[a]*/ScalarFieldName("FirstName")/*[/a]*/]
51 public abstract /*[a]*/Dictionary<int,string>/*[/a]*/ GetPersonNameDictionary();
52 }
53
54 [Test]
55 public void Test()
56 {
57 PersonAccessor pa = DataAccessor.CreateInstance<PersonAccessor>();
58
59 // ExecuteDictionary.
60 //
61 Dictionary<int,Person> dic;
62
63 dic = pa.GetPersonDictionary1();
64 dic = pa.GetPersonDictionary2();
65 dic = pa.GetPersonDictionary3();
66
67 foreach (int id in dic.Keys)
68 Console.WriteLine("{0}: {1} {2}", id, dic[id].FirstName, dic[id].LastName);
69
70 // ExecuteScalarDictionary.
71 //
72 Dictionary<int,string> sdic = pa.GetPersonNameDictionary();
73
74 foreach (int id in dic.Keys)
75 Console.WriteLine("{0}: {1}", id, sdic[id]);
76 }
77 }
78
79 [TestFixture]
80 public class ExecuteDictionary2
81 {
82 // This example demonstrates how to use a multiple field key.
83 //
84 public class Person
85 {
86 [/*[a]*/PrimaryKey(1)/*[/a]*/, MapField("PersonID")]
87 public int ID;
88 [/*[a]*/PrimaryKey(2)/*[/a]*/]
89 public string LastName;
90
91 public string FirstName;
92 public string MiddleName;
93 }
94
95 public abstract class /*[a]*/PersonAccessor/*[/a]*/ : /*[a]*/DataAccessor/*[/a]*/<Person>
96 {
97 // This method uses Person class primary key information.
98 // Note that the key type of the dictionary must be of /*[a]*/IndexValue/*[/a]*/ type.
99 // It is required if the index consists of more than one element.
100 //
101 [ActionName("SelectAll")]
102 public abstract /*[a]*/Dictionary<CompoundValue,Person>/*[/a]*/ GetPersonDictionary();
103
104 // This method reads a dictionary containing scalar values.
105 //
106 [SqlQuery("SELECT PersonID, LastName, FirstName FROM Person")]
107 [/*[a]*/Index("PersonID", "LastName")/*[/a]*/]
108 [/*[a]*/ScalarFieldName("FirstName")/*[/a]*/]
109 public abstract /*[a]*/Dictionary<CompoundValue,string>/*[/a]*/ GetPersonNameDictionary();
110 }
111
112 [Test]
113 public void Test()
114 {
115 PersonAccessor pa = DataAccessor.CreateInstance<PersonAccessor>();
116
117 // ExecuteDictionary.
118 //
119 Dictionary<CompoundValue,Person> dic = pa.GetPersonDictionary();
120
121 foreach (CompoundValue idx in dic.Keys)
122 Console.WriteLine("{0}: {1} {2}", dic[idx].ID, dic[idx].FirstName, dic[idx].LastName);
123
124 // ExecuteScalarDictionary.
125 //
126 Dictionary<CompoundValue,string> sdic = pa.GetPersonNameDictionary();
127
128 string firstName = sdic[new CompoundValue(2, "Testerson")];
129
130 Assert.AreEqual("Tester", firstName);
131 }
132 }
133 }