Skip to content

Add missing information about accessing DB #2423

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 29, 2025
Merged
Show file tree
Hide file tree
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 @@ -15,6 +15,15 @@ The database has the following instance methods:
- `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.
- 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`.

The state of the database can be accessed using `database.DbState`.

The state of the database can be set to one of:

- TransactionStarted
- DataWritten
- Invalid
- Closed

## 1. Begin a transaction

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`.
Expand Down
3 changes: 3 additions & 0 deletions exercises/concept/object-relational-mapping/.meta/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
"test": [
"ObjectRelationalMappingTests.cs"
],
"invalidator": [
"Database.cs"
],
"exemplar": [
".meta/Exemplar.cs"
],
Expand Down
54 changes: 54 additions & 0 deletions exercises/concept/object-relational-mapping/Database.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,58 +79,3 @@ public void Disposable()
}
}

// **** 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;
}
}
Loading