Standard Implementation of REN Unit Of Work
Here is the standard implementation of RENUnitOfWork which allows you to use pre-defined UnitOfWork:
public interface IRENUnitOfWork<TDbContext> where TDbContext : DbContext
{
/// <summary>
/// Commits the changes made in the unit of work to the database.
/// </summary>
void SaveChanges();
/// <summary>
/// Commits the changes made in the unit of work to the database asynchronously.
/// </summary>
// <param name="cancellationToken">Optional cancellationToken.</param>
/// <returns>A task representing the success of saving the changes (true if successful; otherwise, false).</returns>
Task SaveChangesAsync(CancellationToken cancellationToken = default);
/// <summary>
/// Gets a repository for a specific entity type.
/// </summary>
/// <typeparam name="TEntity">The entity type for the repository.</typeparam>
/// <returns>An instance of the repository for the specified entity type.</returns>
IRENRepository<TEntity>? GetRENRepository<TEntity>() where TEntity : class;
/// <summary>
/// Gets a repository for a specific entity type asynchronously.
/// </summary>
/// <typeparam name="TEntity">The entity type for the repository.</typeparam>
/// <returns>A task representing an instance of the repository for the specified entity type.</returns>
Task<IRENRepository<TEntity>?> GetRENRepositoryAsync<TEntity>(CancellationToken cancellationToken = default) where TEntity : class;
/// <summary>
/// Creates a transaction.
/// </summary>
// <param name="isolationLevel">System.Data.IsolationLevel for transaction.</param>
void BeginTransaction(IsolationLevel isolationLevel = IsolationLevel.ReadCommitted);
/// <summary>
/// Creates a transaction asynchronously.
/// </summary>
// <param name="isolationLevel">System.Data.IsolationLevel for transaction.</param>
// <param name="cancellationToken">Optional cancellationToken.</param>
/// <returns>A task representing the asynchronous operation.</returns>
Task BeginTransactionAsync(IsolationLevel isolationLevel = IsolationLevel.ReadCommitted, CancellationToken cancellationToken = default);
/// <summary>
/// Commits the changes on transaction object.
/// </summary>
void CommitTransaction();
/// <summary>
/// Commits the changes on transaction object asynchronously.
/// </summary>
// <param name="cancellationToken">Optional cancellationToken.</param>
/// <returns>A task representing the asynchronous operation.</returns>
Task CommitTransactionAsync(CancellationToken cancellationToken = default);
/// <summary>
/// Rollbacks the changes on transaction object.
/// </summary>
void RollbackTransaction();
/// <summary>
/// Rollbacks the changes on transaction object asynchronously.
/// </summary>
// <param name="cancellationToken">Optional cancellationToken.</param>
/// <returns>A task representing the asynchronous operation.</returns>
Task RollbackTransactionAsync(CancellationToken cancellationToken = default);
/// <summary>
/// Disposes the Unit Of Work object.
/// </summary>
void Dispose();
}Here, you can see all methods are defined as virtual which allows you to override them if necessary according to your needs. To use this pre-defined methods, you need register them in Program.cs file:
Here, the content of the RegisterRENDatabaseAccessHelpers is:
As you can see, only the RENUnitOfWork is registered. Since we will instantiate all repositories from RENUnitOfWork, we are not registering them through here.
Then you are good to go! You can use standard RENUnitOfWork like this:
You are good to go! Let's examine the Repository part and expand the usage of UnitFoWork with its full potential!
Last updated