0
|
1 using System;
|
|
2 using System.Collections.Generic;
|
|
3
|
|
4 using NUnit.Framework;
|
|
5
|
|
6 using BLToolkit.Data;
|
|
7 using BLToolkit.DataAccess;
|
|
8 using BLToolkit.Mapping;
|
|
9 using BLToolkit.Reflection;
|
|
10
|
|
11 namespace HowTo.DataAccess
|
|
12 {
|
|
13 [TestFixture]
|
|
14 public class PersonAccessorTest
|
|
15 {
|
|
16 public enum Gender
|
|
17 {
|
|
18 [MapValue("F")] Female,
|
|
19 [MapValue("M")] Male,
|
|
20 [MapValue("U")] Unknown,
|
|
21 [MapValue("O")] Other
|
|
22 }
|
|
23
|
|
24 public abstract class Person
|
|
25 {
|
|
26 [MapField("PersonID"), PrimaryKey, NonUpdatable]
|
|
27 public abstract int ID { get; }
|
|
28
|
|
29 public abstract string LastName { get; set; }
|
|
30 public abstract string FirstName { get; set; }
|
|
31 public abstract string MiddleName { get; set; }
|
|
32 public abstract Gender Gender { get; set; }
|
|
33
|
|
34 public static Person CreateInstance()
|
|
35 {
|
|
36 return TypeAccessor<Person>.CreateInstanceEx();
|
|
37 }
|
|
38 }
|
|
39
|
|
40 public abstract class PersonAccessor : DataAccessor<Person>
|
|
41 {
|
|
42 [SprocName("Person_SelectByKey")]
|
|
43 public abstract Person SelectByID (int id);
|
|
44
|
|
45 public abstract Person SelectByName(Person person);
|
|
46 public abstract Person SelectByName(string firstName, string lastName);
|
|
47
|
|
48 public abstract int Insert (Person person);
|
|
49
|
|
50 [ActionName("SelectByKey")]
|
|
51 public abstract Person SelectByID (DbManager db, int id);
|
|
52
|
|
53 public abstract Person SelectByName(DbManager db, Person person);
|
|
54 public abstract Person SelectByName(DbManager db, string firstName, string lastName);
|
|
55
|
|
56 public abstract int Insert (DbManager db, Person person);
|
|
57
|
|
58 public readonly SprocQuery<Person> Query = new SprocQuery<Person>();
|
|
59 }
|
|
60
|
|
61 [Test]
|
|
62 public void Test()
|
|
63 {
|
|
64 PersonAccessor pa = DataAccessor.CreateInstance<PersonAccessor>();
|
|
65
|
|
66 // Insert and get id.
|
|
67 //
|
|
68 Person person = Person.CreateInstance();
|
|
69
|
|
70 person.FirstName = "Crazy";
|
|
71 person.LastName = "Frog";
|
|
72 person.Gender = Gender.Unknown;
|
|
73
|
|
74 int id = pa.Insert(person);
|
|
75
|
|
76 person = pa.SelectByID(id);
|
|
77
|
|
78 TypeAccessor.WriteConsole(person);
|
|
79 Assert.IsNotNull(person);
|
|
80
|
|
81 // Update.
|
|
82 //
|
|
83 person.Gender = Gender.Other;
|
|
84
|
|
85 pa.Query.Update(person);
|
|
86
|
|
87 person = pa.SelectByID(person.ID);
|
|
88
|
|
89 TypeAccessor.WriteConsole(person);
|
|
90 Assert.AreEqual(Gender.Other, person.Gender);
|
|
91
|
|
92 // Delete.
|
|
93 //
|
|
94 pa.Query.Delete(person);
|
|
95
|
|
96 person = pa.SelectByID(person.ID);
|
|
97
|
|
98 Assert.IsNull(person);
|
|
99
|
|
100 // Get All.
|
|
101 //
|
|
102 List<Person> list = pa.Query.SelectAll();
|
|
103
|
|
104 foreach (Person p in list)
|
|
105 TypeAccessor.WriteConsole(p);
|
|
106 }
|
|
107
|
|
108 [Test]
|
|
109 public void TransactionTest()
|
|
110 {
|
|
111 using (DbManager db = new DbManager())
|
|
112 {
|
|
113 PersonAccessor pa = DataAccessor.CreateInstance<PersonAccessor>();
|
|
114
|
|
115 db.BeginTransaction();
|
|
116
|
|
117 // Insert and get id.
|
|
118 //
|
|
119 Person person = Person.CreateInstance();
|
|
120
|
|
121 person.FirstName = "Crazy";
|
|
122 person.LastName = "Frog";
|
|
123 person.Gender = Gender.Unknown;
|
|
124
|
|
125 int id = pa.Insert(db, person);
|
|
126
|
|
127 person = pa.SelectByID(db, id);
|
|
128
|
|
129 TypeAccessor.WriteConsole(person);
|
|
130 Assert.IsNotNull(person);
|
|
131
|
|
132 // Update.
|
|
133 //
|
|
134 person.Gender = Gender.Other;
|
|
135
|
|
136 pa.Query.Update(db, person);
|
|
137
|
|
138 person = pa.SelectByID(db, person.ID);
|
|
139
|
|
140 TypeAccessor.WriteConsole(person);
|
|
141 Assert.AreEqual(Gender.Other, person.Gender);
|
|
142
|
|
143 // Delete.
|
|
144 //
|
|
145 pa.Query.Delete(db, person);
|
|
146
|
|
147 person = pa.SelectByID(db, person.ID);
|
|
148
|
|
149 Assert.IsNull(person);
|
|
150
|
|
151 db.CommitTransaction();
|
|
152
|
|
153 // Get All.
|
|
154 //
|
|
155 List<Person> list = pa.Query.SelectAll(db);
|
|
156
|
|
157 foreach (Person p in list)
|
|
158 TypeAccessor.WriteConsole(p);
|
|
159 }
|
|
160 }
|
|
161 }
|
|
162 }
|
|
163
|