0
|
1 using System;
|
|
2
|
|
3 using NUnit.Framework;
|
|
4
|
|
5 using BLToolkit.Data;
|
|
6 using BLToolkit.DataAccess;
|
|
7 using BLToolkit.Mapping;
|
|
8
|
|
9 namespace HowTo.DataAccess
|
|
10 {
|
|
11 [TestFixture]
|
|
12 public class Transaction
|
|
13 {
|
|
14 public abstract class TestAccessor : DataAccessor<Person>
|
|
15 {
|
|
16 public abstract int Insert(Person person);
|
|
17 public abstract void Delete(int @PersonID);
|
|
18
|
|
19 public abstract Person SelectByKey(int id);
|
|
20 public abstract Person SelectByKey(/*[a]*/DbManager/*[/a]*/ db, int id);
|
|
21 }
|
|
22
|
|
23 // /*[i]*/DataAccessor/*[/i]*/ takes /*[i]*/DbManager/*[/i]*/ as a parameter.
|
|
24 //
|
|
25 [Test]
|
|
26 public void Test1()
|
|
27 {
|
|
28 using (DbManager db = new DbManager())
|
|
29 {
|
|
30 TestAccessor ta = DataAccessor.CreateInstance<TestAccessor>/*[a]*/(db)/*[/a]*/;
|
|
31
|
|
32 ta./*[a]*/BeginTransaction/*[/a]*/();
|
|
33
|
|
34 int id = ta.Insert(new Person { FirstName = "John", LastName = "Smith" });
|
|
35 Assert.AreNotEqual(0, id);
|
|
36
|
|
37 Person person = ta.SelectByKey(id);
|
|
38 Assert.IsNotNull(person);
|
|
39
|
|
40 ta.Delete(id);
|
|
41
|
|
42 ta./*[a]*/CommitTransaction/*[/a]*/();
|
|
43 }
|
|
44 }
|
|
45
|
|
46 // /*[i]*/DataAccessor/*[/i]*/ method takes /*[i]*/DbManager/*[/i]*/ as a parameter.
|
|
47 //
|
|
48 [Test]
|
|
49 public void Test2()
|
|
50 {
|
|
51 using (DbManager db = new DbManager())
|
|
52 {
|
|
53 db.BeginTransaction();
|
|
54
|
|
55 TestAccessor ta = DataAccessor.CreateInstance<TestAccessor>/*[a]*/()/*[/a]*/;
|
|
56
|
|
57 int id = ta.Insert(new Person { FirstName = "John", LastName = "Smith" });
|
|
58 Assert.AreNotEqual(0, id);
|
|
59
|
|
60 Person person = ta.SelectByKey(/*[a]*/db/*[/a]*/, id);
|
|
61 Assert.IsNotNull(person);
|
|
62
|
|
63 ta.Delete(id);
|
|
64
|
|
65 db.CommitTransaction();
|
|
66 }
|
|
67 }
|
|
68 }
|
|
69 }
|
|
70
|