From 45ae755caefe390707f5a70453d44ef313d90e42 Mon Sep 17 00:00:00 2001 From: Marek J <1138746+tvedeane@users.noreply.github.com> Date: Sun, 27 Apr 2025 10:55:00 +0200 Subject: [PATCH 1/5] Add missing information about accessing DB --- .../.docs/instructions.md | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/exercises/concept/object-relational-mapping/.docs/instructions.md b/exercises/concept/object-relational-mapping/.docs/instructions.md index f704c85bf9..1b0a200e3e 100644 --- a/exercises/concept/object-relational-mapping/.docs/instructions.md +++ b/exercises/concept/object-relational-mapping/.docs/instructions.md @@ -10,11 +10,20 @@ Note that, internally, the database transitions through a number of state: Close The database has the following instance methods: -- `Database.BeginTransaction()` starts a transaction on the database. If this is called when the database is not in a `Closed` state then an exception is thrown. If successful the internal state of the database will change to `TransactionStarted`. -- `Database.Write(string data)` writes data to the database within the transaction. If it receives bad data an exception will be thrown. An attempt to call this method without `BeginTransaction()` having been called will cause an exception to be thrown. If successful the internal state of the database will change to `DataWritten`. -- `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. +- `Database.Begin()` starts a transaction on the database. If this is called when the database is not in a `Closed` state then an exception is thrown. If successful the internal state of the database will change to `TransactionStarted`. +- `Database.Write(string data)` writes data to the database within the transaction. If it receives bad data an exception will be thrown. An attempt to call this method without `Begin()` having been called will cause an exception to be thrown. If successful the internal state of the database will change to `DataWritten`. +- `Database.EndTransaction()` commits the transaction to the database. It may throw an exception if it can't close the transaction or if `Database.Begin()` 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`. From 0ffcc21c1a005745c7a264208d5f140df36f2d6f Mon Sep 17 00:00:00 2001 From: tvedeane <1138746+tvedeane@users.noreply.github.com> Date: Tue, 29 Apr 2025 18:15:36 +0200 Subject: [PATCH 2/5] Move Database to its own file --- .../.meta/config.json | 3 +- .../object-relational-mapping/Database.cs | 54 ++++++++++++++++++ .../ObjectRelationalMappingTests.cs | 55 ------------------- 3 files changed, 56 insertions(+), 56 deletions(-) create mode 100644 exercises/concept/object-relational-mapping/Database.cs diff --git a/exercises/concept/object-relational-mapping/.meta/config.json b/exercises/concept/object-relational-mapping/.meta/config.json index 63fca32d88..1358cbfff1 100644 --- a/exercises/concept/object-relational-mapping/.meta/config.json +++ b/exercises/concept/object-relational-mapping/.meta/config.json @@ -11,7 +11,8 @@ "ObjectRelationalMapping.cs" ], "test": [ - "ObjectRelationalMappingTests.cs" + "ObjectRelationalMappingTests.cs", + "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; - } -} From 402d1c69ba73eefc4ebdbe54d818f438819408bf Mon Sep 17 00:00:00 2001 From: Marek J <1138746+tvedeane@users.noreply.github.com> Date: Tue, 29 Apr 2025 20:26:47 +0200 Subject: [PATCH 3/5] Configure Database.cs correctly Co-authored-by: Erik Schierboom --- exercises/concept/object-relational-mapping/.meta/config.json | 2 ++ 1 file changed, 2 insertions(+) diff --git a/exercises/concept/object-relational-mapping/.meta/config.json b/exercises/concept/object-relational-mapping/.meta/config.json index 1358cbfff1..9561d88d30 100644 --- a/exercises/concept/object-relational-mapping/.meta/config.json +++ b/exercises/concept/object-relational-mapping/.meta/config.json @@ -12,6 +12,8 @@ ], "test": [ "ObjectRelationalMappingTests.cs", + ], + "invalidator": [ "Database.cs" ], "exemplar": [ From 531f39074b6c901d6f55e45bbcf8bfacc67b97eb Mon Sep 17 00:00:00 2001 From: tvedeane <1138746+tvedeane@users.noreply.github.com> Date: Tue, 29 Apr 2025 20:28:42 +0200 Subject: [PATCH 4/5] Undo BeginTransaction renaming --- .../concept/object-relational-mapping/.docs/instructions.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/exercises/concept/object-relational-mapping/.docs/instructions.md b/exercises/concept/object-relational-mapping/.docs/instructions.md index 1b0a200e3e..0adf01d1b6 100644 --- a/exercises/concept/object-relational-mapping/.docs/instructions.md +++ b/exercises/concept/object-relational-mapping/.docs/instructions.md @@ -10,9 +10,9 @@ Note that, internally, the database transitions through a number of state: Close The database has the following instance methods: -- `Database.Begin()` starts a transaction on the database. If this is called when the database is not in a `Closed` state then an exception is thrown. If successful the internal state of the database will change to `TransactionStarted`. -- `Database.Write(string data)` writes data to the database within the transaction. If it receives bad data an exception will be thrown. An attempt to call this method without `Begin()` having been called will cause an exception to be thrown. If successful the internal state of the database will change to `DataWritten`. -- `Database.EndTransaction()` commits the transaction to the database. It may throw an exception if it can't close the transaction or if `Database.Begin()` had not been called. +- `Database.BeginTransaction()` starts a transaction on the database. If this is called when the database is not in a `Closed` state then an exception is thrown. If successful the internal state of the database will change to `TransactionStarted`. +- `Database.Write(string data)` writes data to the database within the transaction. If it receives bad data an exception will be thrown. An attempt to call this method without `BeginTransaction()` having been called will cause an exception to be thrown. If successful the internal state of the database will change to `DataWritten`. +- `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`. From b540a167fb9930ba8b60134ad2018761c88a27b3 Mon Sep 17 00:00:00 2001 From: Marek J <1138746+tvedeane@users.noreply.github.com> Date: Tue, 29 Apr 2025 22:14:17 +0200 Subject: [PATCH 5/5] Fix unnecessary comma Co-authored-by: Erik Schierboom --- exercises/concept/object-relational-mapping/.meta/config.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exercises/concept/object-relational-mapping/.meta/config.json b/exercises/concept/object-relational-mapping/.meta/config.json index 9561d88d30..2200b12131 100644 --- a/exercises/concept/object-relational-mapping/.meta/config.json +++ b/exercises/concept/object-relational-mapping/.meta/config.json @@ -11,7 +11,7 @@ "ObjectRelationalMapping.cs" ], "test": [ - "ObjectRelationalMappingTests.cs", + "ObjectRelationalMappingTests.cs" ], "invalidator": [ "Database.cs"