From d5a9e3e608bc10dc0e49816b4d51282c5daed1c0 Mon Sep 17 00:00:00 2001 From: Kenny Pflug Date: Sun, 23 Mar 2025 18:54:33 +0100 Subject: [PATCH] feat!: overhaul DatabaseAccessAbstractions This commit removes all synchronous interfaces and everything that has to do with explicit transactions. IAsyncReadOnlySession is also removed as it will be replaced by .NET's IAsyncDisposable. --- .../IAsyncReadOnlySession.cs | 21 ------------- .../IAsyncSession.cs | 31 ------------------- .../IAsyncTransaction.cs | 19 ------------ .../IAsyncTransactionalSession.cs | 24 -------------- .../DatabaseAccessAbstractions/ISession.cs | 30 +++++++++--------- .../ITransaction.cs | 23 -------------- .../ITransactionalSession.cs | 29 ----------------- 7 files changed, 16 insertions(+), 161 deletions(-) delete mode 100644 Code/Light.SharedCore/DatabaseAccessAbstractions/IAsyncReadOnlySession.cs delete mode 100644 Code/Light.SharedCore/DatabaseAccessAbstractions/IAsyncSession.cs delete mode 100644 Code/Light.SharedCore/DatabaseAccessAbstractions/IAsyncTransaction.cs delete mode 100644 Code/Light.SharedCore/DatabaseAccessAbstractions/IAsyncTransactionalSession.cs delete mode 100644 Code/Light.SharedCore/DatabaseAccessAbstractions/ITransaction.cs delete mode 100644 Code/Light.SharedCore/DatabaseAccessAbstractions/ITransactionalSession.cs diff --git a/Code/Light.SharedCore/DatabaseAccessAbstractions/IAsyncReadOnlySession.cs b/Code/Light.SharedCore/DatabaseAccessAbstractions/IAsyncReadOnlySession.cs deleted file mode 100644 index 6273c5e..0000000 --- a/Code/Light.SharedCore/DatabaseAccessAbstractions/IAsyncReadOnlySession.cs +++ /dev/null @@ -1,21 +0,0 @@ -using System; - -namespace Light.SharedCore.DatabaseAccessAbstractions; - -/// -/// Represents an asynchronous session to the database that -/// is only used to read data. This usually means that no explicit -/// transaction is needed for this session, although implementations can support -/// it to allow using READ UNCOMMITED isolation levels for queries, for example. -/// The connection to the database can be terminated by calling -/// (or ). -/// If you want to manipulate data, use . -/// -/// -/// Conceptually, a session is identical to a "Unit of Work" as defined in "Patterns of -/// Enterprise Application Architecture" by Martin Fowler et al. It manages the connection -/// to the database and represents a transaction. Strictly speaking, a Unit of Work also needs -/// to do Change Tracking which plain ADO.NET and all Micro-ORMs do not support. For this reason, -/// we chose the term "session" instead of "Unit of Work", also because it is simpler to use in daily life. -/// -public interface IAsyncReadOnlySession : IDisposable, IAsyncDisposable; \ No newline at end of file diff --git a/Code/Light.SharedCore/DatabaseAccessAbstractions/IAsyncSession.cs b/Code/Light.SharedCore/DatabaseAccessAbstractions/IAsyncSession.cs deleted file mode 100644 index 32c7564..0000000 --- a/Code/Light.SharedCore/DatabaseAccessAbstractions/IAsyncSession.cs +++ /dev/null @@ -1,31 +0,0 @@ -using System; -using System.Threading; -using System.Threading.Tasks; - -namespace Light.SharedCore.DatabaseAccessAbstractions; - -/// -/// Represents an asynchronous session to a database that manipulates data. This means that -/// the implementation will either use a single dedicated transaction in the case of ADO.NET or Micro-ORMs -/// to ensure ACID properties, or the Change Tracking capabilities of a full ORM like Entity Framework Core. -/// The connection to the database can be terminated by calling -/// (or ), the underlying transaction -/// will be automatically rolled back if was not called beforehand. -/// Changes can be saved or committed to the database by calling . -/// If your session does not manipulate data, consider using the interface instead. -/// -/// -/// Conceptually, a session is identical to a "Unit of Work" as defined in "Patterns of -/// Enterprise Application Architecture" by Martin Fowler et al. It manages the connection -/// to the database and represents a transaction. Strictly speaking, a Unit of Work also needs -/// to do Change Tracking which plain ADO.NET and all Micro-ORMs do not support. For this reason, -/// we chose the term "session" instead of "Unit of Work", also because it is simpler to use in daily life. -/// -public interface IAsyncSession : IAsyncReadOnlySession -{ - /// - /// Commits all changes to the database. - /// - /// The token to cancel this asynchronous operation (optional). - Task SaveChangesAsync(CancellationToken cancellationToken = default); -} \ No newline at end of file diff --git a/Code/Light.SharedCore/DatabaseAccessAbstractions/IAsyncTransaction.cs b/Code/Light.SharedCore/DatabaseAccessAbstractions/IAsyncTransaction.cs deleted file mode 100644 index e8ad148..0000000 --- a/Code/Light.SharedCore/DatabaseAccessAbstractions/IAsyncTransaction.cs +++ /dev/null @@ -1,19 +0,0 @@ -using System; -using System.Threading; -using System.Threading.Tasks; - -namespace Light.SharedCore.DatabaseAccessAbstractions; - -/// -/// Represents an asynchronous transaction that can be committed. The transaction should always be disposed. -/// A rollback is performed automatically on when was not -/// called beforehand. -/// -public interface IAsyncTransaction : IAsyncDisposable, IDisposable -{ - /// - /// Commits the changes made during this transaction to the database. - /// - /// The token to cancel this asynchronous operation (optional). - Task CommitAsync(CancellationToken cancellationToken = default); -} \ No newline at end of file diff --git a/Code/Light.SharedCore/DatabaseAccessAbstractions/IAsyncTransactionalSession.cs b/Code/Light.SharedCore/DatabaseAccessAbstractions/IAsyncTransactionalSession.cs deleted file mode 100644 index 4049993..0000000 --- a/Code/Light.SharedCore/DatabaseAccessAbstractions/IAsyncTransactionalSession.cs +++ /dev/null @@ -1,24 +0,0 @@ -using System; -using System.Threading; -using System.Threading.Tasks; - -namespace Light.SharedCore.DatabaseAccessAbstractions; - -/// -/// Represents an asynchronous session to a database that is able to create individual transactions. -/// The connection to the database can be terminated by calling . -/// Changes can be committed by first creating a transaction using -/// and later committing this transaction. -/// PLEASE REMEMBER: when your connection to the database always uses a single transaction, please -/// consider using the interface instead. Avoid nesting of several transactions -/// as the interfaces in this package do not cover trees of transactions. -/// -public interface IAsyncTransactionalSession : IAsyncReadOnlySession -{ - /// - /// Creates a new transaction. Please ensure not to call this method while another transaction - /// is still active in your current scope (to avoid nesting transactions). - /// - /// The token to cancel this asynchronous operation (optional). - ValueTask BeginTransactionAsync(CancellationToken cancellationToken = default); -} \ No newline at end of file diff --git a/Code/Light.SharedCore/DatabaseAccessAbstractions/ISession.cs b/Code/Light.SharedCore/DatabaseAccessAbstractions/ISession.cs index 8b86dc9..9f90f11 100644 --- a/Code/Light.SharedCore/DatabaseAccessAbstractions/ISession.cs +++ b/Code/Light.SharedCore/DatabaseAccessAbstractions/ISession.cs @@ -1,22 +1,23 @@ using System; +using System.Threading; +using System.Threading.Tasks; namespace Light.SharedCore.DatabaseAccessAbstractions; /// /// -/// PLEASE REMEMBER: database calls should be performed asynchronously -/// by default, especially in service apps to avoid blocking threads. -/// Consider using the interface instead. +/// Represents the base interface for an asynchronous session to a database that manipulates data. This means that +/// the implementation will either use a single dedicated transaction in the case of ADO.NET or Micro-ORMs +/// to ensure ACID properties, or the change tracking capabilities of a full ORM like Entity Framework Core. /// /// -/// Represents a synchronous session to a database. This means that -/// the implementation will either use a single dedicated transaction in the case of ADO.NET or Micro-ORMs -/// to ensure ACID properties, or the Change Tracking capabilities of a full ORM like Entity Framework Core. The -/// connection to the database can be terminated by calling -/// . Changes can be saved or committed -/// to the database by calling . -/// If your session does not manipulate data, consider deriving your session abstraction from -/// the interface instead. +/// The connection to the database can be terminated by calling +/// , the underlying transaction +/// will be automatically rolled back if was not called beforehand. +/// +/// +/// If you don't want the caller to explicitly commit the changes, consider deriving your session from +/// directly. /// /// /// @@ -26,10 +27,11 @@ namespace Light.SharedCore.DatabaseAccessAbstractions; /// to do Change Tracking which plain ADO.NET and all Micro-ORMs do not support. For this reason, /// we chose the term "session" instead of "Unit of Work", also because it is simpler to use in daily life. /// -public interface ISession : IDisposable +public interface ISession : IAsyncDisposable { /// - /// Writes or commits all changes that occurred during the session to the target database. + /// Commits all changes to the database. /// - void SaveChanges(); + /// The token to cancel this asynchronous operation (optional). + Task SaveChangesAsync(CancellationToken cancellationToken = default); } \ No newline at end of file diff --git a/Code/Light.SharedCore/DatabaseAccessAbstractions/ITransaction.cs b/Code/Light.SharedCore/DatabaseAccessAbstractions/ITransaction.cs deleted file mode 100644 index cae41c4..0000000 --- a/Code/Light.SharedCore/DatabaseAccessAbstractions/ITransaction.cs +++ /dev/null @@ -1,23 +0,0 @@ -using System; - -namespace Light.SharedCore.DatabaseAccessAbstractions; - -/// -/// -/// PLEASE REMEMBER: database calls should be performed asynchronously -/// by default, especially in service apps to avoid blocking threads. -/// Consider using instead. -/// -/// -/// Represents a synchronous transaction that can be committed. The transaction should always be disposed. -/// A rollback is performed automatically on when commit was not called -/// beforehand. -/// -/// -public interface ITransaction : IDisposable -{ - /// - /// Commits the changes made during this transaction to the database. - /// - void Commit(); -} \ No newline at end of file diff --git a/Code/Light.SharedCore/DatabaseAccessAbstractions/ITransactionalSession.cs b/Code/Light.SharedCore/DatabaseAccessAbstractions/ITransactionalSession.cs deleted file mode 100644 index dc5b69f..0000000 --- a/Code/Light.SharedCore/DatabaseAccessAbstractions/ITransactionalSession.cs +++ /dev/null @@ -1,29 +0,0 @@ -using System; - -namespace Light.SharedCore.DatabaseAccessAbstractions; - -/// -/// -/// PLEASE REMEMBER: database calls should be performed asynchronously -/// by default, especially in service apps to avoid blocking threads. Consider using the -/// instead. Furthermore, when your connection to the -/// database always uses a single transaction, please consider using the -/// or interfaces instead. -/// -/// -/// Represents a synchronous session to a database that is able to create individual transactions. -/// The connection to the database can be terminated by calling . -/// Changes can be committed by first creating a transaction using -/// and later committing this transaction. -/// Avoid nesting of several transactions as the interfaces -/// in this package do not cover trees of transactions. -/// -/// -public interface ITransactionalSession : IDisposable -{ - /// - /// Creates a new transaction. Please ensure not to call this method while another transaction - /// is still active in your current scope (to avoid nesting transactions). - /// - ITransaction BeginTransaction(); -} \ No newline at end of file