Skip to content

Commit d8e80ce

Browse files
authored
adds Database class to ObjectRelationalMapping.cs
It was very confusing for me, to do this exercise as I was not able to check how the Database class looks like. I could not check the logic, whether I have to impelement some of the part, which were already in Database class. In the end, the exercise was easy, but I had to download it locally and check the tests. I suggest to add there how the Database is implemented.
1 parent 9637c54 commit d8e80ce

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

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

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,59 @@ public void Commit()
2222
throw new NotImplementedException($"Please implement the Orm.Commit() method");
2323
}
2424
}
25+
26+
// **** please do not modify the Database class ****
27+
public class Database : IDisposable
28+
{
29+
public enum State { TransactionStarted, DataWritten, Invalid, Closed }
30+
31+
public State DbState { get; private set; } = State.Closed;
32+
public string lastData = string.Empty;
33+
34+
public void BeginTransaction()
35+
{
36+
if (DbState != State.Closed)
37+
{
38+
throw new InvalidOperationException();
39+
}
40+
DbState = State.TransactionStarted;
41+
}
42+
43+
public void Write(string data)
44+
{
45+
if (DbState != State.TransactionStarted)
46+
{
47+
throw new InvalidOperationException();
48+
}
49+
// this does something significant with the db transaction object
50+
lastData = data;
51+
if (data == "bad write")
52+
{
53+
DbState = State.Invalid;
54+
throw new InvalidOperationException();
55+
}
56+
57+
DbState = State.DataWritten;
58+
}
59+
60+
public void EndTransaction()
61+
{
62+
if (DbState != State.DataWritten && DbState != State.TransactionStarted)
63+
{
64+
throw new InvalidOperationException();
65+
}
66+
// this does something significant to end the db transaction object
67+
if (lastData == "bad commit")
68+
{
69+
DbState = State.Invalid;
70+
throw new InvalidOperationException();
71+
}
72+
73+
DbState = State.Closed;
74+
}
75+
76+
public void Dispose()
77+
{
78+
DbState = State.Closed;
79+
}
80+
}

0 commit comments

Comments
 (0)