comparison HowTo/Data/SetSpCommand.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.Data;
7 using BLToolkit.Mapping;
8
9 namespace HowTo.Data
10 {
11 using DataAccess;
12
13 [TestFixture]
14 public class SetSpCommand
15 {
16 // Select a person list.
17 //
18 public IList<Person> GetPersonList()
19 {
20 using (DbManager db = new DbManager())
21 {
22 return db
23 ./*[a]*/SetSpCommand/*[/a]*/("Person_SelectAll")
24 .ExecuteList<Person>();
25 }
26 }
27
28 [Test]
29 public void Test1()
30 {
31 IList<Person> list = GetPersonList();
32
33 Assert.AreNotEqual(0, list.Count);
34 }
35
36 // Select a person.
37 //
38 public Person GetPersonByID1(int id)
39 {
40 using (DbManager db = new DbManager())
41 {
42 // Pass a parameter using the [b]Parameter[/b] method.
43 //
44 return db
45 ./*[a]*/SetSpCommand/*[/a]*/("Person_SelectByKey",
46 db./*[a]*/Parameter/*[/a]*/("@id", id))
47 .ExecuteObject<Person>();
48 }
49 }
50
51 public Person GetPersonByID2(int id)
52 {
53 using (DbManager db = new DbManager())
54 {
55 // Pass a parameter using the [b]params[/b] parameter of the SetSpCommand method.
56 //
57 return db
58 ./*[a]*/SetSpCommand/*[/a]*/("Person_SelectByKey", /*[a]*/id/*[/a]*/)
59 .ExecuteObject<Person>();
60 }
61 }
62
63 [Test]
64 public void Test2()
65 {
66 Person person = GetPersonByID1(1);
67 Assert.IsNotNull(person);
68
69 person = GetPersonByID2(1);
70 Assert.IsNotNull(person);
71 }
72
73 // Insert, Update, and Delete a person.
74 //
75 public Person GetPersonByID(DbManager db, int id)
76 {
77 return db
78 ./*[a]*/SetSpCommand/*[/a]*/("Person_SelectByKey", /*[a]*/id/*[/a]*/)
79 .ExecuteObject<Person>();
80 }
81
82 public Person CreatePerson(DbManager db)
83 {
84 int id = db
85 ./*[a]*/SetSpCommand/*[/a]*/("Person_Insert",
86 db./*[a]*/Parameter/*[/a]*/("@LastName", "Frog"),
87 db./*[a]*/Parameter/*[/a]*/("@MiddleName", null),
88 db./*[a]*/Parameter/*[/a]*/("@FirstName", "Crazy"),
89 db./*[a]*/Parameter/*[/a]*/("@Gender", Map.EnumToValue(Gender.Male)))
90 .ExecuteScalar<int>();
91
92 return GetPersonByID(db, id);
93 }
94
95 public Person UpdatePerson(DbManager db, Person person)
96 {
97 db
98 ./*[a]*/SetSpCommand/*[/a]*/("Person_Update", db./*[a]*/CreateParameters/*[/a]*/(person))
99 .ExecuteNonQuery();
100
101 return GetPersonByID(db, person.ID);
102 }
103
104 public Person DeletePerson(DbManager db, Person person)
105 {
106 db
107 ./*[a]*/SetSpCommand/*[/a]*/("Person_Delete", /*[a]*/person.ID/*[/a]*/)
108 .ExecuteNonQuery();
109
110 return GetPersonByID(db, person.ID);
111 }
112
113 [Test]
114 public void Test3()
115 {
116 using (DbManager db = new DbManager())
117 {
118 db.BeginTransaction();
119
120 // Insert.
121 //
122 Person person = CreatePerson(db);
123
124 Assert.IsNotNull(person);
125
126 // Update.
127 //
128 Assert.AreEqual(Gender.Male, person.Gender);
129
130 person.Gender = Gender.Female;
131
132 person = UpdatePerson(db, person);
133
134 Assert.AreEqual(Gender.Female, person.Gender);
135
136 // Delete.
137 //
138 person = DeletePerson(db, person);
139
140 Assert.IsNull(person);
141
142 db.CommitTransaction();
143 }
144 }
145 }
146 }