Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,59 @@ public void Commit()
throw new NotImplementedException($"Please implement the Orm.Commit() method");
}
}

// **** please do not modify the Database class ****
public class Database : IDisposable
{
public enum State { TransactionStarted, DataWritten, Invalid, Closed }

public State DbState { get; private set; } = State.Closed;
public string lastData = string.Empty;

public void BeginTransaction()
{
if (DbState != State.Closed)
{
throw new InvalidOperationException();
}
DbState = State.TransactionStarted;
}

public void Write(string data)
{
if (DbState != State.TransactionStarted)
{
throw new InvalidOperationException();
}
// this does something significant with the db transaction object
lastData = data;
if (data == "bad write")
{
DbState = State.Invalid;
throw new InvalidOperationException();
}

DbState = State.DataWritten;
}

public void EndTransaction()
{
if (DbState != State.DataWritten && DbState != State.TransactionStarted)
{
throw new InvalidOperationException();
}
// this does something significant to end the db transaction object
if (lastData == "bad commit")
{
DbState = State.Invalid;
throw new InvalidOperationException();
}

DbState = State.Closed;
}

public void Dispose()
{
DbState = State.Closed;
}
}