Skip to content

Commit 17be731

Browse files
authored
Add missing information about accessing DB (#2423)
1 parent 648ded8 commit 17be731

File tree

4 files changed

+66
-55
lines changed

4 files changed

+66
-55
lines changed

exercises/concept/object-relational-mapping/.docs/instructions.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@ The database has the following instance methods:
1515
- `Database.EndTransaction()` commits the transaction to the database. It may throw an exception if it can't close the transaction or if `Database.BeginTransaction()` had not been called.
1616
- A call to`Database.Dispose()` will clean up the database if an exception is thrown during a transaction. This will change the state of the database to `Closed`.
1717

18+
The state of the database can be accessed using `database.DbState`.
19+
20+
The state of the database can be set to one of:
21+
22+
- TransactionStarted
23+
- DataWritten
24+
- Invalid
25+
- Closed
26+
1827
## 1. Begin a transaction
1928

2029
Implement `Orm.Begin()` to start a transaction on the database. If the database does not start with an internal state of `State.Closed` then it throws an `InvalidOperationException`.

exercises/concept/object-relational-mapping/.meta/config.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
"test": [
1414
"ObjectRelationalMappingTests.cs"
1515
],
16+
"invalidator": [
17+
"Database.cs"
18+
],
1619
"exemplar": [
1720
".meta/Exemplar.cs"
1821
],
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
public class Database : IDisposable
2+
{
3+
public enum State { TransactionStarted, DataWritten, Invalid, Closed }
4+
5+
public State DbState { get; private set; } = State.Closed;
6+
public string lastData = string.Empty;
7+
8+
public void BeginTransaction()
9+
{
10+
if (DbState != State.Closed)
11+
{
12+
throw new InvalidOperationException();
13+
}
14+
DbState = State.TransactionStarted;
15+
}
16+
17+
public void Write(string data)
18+
{
19+
if (DbState != State.TransactionStarted)
20+
{
21+
throw new InvalidOperationException();
22+
}
23+
// this does something significant with the db transaction object
24+
lastData = data;
25+
if (data == "bad write")
26+
{
27+
DbState = State.Invalid;
28+
throw new InvalidOperationException();
29+
}
30+
31+
DbState = State.DataWritten;
32+
}
33+
34+
public void EndTransaction()
35+
{
36+
if (DbState != State.DataWritten && DbState != State.TransactionStarted)
37+
{
38+
throw new InvalidOperationException();
39+
}
40+
// this does something significant to end the db transaction object
41+
if (lastData == "bad commit")
42+
{
43+
DbState = State.Invalid;
44+
throw new InvalidOperationException();
45+
}
46+
47+
DbState = State.Closed;
48+
}
49+
50+
public void Dispose()
51+
{
52+
DbState = State.Closed;
53+
}
54+
}

exercises/concept/object-relational-mapping/ObjectRelationalMappingTests.cs

Lines changed: 0 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -79,58 +79,3 @@ public void Disposable()
7979
}
8080
}
8181

82-
// **** please do not modify the Database class ****
83-
public class Database : IDisposable
84-
{
85-
public enum State { TransactionStarted, DataWritten, Invalid, Closed }
86-
87-
public State DbState { get; private set; } = State.Closed;
88-
public string lastData = string.Empty;
89-
90-
public void BeginTransaction()
91-
{
92-
if (DbState != State.Closed)
93-
{
94-
throw new InvalidOperationException();
95-
}
96-
DbState = State.TransactionStarted;
97-
}
98-
99-
public void Write(string data)
100-
{
101-
if (DbState != State.TransactionStarted)
102-
{
103-
throw new InvalidOperationException();
104-
}
105-
// this does something significant with the db transaction object
106-
lastData = data;
107-
if (data == "bad write")
108-
{
109-
DbState = State.Invalid;
110-
throw new InvalidOperationException();
111-
}
112-
113-
DbState = State.DataWritten;
114-
}
115-
116-
public void EndTransaction()
117-
{
118-
if (DbState != State.DataWritten && DbState != State.TransactionStarted)
119-
{
120-
throw new InvalidOperationException();
121-
}
122-
// this does something significant to end the db transaction object
123-
if (lastData == "bad commit")
124-
{
125-
DbState = State.Invalid;
126-
throw new InvalidOperationException();
127-
}
128-
129-
DbState = State.Closed;
130-
}
131-
132-
public void Dispose()
133-
{
134-
DbState = State.Closed;
135-
}
136-
}

0 commit comments

Comments
 (0)