diff --git a/exercises/concept/object-relational-mapping/.docs/instructions.md b/exercises/concept/object-relational-mapping/.docs/instructions.md index f704c85bf9..0adf01d1b6 100644 --- a/exercises/concept/object-relational-mapping/.docs/instructions.md +++ b/exercises/concept/object-relational-mapping/.docs/instructions.md @@ -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`. diff --git a/exercises/concept/object-relational-mapping/.meta/config.json b/exercises/concept/object-relational-mapping/.meta/config.json index 63fca32d88..2200b12131 100644 --- a/exercises/concept/object-relational-mapping/.meta/config.json +++ b/exercises/concept/object-relational-mapping/.meta/config.json @@ -13,6 +13,9 @@ "test": [ "ObjectRelationalMappingTests.cs" ], + "invalidator": [ + "Database.cs" + ], "exemplar": [ ".meta/Exemplar.cs" ], diff --git a/exercises/concept/object-relational-mapping/Database.cs b/exercises/concept/object-relational-mapping/Database.cs new file mode 100644 index 0000000000..e5d9b201a6 --- /dev/null +++ b/exercises/concept/object-relational-mapping/Database.cs @@ -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; + } +} diff --git a/exercises/concept/object-relational-mapping/ObjectRelationalMappingTests.cs b/exercises/concept/object-relational-mapping/ObjectRelationalMappingTests.cs index c5efee0d12..7dc897393b 100644 --- a/exercises/concept/object-relational-mapping/ObjectRelationalMappingTests.cs +++ b/exercises/concept/object-relational-mapping/ObjectRelationalMappingTests.cs @@ -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; - } -}